aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFabian Kosmale <[email protected]>2025-07-07 16:50:15 +0200
committerFabian Kosmale <[email protected]>2025-07-08 11:50:10 +0200
commitf8a1250f8c4a2ca7b28090be8c8f9f6f66cd7140 (patch)
tree1dbdb21324ad2f2c00e3c45924827d813cd2ad2c
parentdfb6642aaa0b9e333a8592deb19c43a55fd52387 (diff)
qmllint doc: Add assignment-in-conditionHEADdev
Task-number: QTBUG-127037 Pick-to: 6.10 Change-Id: Ibafc358bfec707799d947634d3dda331643c4cdd Reviewed-by: Leena Miettinen <[email protected]> Reviewed-by: Ulf Hermann <[email protected]> Reviewed-by: Sami Shalayel <[email protected]>
-rw-r--r--src/qml/doc/src/qmllint/warn-assignment-in-condition.qdoc63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/qml/doc/src/qmllint/warn-assignment-in-condition.qdoc b/src/qml/doc/src/qmllint/warn-assignment-in-condition.qdoc
new file mode 100644
index 0000000000..2c6543e10f
--- /dev/null
+++ b/src/qml/doc/src/qmllint/warn-assignment-in-condition.qdoc
@@ -0,0 +1,63 @@
+// Copyright (C) 2025 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
+
+/*!
+\page qmllint-warnings-and-errors-assignment-in-condition.html
+\ingroup qmllint-warnings-and-errors
+\qmllintwarningcategory warn-assignment-in-condition
+
+\title Warn about using assignment in conditions
+\brief [assignment-in-condition] An assignment statement is used inside a condition
+
+\section1 Warn about using assignment in conditions
+
+\section2 What happened?
+You used an assignment inside an \c{if-condition}.
+
+\section2 Why is this bad?
+This is often a mistake, and a comparison should have been used. If it was intentional, it is still
+often considered confusing.
+
+\section2 Example
+\qml
+import QtQuick
+
+Item {
+ id: root
+ Component.onCompleted: {
+ // mistake: should have been a comparison
+ if (width = height)
+ console.log("A square")
+ let mypoint = Qt.point(1,2)
+ let hit = false
+ // intentional, but possibly misleading
+ if (hit = root.contains(myPoint))
+ console.log("hit")
+ root.enabled = hit
+ }
+}
+\endqml
+
+To fix this warning, change the assignment to a comparison if it was a mistake. Otherwise, wrap the
+assignment into parentheses to indicate that it was done intentionally.
+
+\qml
+import QtQuick
+
+Item {
+ id: root
+ Component.onCompleted: {
+ // fixed
+ if (width === height)
+ console.log("A square")
+ let mypoint = Qt.point(1,2)
+ let hit = false
+ // intentional
+ if ((hit = root.contains(point)))
+ console.log("hit")
+ root.enabled = hit
+ }
+}
+\endqml
+*/
+