Warn about using assignment in conditions

This warning category is spelled [warn-assignment-in-condition] by qmllint.

Warn about using assignment in conditions

What happened?

You used an assignment inside an if-condition.

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.

Example

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
    }
}

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.

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
    }
}

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.