[java:S2129] Improve message

Qube: Community 9.9

The Java rule S2129 tells me to “remove the Float constructor”. It should better say “Use Float.valueOf instead”.


Thank you @mfroehlich for your feedback. The rule S2129 seems to be pretty clear and it comes with a quick fix that suggests either replacing the constructor with the literal value, or the BigInteger.valueOf().

In your specific case, the quick fix should be suggesting you Replace this “Float” constructor with the float literal passed as parameter, which in my opinion is clear enough.

Don’t you think this is clear and helpful enough?


Ah, then I didn’t read the description carefully enough. Yes, using the literal is what should be done. And then the description is indeed clear enough.

Shouldn’t the rule also recognize this code then?

Float f = Float.valueOf( 123f );

1 Like

I checked the rule and it doesn’t raise any issue for Float f = Float.valueOf(123f); . Can you confirm that this is the case for you?

Under the hood Float.valueOf(123f) is still using new Float(123f), and thus the most efficient solution would be to use the primitive directly: 123f. If you’re using IntelliJ you should see the use of valueOf highlighted because it is unnecessary.

In my opinion, it would be out of scope to cover such cases in rule S2129 unless we decide to broaden its scope on purpose; alternatively this may be an idea for a new rule.

No, it doesn’t raise an issue for Float.valueOf(123f). My thought was, that it should do that, because I replaced the direct usages of new Float(123f) with valueOf. And to eventually encourage me to directly use the primitive, it should also raise an issue for valueOf.

I am using Eclipse.

1 Like

Thank you @mfroehlich for sharing these details. I have created an issue to improve the rule, you can check the progress on [SONARJAVA-4641] - Jira.