IDEA-91439 (StringBuffer replacement with String is wrong when char[] is involved)

This commit is contained in:
Bas Leijdekkers
2012-09-12 16:59:14 +02:00
parent fc18fca21d
commit b91a8e6d6b
4 changed files with 15 additions and 13 deletions
@@ -180,9 +180,11 @@ public class StringBufferReplaceableByStringInspection extends BaseInspection {
result.append('(').append(argument.getText()).append(')');
}
else {
result.append(argument.getText());
if (type != null && !type.equalsToText(CommonClassNames.JAVA_LANG_STRING)) {
result.append(".toString()");
result.append("String.valueOf(").append(argument.getText()).append(")");
}
else {
result.append(argument.getText());
}
}
}
@@ -280,7 +282,7 @@ public class StringBufferReplaceableByStringInspection extends BaseInspection {
public static boolean isAppend(PsiMethodCallExpression methodCallExpression) {
final PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression();
final String methodName = methodExpression.getReferenceName();
@NonNls final String methodName = methodExpression.getReferenceName();
return "append".equals(methodName);
}
}
@@ -299,7 +301,7 @@ public class StringBufferReplaceableByStringInspection extends BaseInspection {
if (!(grandParent instanceof PsiMethodCallExpression)) {
break;
}
final String name = referenceExpression.getReferenceName();
@NonNls final String name = referenceExpression.getReferenceName();
if ("append".equals(name)) {
final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)grandParent;
final PsiExpressionList argumentList = methodCallExpression.getArgumentList();
@@ -2,7 +2,7 @@ package com.siyeh.igfixes.style.replace_with_string;
class NonString2 {
String foo(Object o) {
return o.toString();
String foo(char[] o) {
return String.valueOf(o);
}
}
@@ -2,7 +2,7 @@ package com.siyeh.igfixes.style.replace_with_string;
class NonString2 {
String foo(Object o) {
String foo(char[] o) {
return new StringBuilder<caret>().append(o).toString();
}
}
@@ -15,13 +15,13 @@ public class StringBufferReplaceableWithStringFixTest extends IGQuickFixesTestCa
}
public void testSimpleStringBuffer() { doTest(); }
public void testStringBuilderAppend() { doTest("StringBuilderAppend", InspectionGadgetsBundle.message("string.builder.replaceable.by.string.quickfix")); }
public void testStringBuilderAppend() { doTest(InspectionGadgetsBundle.message("string.builder.replaceable.by.string.quickfix")); }
public void testStringBufferVariable() { doTest(); }
public void testStringBufferVariable2() { doTest(); }
public void testStartsWithPrimitive() { doTest(); }
public void testPrecedence() { doTest("Precedence", InspectionGadgetsBundle.message("string.builder.replaceable.by.string.quickfix")); }
public void testPrecedence2() { doTest("Precedence2", InspectionGadgetsBundle.message("string.builder.replaceable.by.string.quickfix")); }
public void testPrecedence3() { doTest("Precedence3", InspectionGadgetsBundle.message("string.builder.replaceable.by.string.quickfix")); }
public void testNonString1() { doTest("Precedence3", InspectionGadgetsBundle.message("string.builder.replaceable.by.string.quickfix")); }
public void testNonString2() { doTest("Precedence3", InspectionGadgetsBundle.message("string.builder.replaceable.by.string.quickfix")); }
public void testPrecedence() { doTest(InspectionGadgetsBundle.message("string.builder.replaceable.by.string.quickfix")); }
public void testPrecedence2() { doTest(InspectionGadgetsBundle.message("string.builder.replaceable.by.string.quickfix")); }
public void testPrecedence3() { doTest(InspectionGadgetsBundle.message("string.builder.replaceable.by.string.quickfix")); }
public void testNonString1() { doTest(InspectionGadgetsBundle.message("string.builder.replaceable.by.string.quickfix")); }
public void testNonString2() { doTest(InspectionGadgetsBundle.message("string.builder.replaceable.by.string.quickfix")); }
}