gpt4 book ai didi

java - 有没有办法让 SonarQube 只警告不完整的 Switch 语句?

转载 作者:行者123 更新时间:2023-12-05 05:17:00 26 4
gpt4 key购买 nike

使用 Java,SonarQube 提示枚举值上的 switch 语句没有 default: 大小写。

给出的推理是:

"The requirement for a final default clause is defensive programming. The clause should either take appropriate action, or contain a suitable comment as to why no action is taken. When the switch covers all current values of an enum - and especially when it doesn't - a default case should still be used because there is no guarantee that the enum won't be extended."

我不同意以上声明 - 我希望以下行为产生警告:

  • 修改枚举,使开关不再涵盖所有情况。

通过要求默认情况 - 如果枚举发生变化,我们将不会收到警告,并且 switch 将不再处理所有情况。

最佳答案

SonarQube 让您能够 add custom rules .

假设不存在关于不完整的 switch case 覆盖的现有枚举相关规则警告,您可以简单地在 SQ 上添加您自己的规则,并在连接到您的 SQ 服务器的所有 SonarLint 实例上强制执行它。

关于java - 有没有办法让 SonarQube 只警告不完整的 Switch 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49658261/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com