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
warn-assignment-in-condition.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 Warn about using assignment in conditions
8\brief [assignment-in-condition] An assignment statement is used inside a condition.
9
10\qmllintwarningcategory assignment-in-condition
11
12\section1 Warn about using assignment in conditions
13
14\section2 What happened?
15You used an assignment inside an \c{if-condition}.
16
17\section2 Why is this bad?
18This is often a mistake, and a comparison should have been used. If it was intentional, it is still
19often considered confusing.
20
21\section2 Example
22\qml
23import QtQuick
24
25Item {
26 id: root
27 Component.onCompleted: {
28 // mistake: should have been a comparison
29 if (width = height)
30 console.log("A square")
31 let mypoint = Qt.point(1,2)
32 let hit = false
33 // intentional, but possibly misleading
34 if (hit = root.contains(myPoint))
35 console.log("hit")
36 root.enabled = hit
37 }
38}
39\endqml
40
41To fix this warning, change the assignment to a comparison if it was a mistake. Otherwise, wrap the
42assignment into parentheses to indicate that it was done intentionally.
43
44\qml
45import QtQuick
46
47Item {
48 id: root
49 Component.onCompleted: {
50 // fixed
51 if (width === height)
52 console.log("A square")
53 let mypoint = Qt.point(1,2)
54 let hit = false
55 // intentional
56 if ((hit = root.contains(point)))
57 console.log("hit")
58 root.enabled = hit
59 }
60}
61\endqml
62*/