google / google/error-prone

Patch suggestion of PatternMatchingInstanceOf introduces duplicate variable names

Open
#4,922 5 comments 3 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
7.2k
Forks
820
Avg merge
5h 9m
Merged PRs (30d)
50

Description

In Error Prone 2.37.0, the check PatternMatchingInstanceOf produces suggestions that use variable names that are already used in the existing code, or even use the same variable name twice. This fails to compile.

Example:
```java
public class Test2 {

int test_two_variables() {
Object o1 = 1;
Object o2 = 1;
// Here Error Prone uses the variable name "i" twice.
if (o1 instanceof Integer && o2 instanceof Integer) {
return ((Integer) o1) + ((Integer) o2);
}
return 0;
}

void test_local_variable() {
Object o = 1;
// Here Error Prone uses the variable name "i" that is already used.
Integer i = o instanceof Integer ? (Integer) o : 0;
}
}
```

Generated suggestion:
```diff
--- Test.java
+++ Test.java
@@ -5,6 +5,6 @@
Object o2 = 1;
// Here Error Prone uses the variable name "i" twice.
- if (o1 instanceof Integer && o2 instanceof Integer) {
- return ((Integer) o1) + ((Integer) o2);
+ if (o1 instanceof Integer i && o2 instanceof Integer i) {
+ return i + i;
}
return 0;
@@ -14,5 +14,5 @@
Object o = 1;
// Here Error Prone uses the variable name "i" that is already used.
- Integer i = o instanceof Integer ? (Integer) o : 0;
+ Integer i = o instanceof Integer i ? i : 0;
}
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.