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?
16
Some lookups use optional chaining when it isn't necessary. This can happen when looking up enum
17
values or when performing a lookup on a base that cannot be \c{null} or \c{undefined}.
18
19
\section2 Why is this bad?
20
An optional lookup needs to perform a runtime check that a regular lookup doesn't. These extra
21
instructions cannot always be determined to be redundant and optimized out by the tooling. They
22
then add an extra runtime performance cost and bloat the program unnecessarily.
23
24
Additionally, the warning may hint that the optional lookup was performed on the wrong base in the
25
chain. See the next section for a more concrete example.
26
27
\section2 Example
28
\qml
29
// Main.qml
30
import QtQml
31
32
QtObject {
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
45
To fix these warnings, replace the redundant optional lookups with non-optional ones:
46
\qml
47
// Main.qml
48
import QtQml
49
50
QtObject {
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
*/
qtdeclarative
src
qml
doc
src
qmllint
redundantOptionalChaining.qdoc
Generated on
for Qt by
1.14.0