Sonar问题 – switch语句应以无条件的“break”语句结束

huangapple go评论118阅读模式
英文:

Sonar issue - Switch cases should end with an unconditional "break" statement

问题

Sonar在下面的代码中将案例EQUALS标记为sonar问题squid:S128。

  • Switch语句应以无条件的“break”语句结束。
    我不认为在这种情况下我需要添加“break”语句。
    有人可以帮助我吗?这是误报吗?

提前致谢。

英文:

I would like to ask you for your help with sonar issue. Sonar marked case EQUALS in the code below as sonar issue squid:S128.

-Switch cases should end with an unconditional "break" statement.
I dont think that in this case I have to add "break" statement.
Can someone help me? Is it false positive issue?

Thank you in advance.

  1. public boolean causeException(Throwable throwable) {
  2. Throwable causeException = ExceptionUtils.getRootCause(throwable);
  3. Map<String, MatchMode> configuration = infoMessage.get(causeException.getClass());
  4. String message = throwable.getMessage();
  5. for (String key : configuration.keySet()) {
  6. MatchMode matchMode = configuration.get(key);
  7. switch (matchMode) {
  8. case EQUALS:
  9. if (message.equals(key)) {
  10. return true;
  11. }
  12. case CONTAINS:
  13. if (message.contains(key)) {
  14. return false;
  15. }
  16. }
  17. }
  18. return false;
  19. }

答案1

得分: 2

你的消息与关键字不相等时,会出现 fallthrough。你需要添加 break 来解决这个问题:

  1. public boolean causeException(Throwable throwable) {
  2. Throwable causeException = ExceptionUtils.getRootCause(throwable);
  3. Map<String, MatchMode> configuration = infoMessage.get(causeException.getClass());
  4. String message = throwable.getMessage();
  5. for (String key : configuration.keySet()) {
  6. MatchMode matchMode = configuration.get(key);
  7. switch (matchMode) {
  8. case EQUALS:
  9. if (message.equals(key)) {
  10. return true;
  11. }
  12. break; // 停止 fallthrough
  13. // 如果没有 "break",它还会执行 CONTAINS 块
  14. case CONTAINS:
  15. if (message.contains(key)) {
  16. return true;
  17. }
  18. break; // 停止 fallthrough
  19. default:
  20. // 仅为了清晰起见
  21. break;
  22. }
  23. }
  24. return false;
  25. }
英文:

You're going to get fallthrough if you message doesn't equal key. You need to add breaks to address this issue:

  1. public boolean causeException(Throwable throwable) {
  2. Throwable causeException = ExceptionUtils.getRootCause(throwable);
  3. Map&lt;String, MatchMode&gt; configuration = infoMessage.get(causeException.getClass());
  4. String message = throwable.getMessage();
  5. for (String key : configuration.keySet()) {
  6. MatchMode matchMode = configuration.get(key);
  7. switch (matchMode) {
  8. case EQUALS:
  9. if (message.equals(key)) {
  10. return true;
  11. }
  12. break; // stops fallthrough
  13. // With no &quot;break&quot; it would do the CONTAINS block as well
  14. case CONTAINS:
  15. if (message.contains(key)) {
  16. return true;
  17. }
  18. break; // stops fallthrough
  19. default:
  20. // just for clarity
  21. break;
  22. }
  23. }
  24. return false;
  25. }

huangapple
  • 本文由 发表于 2020年8月31日 23:18:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63673541.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定