【问题标题】:SonarQube - Java rule "S128" - Why the rule complains about the fact that a break statement is missing when this is obviously not necessary?SonarQube - Java 规则“S128” - 为什么规则抱怨在显然没有必要时缺少 break 语句的事实?
【发布时间】:2021-05-09 14:19:50
【问题描述】:

我真的不明白为什么 Sonar 一直抱怨我“没有休息声明”这一事实,即使它不需要。

我的开关:

    public static String lookupVoyageId(String referenceNumber, String sender) {
    switch (sender) {
        case "400_HGENT":
        case "200_HAPEN":
        case "500_HOOST":
            Preconditions.checkArgument(referenceNumber.contains("-"));
            return referenceNumber.split("-")[0];
        case "600_HZEEB":
            Preconditions.checkArgument(referenceNumber.length() >= 6);
            return referenceNumber.substring(0, 6);
        case "800_BVL":
            throw new TransferException("This reference number for IBIS isn't according to the requirements. Can't implement it yet.");
        case "MCCD":
            throw new TransferException("This reference number for MCCD isn't according to the requirements. Can't implement it yet.");
        default:
            throw new TransferException("The sender (" + sender + ") couldn't be identified.");
    }
}

声纳不断给我关键: "switch 语句不包含 break"

这是为什么?我不需要在此开关中进行任何中断?

我知道这可能是一个特定的案例,但我在网上找不到任何东西。

【问题讨论】:

  • 它给你的错误是哪一行?不是针对那些失败的案例吗?就个人而言,我希望在那里看到 // fall-through 评论,但我怀疑这不是 Sonar 所抱怨的......
  • @Slanec 就在开关的第一行。而且我在另一个开关中也遇到了同样的关键问题。
  • @Slanec 实际上,该评论可能会很好地修复警告。根据stackoverflow.com/questions/5479019/… sonar 使用 checkstyle 等库。并且 checkstyle (checkstyle.sourceforge.net/config_coding.html#FallThrough) 如果打算失败,则希望得到评论。
  • 如果能准确地确定引发此问题的规则键,那就太好了。如果这个问题是由规则 S128 提出的,那么这将检测到每一个失败甚至是有意的。
  • @Magnilex SonarQube 主要依靠自己的分析仪,除非另有特别说明:)

标签: java sonarqube


【解决方案1】:

如果您不能减少 switch case 的数量或无法重构代码,您可以使用

抑制警告
 @SuppressWarnings({"squid:S128", "squid:S1479"}

示例用法here

【讨论】:

    【解决方案2】:

    注意:我不是要回答这个问题。但是让我们看看这个特定的规则是怎么说的。

    规则 S128 说:

    切换案例应以无条件的“break”语句结束

    当执行未在切换结束时显式终止时 case,它继续执行下一个case的语句。 虽然这有时是故意的,但通常是一个错误导致 意外行为。

    不合规代码示例

    switch (myVariable) {
      case 1:                              
        foo();
        break;
      case 2:  // Both 'doSomething()' and 'doSomethingElse()' will be executed. Is it on purpose ?
        doSomething();
      default:                               
        doSomethingElse();
        break;
    }
    

    合规解决方案

    switch (myVariable) {
      case 1:                              
        foo();
        break;
      case 2: 
        doSomething();
        break;
      default:                               
        doSomethingElse();
        break;
    }
    

    例外情况

    以下情况放宽此规则:

    switch (myVariable) {
      case 0: // Empty case used to specify the same behavior for a group of cases.
      case 1:                               
        doSomething();
        break;
      case 2:  // Use of return statement
        return;
      case 3:   // Use of throw statement
        throw new IllegalStateException();
      default:  // For the last case, use of break statement is optional
        doSomethingElse();
    }
    

    参考资料: https://sonar.spring.io/rules/show/squid:S128?layout=false

    【讨论】:

    • 这并不是 OP 问题的真正答案或解决方案……
    • 这是一个答案。显然,sonarqube 会哭,因为使用 fall through 并不常见。但这写在黄色引号内的第二句话中。
    【解决方案3】:

    Sonar 无法知道代码 sn-p 是否按预期工作。它无法理解您的应用程序的业务逻辑,因此它不知道您的代码应该像那样工作。

    Sonar可以知道的是,这种模式(即,失败的 switch 语句)是难以发现的错误的常见来源。出于这个原因,Sonar 作为一种代码质量工具不鼓励以这种方式工作,这是减少常见错误的总体目标的一部分。

    【讨论】:

      猜你喜欢
      • 2017-10-11
      • 1970-01-01
      • 1970-01-01
      • 2016-12-20
      • 1970-01-01
      • 1970-01-01
      • 2019-02-23
      • 2012-08-12
      • 2020-04-24
      相关资源
      最近更新 更多