netbeans
netbeans copied to clipboard
Incorrect code analysis (unbalanced-hint)
Apache NetBeans version
Apache NetBeans 15 release candidate
What happened
Java variable is being used but NB says it isn't. See two images
.
How to reproduce
No response
Did this work correctly in an earlier version?
No / Don't know
Operating System
Fedora Linux
JDK
8
Apache NetBeans packaging
Apache NetBeans provided installer
Anything else
No response
Are you willing to submit a pull request?
No
Code of Conduct
Yes
Confirmed. This appears to be a regression caused by #4421 as far as I can tell. cc @mbien can you take a look?
seems to be specific to final array fields with write ops.
private final static char[] bar1 = {'y', 'n'}; // marked unused
private final static char[] bar2 = new char[2]; // ok
public static void main(String[] args) {
final char[] foo1 = {'y', 'n'}; // marked unused
final char[] foo3 = new char[] {'y', 'n'}; // marked unused
char[] foo2 = {'y', 'n'}; // ok
final char[] foo4 = new char[2]; // ok
char[] foo5; // ok
foo5 = new char[] {'Y', 'n'};
System.out.println(foo1);
System.out.println(foo2);
System.out.println(foo3);
System.out.println(foo4);
System.out.println(foo5);
System.out.println(bar1);
System.out.println(bar2);
}
reverting the array handler of #4421 fixes this issue but it would break #4402 again for arrays. Can't come up with a quick way of fixing this atm. Shame we haven't noticed this earlier so we could have simply reverted the PR.
diff --git a/java/java.hints/src/org/netbeans/modules/java/hints/bugs/Unbalanced.java b/java/java.hints/src/org/netbeans/modules/java/hints/bugs/Unbalanced.java
index 51829b5..3c3d3ae 100644
--- a/java/java.hints/src/org/netbeans/modules/java/hints/bugs/Unbalanced.java
+++ b/java/java.hints/src/org/netbeans/modules/java/hints/bugs/Unbalanced.java
@@ -152,7 +152,7 @@
if (secondAccess != null) {
record(ctx.getInfo(), var, secondAccess);
}
- } else if (!var.getModifiers().contains(Modifier.FINAL)) {
+ } else {
record(ctx.getInfo(), var, State.WRITE, State.READ);
}
Thanks @mbien I think the whole PR might need reverting - there's a problem with collections as well as arrays. This shows a warning in 15 but not in 14.
public class Test1 {
private final List<String> collection = new ArrayList<>();
public Test1() {
use(collection);
}
public boolean isEmpty() {
return collection.isEmpty();
}
private void use(List<String> list) {
list.add("FOO");
}
}
We might need some tests for not containing this warning too? :smile:
Shame we haven't noticed this earlier so we could have simply reverted the PR.
Shame none of the 4 requested reviewers looked at it either!
Here's something which is probably the same issue.
public void foo(List<?> list) {
final Object[] listeners = list.toArray(); //<<<<<<<<<< hint on listeners
Objects.requireNonNull(listeners);
}
The first listeners
get the hint
The array is only written to, never read from
Remove the "final" and the hint goes away
Might be related to this issue:
static record Thing(boolean isOkay) {}
void neverRead(Thing thing) {
List<Thing> things = new ArrayList<>(); // NB claims only added to, never read
things.add(thing);
things.forEach(System.out::println); // but here it is read
}