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\ingroup qmllint-warnings-and-errors
6
7\title Redundant Optional Chaining
8\brief [redundant-optional-chaining] Some optional chaining lookups could be non-optional.
9
10\qmllintwarningcategory redundant-optional-chaining
11
12\section1 Redundant Optional Chaining
13
14\section2 What happened?
15Some lookups use optional chaining when it isn't necessary. This can happen when looking up enum
16values or when performing a lookup on a base that cannot be \c{null} or \c{undefined}.
17
18\section2 Why is this bad?
19An optional lookup needs to perform a runtime check that a regular lookup doesn't. These extra
20instructions cannot always be determined to be redundant and optimized out by the tooling. They
21then add an extra runtime performance cost and bloat the program unnecessarily.
22
23Additionally, the warning may hint that the optional lookup was performed on the wrong base in the
24chain. See the next section for a more concrete example.
25
26\section2 Example
27\qml
28// Main.qml
29import QtQml
30
31QtObject {
32 // Main will either be resolved and always work, or throw an exception or fail to compile
33 enum E { A, B, C }
34 property int i: Main?.A
35
36 // A url cannot be null or undefined
37 property url u: ""
38 property string s: u?.toString()
39
40 // Did you mean to make the second lookup optional?
41 property int i: Safe?.Unsafe.i
42}
43\endqml
44To fix these warnings, replace the redundant optional lookups with non-optional ones:
45\qml
46// Main.qml
47import QtQml
48
49QtObject {
50 enum E { A, B, C }
51 property int i: Main.A
52
53 property url u: ""
54 property string s: u.toString()
55
56 property int i: Safe.Unsafe?.i
57}
58\endqml
59*/