易混淆的减号
qmllint 将此警告类别拼写为“[confusing-minuses] ”。
易混淆的减号
发生了什么?
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 可能不等同于a + b ,这取决于 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.