UnnecessaryBoxing: don't warn if inferred variable type will change (IDEA-193096)

This commit is contained in:
Tagir Valeev
2018-06-01 13:26:10 +07:00
parent 1f3cd67c4b
commit d73340243d
3 changed files with 19 additions and 0 deletions
@@ -250,6 +250,11 @@ public class UnnecessaryBoxingInspection extends BaseInspection {
parent instanceof PsiSynchronizedStatement) {
return true;
}
else if (parent instanceof PsiVariable) {
PsiTypeElement typeElement = ((PsiVariable)parent).getTypeElement();
// Inferred type may change if boxing is removed; if it's possible
if (typeElement != null && typeElement.isInferredType()) return true;
}
else if (parent instanceof PsiTypeCastExpression) {
final PsiTypeCastExpression castExpression = (PsiTypeCastExpression)parent;
return TypeUtils.isTypeParameter(castExpression.getType());
@@ -135,4 +135,11 @@ class WithLambdaUnfriendlyOverloads {
System.out.println("hello");
}
}
void testVar() {
var x = Integer.valueOf(5);
Integer y = <warning descr="Unnecessary boxing 'Integer.valueOf(5)'">Integer.valueOf(5)</warning>;
System.out.println(x.getClass());
System.out.println(y.getClass());
}
}
@@ -4,10 +4,17 @@
package com.siyeh.ig.migration;
import com.intellij.codeInspection.InspectionProfileEntry;
import com.intellij.testFramework.LightProjectDescriptor;
import com.siyeh.ig.LightInspectionTestCase;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class UnnecessaryBoxingInspectionTest extends LightInspectionTestCase {
@NotNull
@Override
protected LightProjectDescriptor getProjectDescriptor() {
return JAVA_10;
}
public void testUnnecessaryBoxing() {
doTest();