本页内容

易混淆的加号

qmllint 将此警告类别拼写为“[confusing-pluses] ”。

易混淆的加号

发生了什么?

JavaScript 代码中以令人困惑的方式组合使用了以“+”表示的运算符。相邻的“+”运算符可能难以区分。非标准空格的使用会进一步加剧这一问题。

这有什么问题?

这会降低代码的可读性,并可能导致混淆。

示例

import QtQuick

Item {
    function f(a: int, b: int) {
        let x = a++ + b
        let y = a + +b
        let z = a + ++b
        return x + y + z
    }
}

要解决此警告,请重写代码,确保相邻的“+”运算符不再相似。尽可能简化表达式。删除多余的一元运算符和空格,并使用圆括号将子表达式隔开。

请注意,这些运算符可能会进行类型转换。诸如a + +b 这样的表达式,根据 b 的类型不同,可能与a + b 不等价。

import QtQuick

Item {
    function f(a: int, b: int) {
        let x = (a++) + b
        let y = a + b
        let z = a + b + 1
        return x + y + z
    }
}

© 2026 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.