mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
PatternVariableCanBeUsedInspection: do not warn if there's a conflicting variable name (IDEA-CR-57035)
GitOrigin-RevId: cc1ea92dbabe741f1a0c9c7577912a42ac520957
This commit is contained in:
committed by
intellij-monorepo-bot
parent
af785cc1ce
commit
f03104bcd1
+23
@@ -101,11 +101,34 @@ public class PatternVariableCanBeUsedInspection extends AbstractBaseJavaLocalIns
|
||||
}
|
||||
}
|
||||
}
|
||||
if (isConflictingNameDeclaredInside(identifier, stmt)) return true;
|
||||
if (stmt instanceof PsiSwitchLabelStatementBase) break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean isConflictingNameDeclaredInside(PsiIdentifier identifier, PsiElement statement) {
|
||||
class Visitor extends JavaRecursiveElementWalkingVisitor {
|
||||
boolean hasConflict = false;
|
||||
|
||||
@Override
|
||||
public void visitClass(final PsiClass aClass) {}
|
||||
|
||||
@Override
|
||||
public void visitVariable(PsiVariable variable) {
|
||||
String name = variable.getName();
|
||||
if (name != null && identifier.textMatches(name)) {
|
||||
hasConflict = true;
|
||||
stopWalking();
|
||||
}
|
||||
super.visitVariable(variable);
|
||||
}
|
||||
}
|
||||
Visitor visitor = new Visitor();
|
||||
statement.accept(visitor);
|
||||
return visitor.hasConflict;
|
||||
}
|
||||
|
||||
private boolean canCompleteNormally(@NotNull PsiElement parent, @Nullable PsiStatement statement) {
|
||||
if (statement == null) return true;
|
||||
ControlFlow flow;
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
// "Replace 'n' with pattern variable" "false"
|
||||
class X {
|
||||
void test(Object obj) {
|
||||
if (!(obj instanceof Number)) return;
|
||||
if (false) {
|
||||
Number n;
|
||||
}
|
||||
Number <caret>n = (Number)obj;
|
||||
System.out.println(n.longValue());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user