Unused local variable false positive


void read() throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String s; // unused local variable 's'
        while ((s = reader.readLine()) != null) {

The variable ‘s’ is used on the very next line.


Hello @Vyom-Yadav

In this example, the rule detects that you are not using the value of s, you only assign it.
Did I miss something?

Hi @Quentin
We had a full discussion on this-

Though everyone agrees that the var is unused but JLS says that-

At run time, the result of the assignment expression is the value of the variable after the assignment has occurred. The result of an assignment expression is not itself a variable.

So the value of s is read and compared to null but someone argues that it is not read and reader.readLine() is stored somewhere in a register and accessed at the time when we are comparing it to null.

There is no documentation regarding strong value in a register, etc. So it is upon you to consider this as a FP or a good positive.

Thanks for coming back with this post.

Based on the discussion, I think it is fine to consider it as a TP.


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