[false-negative]S2133 can not detect getClass called by array of classes

Affects Sonarqube Version:
Sonarqube version: 8.1.0.31237
sonar-scanner version: 4.2.0.1873-windows

Rule:
S2133: Objects should not be created only to “getClass”

Description:
Unable to detect getClass called by array of classes,such as: new Object[0].getClass()
This rule is implemented in java-checks-6.2.0.21135-sources.jar!\org\sonar\java\checks\ObjectCreatedOnlyToCallGetClassCheck.java.
This rule uses TypeCriteria.subtypeOf(“java.lang.Object”)).name(“getClass”).withoutParameter() to filter class objects that may have errors, but the array of class objects may be ignored.

Code Sample demonstrating the issue:

assertEquals(new Long[0].getClass(), types[0]);
Constructor<?> constructor = nsDataClass.getConstructor(new Class[]{new byte[0].getClass()});

Expected outcome:
false-negative

Running Sonarqube through:
command line

Hello,
Thanks for reporting this false negative, I created a ticket (SONARJAVA-3442) to keep track of it.

For the record, it seems that the problem is not coming from the line you stated since it changed since version 6.2.
In any case, it should not be too hard to identify.

Best,
Quentin

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