The following code produces a FP from Java rule S1170.
private static long nextID = 1L;
private final long localID = nextID++; // FP
The rule suggests to make this final field static, too. But obviously it cannot be static.
The following code produces a FP from Java rule S1170.
private static long nextID = 1L;
private final long localID = nextID++; // FP
The rule suggests to make this final field static, too. But obviously it cannot be static.
Hey @mfroehlich
I think you know the template at this point.
What product(s) are you using and what version?
Ah, sorry, yes.
Qube: Community 9.9
Hey @mfroehlich,
Thanks for your report. If I’m not mistaken the rule checks that the field is not used in non-static members and fields, so it can be static.
In your case, it’s hard to say anything if the field can be static because we need to examine the whole class. If you’re suggesting that the rule should also take into consideration the name, then I think local
is a bit confusing as it might not refer to the “unique for instance”, but something else.
If you feel that this is an FP, please, provide the full class, and the explanation.
Best,
Margarita
Sorry, my example was indeed not sufficient. You don’t need the complete class though. And no, I’m not suggesting to evaluate the name.
public class C
{
private static long nextID = 1L;
private final long localID = nextID++; // FP
public int getID()
{
return localID;
}
}
So, the rule would just need to look at the whole class, and if the field is used in a non static context.
Thanks for the detailed report, I was able to reproduce the issue. This is a quite old rule and in indeed doesn’t check if the field is used in non-static members. I created a ticket to fix it:
https://sonarsource.atlassian.net/browse/SONARJAVA-4749
Best,
Margarita
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.