令人困惑的减分
此警告类别由 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 } }
© 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.