[java-inspections] IDEA-360880 Replace cast expressions with pattern variable does nothing inside of ternary operator

- use PsiStatement as scope instead of PsiIfStatement
- more tests

GitOrigin-RevId: 2602190296988a80d54520c7a0f15fd0f8e3d942
This commit is contained in:
Mikhail Pyltsin
2024-10-28 21:01:30 +00:00
committed by intellij-monorepo-bot
parent bf865ed4ae
commit 29c026eaf2
13 changed files with 173 additions and 1 deletions
@@ -307,7 +307,7 @@ public final class PatternVariableCanBeUsedInspection extends AbstractBaseJavaLo
if (instanceOfType != null && instanceOfType.getType() instanceof PsiClassType classType && !classType.isRaw()) {
typeElement = instanceOfType;
}
PsiIfStatement psiIfStatement = PsiTreeUtil.getParentOfType(instanceOf, PsiIfStatement.class);
PsiStatement psiIfStatement = PsiTreeUtil.getParentOfType(instanceOf, PsiStatement.class);
if (psiIfStatement == null || psiIfStatement.getParent() == null) return;
var visitor = new JavaRecursiveElementVisitor() {
final List<PsiTypeCastExpression> myCasts = new ArrayList<>();
@@ -0,0 +1,16 @@
// "Replace cast expressions with pattern variable" "true"
public final class A {
private static boolean foo3(Number o, Number n) {
if (!(o instanceof Integer integer)) {
return false;
}
if (integer.describeConstable().get() == 1) {
return true;
}
if (integer.describeConstable().get() == 2) {
return true;
}
return false;
}
}
@@ -0,0 +1,24 @@
// "Replace cast expressions with pattern variable" "true"
public final class A {
private static Object foo5(Object o) {
return new Runnable() {
@Override
public void run() {
if (!(o instanceof Integer integer)) {
return;
}
final String string = integer.toString();
if (string.equals("1")) {
return;
}
if (integer.intValue() == 1) {
return;
}
print(integer);
}
};
}
}
@@ -0,0 +1,17 @@
// "Replace cast expressions with pattern variable" "true"
public final class A {
private static boolean foo4(Number o, Number n) {
if (!(o instanceof Integer integer)) {
return false;
} else if (integer.describeConstable().get() == 1) {
return true;
}
if (integer.describeConstable().get() == 2) {
return true;
}
return integer.describeConstable().get() == 3;
}
}
@@ -0,0 +1,6 @@
// "Replace cast expressions with pattern variable" "true"
public final class A {
private static boolean foo2(Number o, Number n) {
return o instanceof Integer integer && o != n && integer.describeConstable().get() == 1;
}
}
@@ -0,0 +1,17 @@
// "Replace cast expressions with pattern variable" "true"
public final class A {
private static boolean foo4(Number o, Number n) {
if (!(o instanceof Integer integer)) {
return false;
} else if (integer.describeConstable().get() == 1) {
return true;
}
if (integer.describeConstable().get() == 2) {
return true;
}
return integer.describeConstable().get() == 3;
}
}
@@ -0,0 +1,6 @@
// "Replace cast expressions with pattern variable" "true"
public final class A {
private static void foo(Object o) {
int x = o instanceof String string ? string.length() : 0;
}
}
@@ -0,0 +1,16 @@
// "Replace cast expressions with pattern variable" "true"
public final class A {
private static boolean foo3(Number o, Number n) {
if (!(o instanceof Integer)) {
return false;
}
if (((Int<caret>eger) o).describeConstable().get() == 1) {
return true;
}
if (((Integer) o).describeConstable().get() == 2) {
return true;
}
return false;
}
}
@@ -0,0 +1,24 @@
// "Replace cast expressions with pattern variable" "true"
public final class A {
private static Object foo5(Object o) {
return new Runnable() {
@Override
public void run() {
if (!(o instanceof Integer)) {
return;
}
final String string = ((Intege<caret>r) o).toString();
if (string.equals("1")) {
return;
}
if (((Integer) o).intValue() == 1) {
return;
}
print(((Integer) o));
}
};
}
}
@@ -0,0 +1,17 @@
// "Replace cast expressions with pattern variable" "true"
public final class A {
private static boolean foo4(Number o, Number n) {
if (!(o instanceof Integer)) {
return false;
} else if (((In<caret>teger) o).describeConstable().get() == 1) {
return true;
}
if (((Integer) o).describeConstable().get() == 2) {
return true;
}
return ((Integer) o).describeConstable().get() == 3;
}
}
@@ -0,0 +1,6 @@
// "Replace cast expressions with pattern variable" "true"
public final class A {
private static boolean foo2(Number o, Number n) {
return o instanceof Integer && o != n && ((Integ<caret>er) o).describeConstable().get() == 1;
}
}
@@ -0,0 +1,17 @@
// "Replace cast expressions with pattern variable" "true"
public final class A {
private static boolean foo4(Number o, Number n) {
if (!(o instanceof Integer)) {
return false;
} else if (((In<caret>teger) o).describeConstable().get() == 1) {
return true;
}
if (((Integer) o).describeConstable().get() == 2) {
return true;
}
return ((Integer) o).describeConstable().get() == 3;
}
}
@@ -0,0 +1,6 @@
// "Replace cast expressions with pattern variable" "true"
public final class A {
private static void foo(Object o) {
int x = o instanceof String ? ((Strin<caret>g)o).length() : 0;
}
}