eclipse-jdt / eclipse-jdt/eclipse.jdt.ui
Feature Suggestion: Let clean up "Use pattern matching for instanceof" apply only to positive cases (optionally)
- Dominant language
- Java
- Stars
- 59
- Forks
- 127
- Avg merge
- 22h 47m
- Merged PRs (30d)
- 28
Description
Consider the following code:
```java
public class Test {
public void f() {
Object o = String.valueOf("");
if (o instanceof String) {
String a = (String) o;
System.out.println(a);
}
if (!(o instanceof String)) {
System.out.println();
} else {
String b = (String) o;
System.out.println(b);
}
if (!(o instanceof String)) {
return;
}
String c = (String) o;
System.out.println(c);
}
}
```
The clean up "Use pattern matching for instanceof" currently rewrites this to
```java
public class Test {
public void f() {
Object o = String.valueOf("");
if (o instanceof String a) {
System.out.println(a);
}
if (!(o instanceof String b)) {
System.out.println();
} else {
System.out.println(b);
}
if (!(o instanceof String c)) {
return;
}
System.out.println(c);
}
}
```
This is correct and does what the clean up is supposed to do.
However, in my personal opinion, I would usually like to apply this refactoring only to cases like for variable `a` and not for cases like `b` and `c`, because I find the use of pattern matching there hard(er) to read. And I guess that many Java developers would agree with me here.
Thus I would like to make the suggestion that it would be a nice feature, if there would be a check box for this clean up for toggling between applying it to all possible cases or only to cases with positive `instanceof` checks (where the variable declaration is within a syntactic branch that is guarded by the `instanceof`).
Contributor guide
Assessment
This issue has not been assessed yet.