Warnung vor der Verwendung von Zuweisungen in Bedingungen

Diese Warnkategorie wird von qmllint mit [warn-assignment-in-condition] angegeben.

Warnung vor der Verwendung von Zuweisungen in Bedingungen

Was ist passiert?

Sie haben eine Zuweisung innerhalb einer if-condition verwendet.

Warum ist das schlecht?

Dies ist oft ein Fehler, und es hätte ein Vergleich verwendet werden müssen. Auch wenn es Absicht war, wird es oft als verwirrend angesehen.

Beispiel

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

Um diese Warnung zu beheben, ändern Sie die Zuweisung in einen Vergleich, wenn es ein Fehler war. Andernfalls schließen Sie die Zuweisung in Klammern ein, um anzuzeigen, dass dies absichtlich geschehen ist.

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.