Search in sources :

Example 1 with SwitchLike

use of org.sonar.uast.helpers.SwitchLike in project sonar-go by SonarSource.

the class AllBranchesAreIdenticalCheck method handleSwitch.

private void handleSwitch(UastNode node) {
    SwitchLike switchLike = SwitchLike.from(node);
    if (switchLike == null) {
        return;
    }
    List<UastNode> caseNodes = switchLike.caseNodes();
    if (caseNodes.size() < 2) {
        return;
    }
    UastNode firstCase = CaseLike.from(caseNodes.get(0)).body();
    if (caseNodes.stream().noneMatch(UastNode.Kind.DEFAULT_CASE)) {
        return;
    }
    boolean allEquivalent = caseNodes.stream().skip(1).map(caseNode -> CaseLike.from(caseNode).body()).allMatch(body -> syntacticallyEquivalent(firstCase, body));
    if (allEquivalent) {
        reportIssue(switchLike.switchKeyword(), MESSAGE);
    }
}
Also used : HashSet(java.util.HashSet) InputFile(org.sonar.api.batch.fs.InputFile) List(java.util.List) Uast.syntacticallyEquivalent(org.sonar.uast.Uast.syntacticallyEquivalent) Set(java.util.Set) UastNode(org.sonar.uast.UastNode) Rule(org.sonar.check.Rule) IfLike(org.sonar.uast.helpers.IfLike) ElseLike(org.sonar.uast.helpers.ElseLike) SwitchLike(org.sonar.uast.helpers.SwitchLike) CaseLike(org.sonar.uast.helpers.CaseLike) SwitchLike(org.sonar.uast.helpers.SwitchLike) UastNode(org.sonar.uast.UastNode)

Example 2 with SwitchLike

use of org.sonar.uast.helpers.SwitchLike in project sonar-go by SonarSource.

the class NoIdenticalConditionsCheck method handleSwitch.

private void handleSwitch(UastNode node) {
    SwitchLike switchLike = SwitchLike.from(node);
    if (switchLike == null) {
        return;
    }
    List<UastNode> caseNodes = switchLike.caseNodes();
    List<UastNode> allConditions = new ArrayList<>();
    for (UastNode caseNode : caseNodes) {
        CaseLike caseLike = CaseLike.from(caseNode);
        List<UastNode> conditions = caseLike.conditions();
        for (UastNode condition : conditions) {
            for (UastNode prevCondition : allConditions) {
                checkConditions(condition, prevCondition);
            }
            allConditions.add(condition);
        }
    }
}
Also used : SwitchLike(org.sonar.uast.helpers.SwitchLike) ArrayList(java.util.ArrayList) UastNode(org.sonar.uast.UastNode) CaseLike(org.sonar.uast.helpers.CaseLike)

Aggregations

UastNode (org.sonar.uast.UastNode)2 CaseLike (org.sonar.uast.helpers.CaseLike)2 SwitchLike (org.sonar.uast.helpers.SwitchLike)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 InputFile (org.sonar.api.batch.fs.InputFile)1 Rule (org.sonar.check.Rule)1 Uast.syntacticallyEquivalent (org.sonar.uast.Uast.syntacticallyEquivalent)1 ElseLike (org.sonar.uast.helpers.ElseLike)1 IfLike (org.sonar.uast.helpers.IfLike)1