mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[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:
committed by
intellij-monorepo-bot
parent
bf865ed4ae
commit
29c026eaf2
+1
-1
@@ -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;
|
||||
}
|
||||
}
|
||||
+24
@@ -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);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+17
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
+6
@@ -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;
|
||||
}
|
||||
}
|
||||
+17
@@ -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;
|
||||
}
|
||||
}
|
||||
+16
@@ -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;
|
||||
}
|
||||
}
|
||||
+24
@@ -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));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+17
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
+6
@@ -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;
|
||||
}
|
||||
}
|
||||
+17
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user