- Qt 6.11
- Qt Qml
- QML Lint Warning and Errors
- Redundant Optional Chaining
- Qt 6.11.0 Reference Documentation

# 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
     }