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