本页内容

非故意的空代码块

qmllint 将此警告类别拼写为“[unintentional-empty-block] ”。

非预期的空代码块

发生了什么?

将一个空代码块声明为属性绑定的表达式。

您可能本意是声明一个空对象字面量。要实现这一点,请将字面量用圆括号括起来。这是为了将字面量与空代码块区分开来。

为什么这样不好?

将一个不包含任何指令的代码块作为属性绑定,不会产生任何效果,并且可能会让读者感到困惑。在评估该绑定时,不会执行任何指令,且该绑定的评估结果将为 `undefined`。

示例

import QtQml

QtObject {
    property var v: {} // This is not an empty object literal!
}

要解决此警告,请将对象字面量用圆括号括起来,或者直接删除该绑定:

import QtQml

QtObject {
    property var v: ({}) // This is an empty object literal
}

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