IDEA-198546 "Replace cast with variable" inserts unresolved reference

This commit is contained in:
Tagir Valeev
2018-09-12 16:10:45 +07:00
parent 831b7e44b1
commit e890bb870b
2 changed files with 16 additions and 1 deletions

View File

@@ -94,6 +94,7 @@ public class ReplaceCastWithVariableAction extends PsiElementBaseIntentionAction
.filter(PsiTypeCastExpression.class)
.filter(cast -> EquivalenceChecker.getCanonicalPsiEquivalence().expressionsAreEquivalent(cast.getOperand(), operand))
.toList();
PsiResolveHelper resolveHelper = PsiResolveHelper.SERVICE.getInstance(method.getProject());
for (PsiTypeCastExpression occurrence : found) {
ProgressIndicatorProvider.checkCanceled();
final TextRange occurrenceTextRange = occurrence.getTextRange();
@@ -105,7 +106,9 @@ public class ReplaceCastWithVariableAction extends PsiElementBaseIntentionAction
final PsiCodeBlock methodBody = method.getBody();
if (variable != null && methodBody != null &&
!isChangedBetween(castedVar, methodBody, occurrence, expression) && !isChangedBetween(variable, methodBody, occurrence, expression)) {
variable.getName() != null && resolveHelper.resolveReferencedVariable(variable.getName(), expression) == variable &&
!isChangedBetween(castedVar, methodBody, occurrence, expression) &&
!isChangedBetween(variable, methodBody, occurrence, expression)) {
return variable;
}
}

View File

@@ -0,0 +1,12 @@
// "Replace '(String)o' with 's'" "false"
class C {
void foo(Object o) {
if (o instanceof String) {
String s = (String)o;
}
if (o instanceof String) {
String t = (Stri<caret>ng)o;
}
}
}