Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
redundantOptionalChaining.qdoc
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5\page qmllint-warnings-and-errors-redundant-optional-chaining.html
6\ingroup qmllint-warnings-and-errors
7
8\title Redundant Optional Chaining
9\brief [redundant-optional-chaining] Some optional chaining lookups could be non-optional
10
11\qmllintwarningcategory redundant-optional-chaining
12
13\section1 Redundant Optional Chaining
14
15\section2 What happened?
16Some lookups use optional chaining when it isn't necessary. This can happen when looking up enum
17values or when performing a lookup on a base that cannot be \c{null} or \c{undefined}.
18
19\section2 Why is this bad?
20An optional lookup needs to perform a runtime check that a regular lookup doesn't. These extra
21instructions cannot always be determined to be redundant and optimized out by the tooling. They
22then add an extra runtime performance cost and bloat the program unnecessarily.
23
24Additionally, the warning may hint that the optional lookup was performed on the wrong base in the
25chain. See the next section for a more concrete example.
26
27\section2 Example
28\qml
29// Main.qml
30import QtQml
31
32QtObject {
33 // Main will either be resolved and always work, or throw an exception or fail to compile
34 enum E { A, B, C }
35 property int i: Main?.A
36
37 // A url cannot be null or undefined
38 property url u: ""
39 property string s: u?.toString()
40
41 // Did you mean to make the second lookup optional?
42 property int i: Safe?.Unsafe.i
43}
44\endqml
45To fix these warnings, replace the redundant optional lookups with non-optional ones:
46\qml
47// Main.qml
48import QtQml
49
50QtObject {
51 enum E { A, B, C }
52 property int i: Main.A
53
54 property url u: ""
55 property string s: u.toString()
56
57 property int i: Safe.Unsafe?.i
58}
59\endqml
60*/