S5411: False positive on primitive boolean

versions used (SonarLint for Eclipse 4.2.0.201909192007)

Have code that first tests Boolean for null, so reporting of “Use the primitive boolean expression here” at 2 locations in else block are false positive IMHO.

private void test(Boolean isFrom, String arg) {
		if (isFrom == null) {
			System.out.println("IsFrom is null");
		}
		else {
			if (isFrom) {
				System.out.println("true");
			}
			else if (!isFrom && arg != null) {
				System.out.println("false and have arg");
			}
		}
	}

Hello Alain, welcome to the community!

I agree that this code will never raise a NPE, but at the same time, a boxed Boolean is actually used as a boolean. Even if it is not immediately dangerous, it’s the kind of situation we want to report, this could help to avoid misunderstanding when refactoring the code for example.
In addition, supporting such case would greatly increase the complexity of the implementation, I don’t think it is worth it for something that is already arguable.

Hope this clarify the situation!

Best,
Quentin

If you modify the code as follows (redundant non null checks), it’s not reported as boxed Boolean anymore (though it still is). Either it should be reported always or never.

Please see also SONARJAVA-3570.

private void test(Boolean isFrom, String arg) {
        if (isFrom == null) {
            System.out.println("IsFrom is null");
        }
        else {
            if (isFrom!=null && isFrom) {
                System.out.println("true");
            }
            else if (isFrom!=null && !isFrom && arg != null) {
                System.out.println("false and have arg");
            }
        }
    }

Hello @Talon,

Indeed, it seems that this issue was continuously causing misunderstandings, and we decided to create SONARJAVA-3570 to improve the situation.

At the end, according to this ticket, both the initial code and the one you suggest will not be reported.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.