Redundant Optional Chaining

This warning category is spelled [redundant-optional-chaining] by qmllint.

Redundant Optional Chaining

What happened?

Some lookups use optional chaining when it isn't necessary. This can happen when looking up enum values or when performing a lookup on a base that cannot be null or undefined.

Why is this bad?

An optional lookup needs to perform a runtime check that a regular lookup doesn't. These extra instructions cannot always be determined to be redundant and optimized out by the tooling. They then add an extra runtime performance cost and bloat the program unnecessarily.

Additionally, the warning may hint that the optional lookup was performed on the wrong base in the chain. See the next section for a more concrete example.

Example

// Main.qml
import QtQml

QtObject {
    // Main will either be resolved and always work, or throw an exception or fail to compile
    enum E { A, B, C }
    property int i: Main?.A

    // A url cannot be null or undefined
    property url u: ""
    property string s: u?.toString()

    // Did you mean to make the second lookup optional?
    property int i: Safe?.Unsafe.i
}

To fix these warnings, replace the redundant optional lookups with non-optional ones:

// Main.qml
import QtQml

QtObject {
    enum E { A, B, C }
    property int i: Main.A

    property url u: ""
    property string s: u.toString()

    property int i: Safe.Unsafe?.i
}

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.