mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[java-inspections] SimplifyBooleanExpressionFix: disallow retarget to existing pattern variable if variable was changed (IDEA-336123)
GitOrigin-RevId: 82ce60a815b80bad2593de8f5a20af7e53acb5c7
This commit is contained in:
committed by
intellij-monorepo-bot
parent
51849dbdf2
commit
08632d3227
+10
-2
@@ -140,7 +140,15 @@ public class SimplifyBooleanExpressionFix extends PsiUpdateModCommandAction<PsiE
|
||||
(PsiTypeCastExpression)JavaPsiFacade.getElementFactory(expression.getProject()).createExpressionFromText("(a)b", expression);
|
||||
Objects.requireNonNull(cast.getCastType()).replace(checkType);
|
||||
Objects.requireNonNull(cast.getOperand()).replace(instanceOf.getOperand());
|
||||
return InstanceOfUtils.findPatternCandidate(cast);
|
||||
PsiInstanceOfExpression candidate = InstanceOfUtils.findPatternCandidate(cast);
|
||||
if (candidate == null) return null;
|
||||
PsiPrimaryPattern pattern = candidate.getPattern();
|
||||
if (pattern != null) {
|
||||
if (!(pattern instanceof PsiTypeTestPattern existingTypeTest)) return null;
|
||||
PsiPatternVariable existingVar = existingTypeTest.getPatternVariable();
|
||||
if (existingVar != null && VariableAccessUtils.variableIsAssigned(existingVar)) return null;
|
||||
}
|
||||
return candidate;
|
||||
}
|
||||
|
||||
private static boolean containsBreakOrContinue(PsiDoWhileStatement doWhileLoop) {
|
||||
@@ -238,7 +246,6 @@ public class SimplifyBooleanExpressionFix extends PsiUpdateModCommandAction<PsiE
|
||||
Objects.requireNonNull(newPattern.getCheckType()).replace(checkType);
|
||||
PsiPatternVariable newVariable = (PsiPatternVariable)Objects.requireNonNull(newPattern.getPatternVariable()).replace(variable);
|
||||
variable.delete();
|
||||
target.replace(updated);
|
||||
String name = new VariableNameGenerator(target, VariableKind.LOCAL_VARIABLE).byName(variable.getName())
|
||||
.generate(true);
|
||||
if (!name.equals(newVariable.getName())) {
|
||||
@@ -249,6 +256,7 @@ public class SimplifyBooleanExpressionFix extends PsiUpdateModCommandAction<PsiE
|
||||
}
|
||||
}
|
||||
}
|
||||
target.replace(updated);
|
||||
}
|
||||
|
||||
private PsiExpression ensureCodeBlock(@NotNull Project project, PsiExpression subExpression) {
|
||||
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
// "Simplify 'obj instanceof String str' to true extracting side effects" "true"
|
||||
class Test {
|
||||
void test(Object obj) {
|
||||
if (obj instanceof String s) {
|
||||
s = s.trim();
|
||||
String str = (String) obj;
|
||||
if (str.isEmpty()) {
|
||||
|
||||
}
|
||||
System.out.println(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
// "Simplify 'obj instanceof String str' to true extracting side effects" "true"
|
||||
class Test {
|
||||
void test(Object obj) {
|
||||
if (obj instanceof String s) {
|
||||
s = s.trim();
|
||||
if (obj instanceof<caret> String str && str.isEmpty()) {
|
||||
|
||||
}
|
||||
System.out.println(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user