From 5d52119cc692cd88aa846740e6fa63ea02f361d9 Mon Sep 17 00:00:00 2001 From: Mikhail Pyltsin Date: Wed, 11 Jan 2023 15:17:23 +0100 Subject: [PATCH] [java-inspections] IDEA-274033: Inspection to convert instanceof pattern to old school instanceof. add tests GitOrigin-RevId: 393b56ff099abd3a02ffceb425309d1bbad51bdc --- .../PatternVariablesCanBeReplacedWithCastInspection.java | 3 ++- .../patternVariablesCanBeReplacedWithCast/afterSimple.java | 5 +++++ .../patternVariablesCanBeReplacedWithCast/beforeSimple.java | 5 +++++ .../afterSimple.java | 5 +++++ .../beforeSimple.java | 5 +++++ 5 files changed, 22 insertions(+), 1 deletion(-) diff --git a/java/java-impl-inspections/src/com/intellij/codeInspection/PatternVariablesCanBeReplacedWithCastInspection.java b/java/java-impl-inspections/src/com/intellij/codeInspection/PatternVariablesCanBeReplacedWithCastInspection.java index 8385b3c41c5e..f6a9a1354cb9 100644 --- a/java/java-impl-inspections/src/com/intellij/codeInspection/PatternVariablesCanBeReplacedWithCastInspection.java +++ b/java/java-impl-inspections/src/com/intellij/codeInspection/PatternVariablesCanBeReplacedWithCastInspection.java @@ -98,7 +98,8 @@ public class PatternVariablesCanBeReplacedWithCastInspection extends AbstractBas PsiElement parent = current.getParent(); while (parent instanceof PsiParenthesizedExpression || parent instanceof PsiPolyadicExpression || - parent instanceof PsiPrefixExpression) { + parent instanceof PsiPrefixExpression || + parent instanceof PsiConditionalExpression) { current = parent; parent = current.getParent(); } diff --git a/java/java-tests/testData/inspection/patternVariablesCanBeReplacedWithCast/afterSimple.java b/java/java-tests/testData/inspection/patternVariablesCanBeReplacedWithCast/afterSimple.java index bc8d7b854a0f..066388c612df 100644 --- a/java/java-tests/testData/inspection/patternVariablesCanBeReplacedWithCast/afterSimple.java +++ b/java/java-tests/testData/inspection/patternVariablesCanBeReplacedWithCast/afterSimple.java @@ -275,4 +275,9 @@ public class Simple{ System.out.println(s); } } + + public static void ternary(Object object) { + String result = object instanceof String ? (String) object : "1"; + String result2 = object instanceof String text ? text = "2" : "1"; + } } \ No newline at end of file diff --git a/java/java-tests/testData/inspection/patternVariablesCanBeReplacedWithCast/beforeSimple.java b/java/java-tests/testData/inspection/patternVariablesCanBeReplacedWithCast/beforeSimple.java index bc83ab2ae9cc..e0099ee50a6d 100644 --- a/java/java-tests/testData/inspection/patternVariablesCanBeReplacedWithCast/beforeSimple.java +++ b/java/java-tests/testData/inspection/patternVariablesCanBeReplacedWithCast/beforeSimple.java @@ -234,4 +234,9 @@ public class Simple{ System.out.println(s); } } + + public static void ternary(Object object) { + String result = object instanceof String text ? text : "1"; + String result2 = object instanceof String text ? text = "2" : "1"; + } } \ No newline at end of file diff --git a/java/java-tests/testData/inspection/patternVariablesCanBeReplacedWithCastNotPreserve/afterSimple.java b/java/java-tests/testData/inspection/patternVariablesCanBeReplacedWithCastNotPreserve/afterSimple.java index 740c80aaaefb..d85af2d43ba4 100644 --- a/java/java-tests/testData/inspection/patternVariablesCanBeReplacedWithCastNotPreserve/afterSimple.java +++ b/java/java-tests/testData/inspection/patternVariablesCanBeReplacedWithCastNotPreserve/afterSimple.java @@ -265,4 +265,9 @@ public class Simple{ System.out.println(s); } } + + public static void ternary(Object object) { + String result = object instanceof String ? (String) object : "1"; + String result2 = object instanceof String text ? text = "2" : "1"; + } } \ No newline at end of file diff --git a/java/java-tests/testData/inspection/patternVariablesCanBeReplacedWithCastNotPreserve/beforeSimple.java b/java/java-tests/testData/inspection/patternVariablesCanBeReplacedWithCastNotPreserve/beforeSimple.java index bc83ab2ae9cc..e0099ee50a6d 100644 --- a/java/java-tests/testData/inspection/patternVariablesCanBeReplacedWithCastNotPreserve/beforeSimple.java +++ b/java/java-tests/testData/inspection/patternVariablesCanBeReplacedWithCastNotPreserve/beforeSimple.java @@ -234,4 +234,9 @@ public class Simple{ System.out.println(s); } } + + public static void ternary(Object object) { + String result = object instanceof String text ? text : "1"; + String result2 = object instanceof String text ? text = "2" : "1"; + } } \ No newline at end of file