未结束的非空 case 代码块
qmllint 将此警告类别拼写为“[unterminated-case] ”。
未结束的非空 case 代码块
发生了什么?
switch 语句中的某个 case 代码块内容不为空,但未通过 break、return 或 throw 语句结束。此外,该 code block 也没有包含“允许向下传递”的注释。
为什么这样不好?
switch 语句中的向下传递逻辑可能会令人困惑或被忽略。如果是故意设计的,应使用“// fallthrough”之类的注释明确标注。如果并非有意为之,则该警告表明遗漏了终止语句。
示例
switch (state) {
case Main.State.Reset:
clearState() // <--- "Unterminated non-empty case block"
case Main.State.Invalid:
asyncInitState()
return false
case Main.State.Initializing:
// wait for initialization to complete
return false
case Main.State.Ready:
res = lookup(query)
log(res.stats)
saveToDisk(res.data) // <--- "Unterminated non-empty case block"
default:
throw new InvalidStateException("Unknown state")
}要修复此警告,请添加缺失的终止语句,或添加明确允许落入后续逻辑的注释:
switch (state) {
case Main.State.Reset:
clearState()
// fallthrough // <--- add fallthrough comment
case Main.State.Invalid:
asyncInitState()
return false
case Main.State.Initializing:
// wait for initialization to complete
return false
case Main.State.Ready:
res = lookup(query)
log(res.stats)
saveToDisk(res.data)
return true // <--- add forgotten terminating statement
default:
throw new InvalidStateException("Unknown state")
}© 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.