IDEA-160280 ("Remove boxing" quickfix produces incorrect code)

This commit is contained in:
Bas Leijdekkers
2016-08-31 18:06:25 +02:00
parent a6df27ded9
commit 196b341f42
2 changed files with 27 additions and 9 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2015 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2016 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -86,10 +86,6 @@ public class UnnecessaryBoxingInspection extends BaseInspection {
@Override
public void doFix(@NotNull Project project, ProblemDescriptor descriptor) throws IncorrectOperationException {
final PsiCallExpression expression = (PsiCallExpression)descriptor.getPsiElement();
final PsiType boxedType = expression.getType();
if (boxedType == null) {
return;
}
final PsiExpressionList argumentList = expression.getArgumentList();
if (argumentList == null) {
return;
@@ -110,7 +106,7 @@ public class UnnecessaryBoxingInspection extends BaseInspection {
return;
}
}
final String replacementText = getUnboxedExpressionText(unboxedExpression, boxedType);
final String replacementText = getUnboxedExpressionText(unboxedExpression, expression);
if (replacementText == null) {
return;
}
@@ -118,7 +114,11 @@ public class UnnecessaryBoxingInspection extends BaseInspection {
}
@Nullable
private static String getUnboxedExpressionText(@NotNull PsiExpression unboxedExpression, @NotNull PsiType boxedType) {
private static String getUnboxedExpressionText(@NotNull PsiExpression unboxedExpression, @NotNull PsiExpression boxedExpression) {
final PsiType boxedType = boxedExpression.getType();
if (boxedType == null) {
return null;
}
final PsiType expressionType = unboxedExpression.getType();
if (expressionType == null) {
return null;
@@ -129,7 +129,13 @@ public class UnnecessaryBoxingInspection extends BaseInspection {
}
final String text = unboxedExpression.getText();
if (expressionType.equals(unboxedType)) {
return text;
final PsiElement parent = boxedExpression.getParent();
if (parent instanceof PsiExpression && ParenthesesUtils.areParenthesesNeeded(unboxedExpression, (PsiExpression) parent, false)) {
return '(' + text + ')';
}
else {
return text;
}
}
if (unboxedExpression instanceof PsiLiteralExpression) {
if (unboxedType.equals(PsiType.LONG) && expressionType.equals(PsiType.INT)) {
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -62,6 +62,18 @@ public class UnnecessaryBoxingFixTest extends IGQuickFixesTestCase {
"final Boolean aBoolean = Boolean.TRUE;");
}
public void testStringConcatenation() {
doExpressionTest(InspectionGadgetsBundle.message("unnecessary.boxing.remove.quickfix"),
"\"a\" + Long/**/.valueOf(1L + 2L) + \"b\"",
"\"a\" + (1L + 2L) + \"b\"");
}
public void testStringConcatenation2() {
doExpressionTest(InspectionGadgetsBundle.message("unnecessary.boxing.remove.quickfix"),
"\"a\" + Long/**/.valueOf(1L - 2L) + \"b\"",
"\"a\" + (1L - 2L) + \"b\"");
}
public void testCast() {
doFixTest();
}