cnumr / ecoCode

Reduce the environmental footprint of your software applications with this cutting-edge sonarQube plugin
GNU General Public License v3.0
62 stars 48 forks source link

The rule "The variable value is not assigned" (JAVA) can report false positives. #144

Open inguemark-spec opened 2 years ago

inguemark-spec commented 2 years ago

The rule "The variable value is not assigned" (JAVA) can report false positives.

For example :

private void test() {

    Object value = getValue ();

    if (value instanceof String stringValue)
    {
        Logger.getLogger("this is a String : " + stringValue);
    }

    if (value instanceof Integer intValue)
    {
        Logger.getLogger("this is a Integer : " + intValue);
    }

}

In this case, the variable is indeed used in the condition, and is then cast.

wokier commented 2 years ago

Note: This is a Java 17 language feature https://docs.oracle.com/en/java/javase/17/language/pattern-matching-instanceof-operator.html