Java - squid:S128 - False positive with nested switch-case

36 views
Skip to first unread message

Vincent Privat

unread,
Apr 14, 2017, 2:55:08 PM4/14/17
to SonarQube
Hi,
Please find below two false positives for squid:S128 which both include nested switch-case.

Example 1:

        public boolean applies(Environment e) {
            switch(e.getContext()) {
            case PRIMITIVE:
End this switch case with an unconditional break, return or throw statement.  
  •  
  • L536
  •  
  •  
  •  
  •  
  •  
  •  
  • 10min effort
  •  
                switch (matchType) {
                case TRUE:
                    return e.osm.isKeyTrue(label) ^ negateResult;
                case FALSE:
                    return e.osm.isKeyFalse(label) ^ negateResult;
                case REGEX:
                    return e.osm.keySet().stream().anyMatch(containsPattern) ^ negateResult;
                default:
                    return e.osm.hasKey(label) ^ negateResult;
                }
            case LINK:
                Utils.ensure(false, "Illegal state: KeyCondition not supported in LINK context");
                return false;
            default: throw new AssertionError();
            }
        }

Example 2:

    protected void sort() {
        data.sort((c1, c2) -> {
                if (c1.getModificationType().equals(c2.getModificationType())) {
                    long id1 = c1.getPrimitive().getId();
                    long id2 = c2.getPrimitive().getId();
 
                    if (id1 == id2)
                        return 0;
                    else if (id1 < id2)
                        return -1;
                    return 1;
                }
                switch(c1.getModificationType()) {
                case CREATED: return -1;
                case UPDATED:
End this switch case with an unconditional break, return or throw statement.  
  •  
  • L109
  •  
  •  
  •  
  •  
  •  
  •  
  • 10min effort
  •  
                    switch(c2.getModificationType()) {
                    case CREATED: return 1;
                    default: return -1;
                    }
                case DELETED:
                    return 1;
                }
                // should not happen
                return 0;
            }
        );
    }

Regards,
Vincent

Michael Gumowski

unread,
Apr 18, 2017, 5:00:59 AM4/18/17
to Vincent Privat, SonarQube
Hello Vincent,

Thanks for the feedback. This is a known issue, which will be handled by SONARJAVA-2231.

Cheers,
Michael
--

Michael GUMOWSKI | SonarSource
Software Developer @ Language Team
http://sonarsource.com
Reply all
Reply to author
Forward
0 new messages