[false-negative]S3052 can not detect assignment which right statement has a type conversion

S3052: Fields should not be initialized to default values

Unable to detect assignment which right statement has a type conversion, for example: byte foo = (byte)0,
This rule is implemented in sonar-java-plugin-\org\sonar\java\checks\DefaultInitializedFieldCheck.java,
This rule converts the assignment(LiteralUtils.longLiteralValue(expression);, Double.valueOf(((LiteralTree) expression).value()) after using switch to classify the variable type(case CHAR_LITERAL:, case BOOLEAN_LITERAL:)
When there is a type conversion on the right side of the assignment statement, the value conversion may be wrong.

Code Sample demonstrating the issue:

private byte lastByte=(byte)0;
private float _currAlpha = (float)0.0;

Expected outcome:

Running Sonarqube through:
command line

1 Like

Hey @wongwuchiu,

Sorry for the late reply, this is indeed a False Negative (FN) and we should be able to fix it easily.
I created the following ticket to handle it: SONARJAVA-3503.


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