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