未终止的非空案块

该警告类别由 qmllint 拼写[unterminated-case]

未终止的非空案块

发生了什么?

switch 语句中的一个 case 块是非空的,但没有用 break、return 或 throw 语句终止。此外,也没有选择通过注释。

为什么会这样?

开关语句中的穿墙逻辑可能会引起混淆或被忽视。如果是有意为之,就应该用注释(如"// 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")
}

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