mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[java-refactoring] Disable introducing pattern variable when upcasting
GitOrigin-RevId: 14ef22642562271ed7f914f6337e4158bd3d43ff
This commit is contained in:
committed by
intellij-monorepo-bot
parent
b9161cc09c
commit
9ca6912853
+12
-7
@@ -316,13 +316,18 @@ final class VariableExtractor {
|
||||
PsiExpression firstOccurrence = Collections.min(allOccurrences, Comparator.comparing(e -> e.getTextRange().getStartOffset()));
|
||||
if (HighlightingFeature.PATTERNS.isAvailable(anchor)) {
|
||||
PsiTypeCastExpression cast = ObjectUtils.tryCast(PsiUtil.skipParenthesizedExprDown(firstOccurrence), PsiTypeCastExpression.class);
|
||||
if (cast != null && !(cast.getType() instanceof PsiPrimitiveType) &&
|
||||
!(PsiUtil.skipParenthesizedExprUp(firstOccurrence.getParent()) instanceof PsiExpressionStatement)) {
|
||||
PsiInstanceOfExpression candidate = InstanceOfUtils.findPatternCandidate(cast);
|
||||
if (candidate != null && allOccurrences.stream()
|
||||
.map(occ -> ObjectUtils.tryCast(PsiUtil.skipParenthesizedExprDown(occ), PsiTypeCastExpression.class))
|
||||
.allMatch(occ -> occ != null && (occ == firstOccurrence || InstanceOfUtils.findPatternCandidate(occ) == candidate))) {
|
||||
return candidate;
|
||||
if (cast != null) {
|
||||
PsiType castType = cast.getType();
|
||||
PsiExpression operand = cast.getOperand();
|
||||
if (castType != null && !(castType instanceof PsiPrimitiveType) && operand != null && operand.getType() != null &&
|
||||
!(castType.isAssignableFrom(operand.getType())) &&
|
||||
!(PsiUtil.skipParenthesizedExprUp(firstOccurrence.getParent()) instanceof PsiExpressionStatement)) {
|
||||
PsiInstanceOfExpression candidate = InstanceOfUtils.findPatternCandidate(cast);
|
||||
if (candidate != null && allOccurrences.stream()
|
||||
.map(occ -> ObjectUtils.tryCast(PsiUtil.skipParenthesizedExprDown(occ), PsiTypeCastExpression.class))
|
||||
.allMatch(occ -> occ != null && (occ == firstOccurrence || InstanceOfUtils.findPatternCandidate(occ) == candidate))) {
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
class A {
|
||||
void test(String obj) {
|
||||
if (obj instanceof String) {
|
||||
var temp = obj;
|
||||
if (temp.trim().isEmpty()) {
|
||||
System.out.println("Found");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
class A {
|
||||
void test(String obj) {
|
||||
if (obj instanceof String && <selection>((String)obj)</selection>.trim().isEmpty()) {
|
||||
System.out.println("Found");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -143,6 +143,9 @@ public class IntroduceVariableTest extends LightJavaCodeInsightTestCase {
|
||||
public void testPatternVariableDeclarationJava15Preview() {
|
||||
doTestWithVarType(new MockIntroduceVariableHandler("temp", true, false, false, JAVA_LANG_STRING));
|
||||
}
|
||||
public void testPatternVariableDeclarationUpcastJava16() {
|
||||
doTestWithVarType(new MockIntroduceVariableHandler("temp", true, false, false, JAVA_LANG_STRING));
|
||||
}
|
||||
public void testPatternVariableDeclarationUsedInLocalJava15Preview() { doTest("temp", true, false, false, JAVA_LANG_STRING);}
|
||||
public void testPatternVariableDeclarationAfterIfJava15Preview() { doTest("temp", true, false, false, JAVA_LANG_STRING);}
|
||||
public void testNonPatternVariableDeclarationTwoBlocksJava15Preview() { doTest("temp", true, false, false, JAVA_LANG_STRING);}
|
||||
|
||||
Reference in New Issue
Block a user