IDEA-85620 (Inspection "'StringBuffer' can be replaced with 'String'" produces invalid code.)

This commit is contained in:
Bas Leijdekkers
2012-05-07 10:56:53 +02:00
parent cdd823f7fb
commit 2ca03e447b
4 changed files with 19 additions and 2 deletions
@@ -154,7 +154,7 @@ public class StringBufferReplaceableByStringInspection extends BaseInspection {
final PsiExpression argument = arguments[0];
if (result.length() != 0) {
result.append('+');
if (ParenthesesUtils.getPrecedence(argument) > ParenthesesUtils.ADDITIVE_PRECEDENCE) {
if (ParenthesesUtils.getPrecedence(argument) >= ParenthesesUtils.ADDITIVE_PRECEDENCE) {
result.append('(').append(argument.getText()).append(')');
}
else {
@@ -167,7 +167,7 @@ public class StringBufferReplaceableByStringInspection extends BaseInspection {
result.append("String.valueOf(").append(argument.getText()).append(")");
}
else {
if (ParenthesesUtils.getPrecedence(argument) > ParenthesesUtils.ADDITIVE_PRECEDENCE) {
if (ParenthesesUtils.getPrecedence(argument) >= ParenthesesUtils.ADDITIVE_PRECEDENCE) {
result.append('(').append(argument.getText()).append(')');
}
else {
@@ -0,0 +1,8 @@
package com.siyeh.igfixes.style.replace_with_string;
class Precedence2 {
void foo() {
long start = 0, end = 0;
String string = "Time: " + (end - start) + ".";
}
}
@@ -0,0 +1,8 @@
package com.siyeh.igfixes.style.replace_with_string;
class Precedence2 {
void foo() {
long start = 0, end = 0;
String string = new StringB<caret>uilder().append("Time: ").append(end - start).append(".").toString();
}
}
@@ -35,4 +35,5 @@ public class StringBufferReplaceableWithStringFixTest extends IGQuickFixesTestCa
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")); }
}