unnecessary local: don't raise for inherited targets (IDEA-145135)

This commit is contained in:
Anna Kozlova
2015-09-16 18:53:34 +03:00
parent f61de7a241
commit d4ab53ef53
2 changed files with 46 additions and 1 deletions
@@ -169,6 +169,9 @@ public class UnnecessaryLocalVariableInspectionBase extends BaseInspection {
!HighlightControlFlowUtil.isEffectivelyFinal(initialization, containingScope, null) &&
HighlightControlFlowUtil.isEffectivelyFinal(variable, containingScope, null);
final PsiType variableType = variable.getType();
final PsiType initializationType = initialization.getType();
final boolean sameType = Comparing.equal(variableType, initializationType);
for (PsiReference ref : ReferencesSearch.search(variable, new LocalSearchScope(containingScope))) {
final PsiElement refElement = ref.getElement();
if (finalVariableIntroduction) {
@@ -181,9 +184,20 @@ public class UnnecessaryLocalVariableInspectionBase extends BaseInspection {
if (resolveHelper.resolveReferencedVariable(initializationName, refElement) != initialization) {
return false;
}
if (!sameType) {
final PsiElement parent = refElement.getParent();
if (parent instanceof PsiReferenceExpression) {
final PsiElement resolve = ((PsiReferenceExpression)parent).resolve();
if (resolve instanceof PsiMember &&
((PsiMember)resolve).hasModifierProperty(PsiModifier.PRIVATE)) {
return false;
}
}
}
}
return !TypeConversionUtil.boxingConversionApplicable(variable.getType(), initialization.getType());
return !TypeConversionUtil.boxingConversionApplicable(variableType, initializationType);
}
private boolean isImmediatelyReturned(PsiVariable variable) {
@@ -154,4 +154,35 @@ class C {
}
};
}
abstract class AbstractSchedulingElement<T extends AbstractSchedulingElement<T>> {
private String scenarioSettings;
public String scenarioSettings1;
public T copyInto(T target) {
AbstractSchedulingElement<T> targetAsAbstractSchedulingElement = target;
targetAsAbstractSchedulingElement.scenarioSettings = scenarioSettings;
return target;
}
public T copyInto1(T target) {
AbstractSchedulingElement<T> <warning descr="Local variable 'targetAsAbstractSchedulingElement' is redundant">targetAsAbstractSchedulingElement</warning> = target;
targetAsAbstractSchedulingElement.scenarioSettings1 = scenarioSettings1;
return target;
}
public SchedulingElement copyInto2(SchedulingElement target) {
AbstractSchedulingElement<SchedulingElement> targetAsAbstractSchedulingElement = target;
targetAsAbstractSchedulingElement.scenarioSettings = scenarioSettings;
return target;
}
public SchedulingElement copyInto3(SchedulingElement target) {
AbstractSchedulingElement<SchedulingElement> <warning descr="Local variable 'targetAsAbstractSchedulingElement' is redundant">targetAsAbstractSchedulingElement</warning> = target;
targetAsAbstractSchedulingElement.scenarioSettings1 = scenarioSettings1;
return target;
}
}
class SchedulingElement extends AbstractSchedulingElement<SchedulingElement>{}
}