False-Positive of rule "Expressions should not be too complex" regarding multiple instanceof checks

  • versions used:
    - Version 7.9.1 (build 27448) Community Edition
    - SonarJava: 5.14 (build 18788)

In my opinion the rule “squid:S1067: Expressions should not be too complex” creates a false-positive in case of just multiple instanceof checks (I know this is not good OO design). The code is not getting significantly more complex if you add another instanceof check.
(Perhaps there are other scenarios of the same type.)

  private static boolean isRelevant(Object artifact) {
    return artifact instanceof A
        || artifact instanceof B
        || artifact instanceof C
        || artifact instanceof D
        || artifact instanceof E
        || artifact instanceof F
        || artifact instanceof G
        || artifact instanceof H
        || artifact instanceof I;

This rule does not care about number of instanceof but about number of conditional operators. Obviously there are pretty a lot of them. If you believe this code does not reduce the quality of the project just won't fix the issue, but that’s not a FP in terms of rule definition.

1 Like

We actually have a false positive case to demonstrate. In this constructor all of logical OR’s are counted, but the count does not restart for each statement, instead counting within the scope of the method, despite the individual statement scopes.

Completxity_Image

Is this actually a bug, or something we can configure?

Hello @rjohnson,

I am afraid you got confused, your message is not targeting the same language (JS and not Java), and not the same rule!

In fact, the example that you show is about Cognitive Complexity of functions, not complexity of expressions, and it seems to behave as expected.

Hope this helps,
Quentin

1 Like