StringBufferReplaceableByString: do not convert first operand to String if the second is String

Because implicit conversion will be applied anyway
Fixes IDEA-205019 Conversion StringBuilder -> String can be slightly improved
This commit is contained in:
Tagir Valeev
2019-01-11 11:06:09 +07:00
parent 4a677e3f67
commit ca88b468f9
12 changed files with 66 additions and 26 deletions
@@ -330,26 +330,31 @@ public class StringBufferReplaceableByStringInspection extends BaseInspection {
final PsiExpression argument = arguments[0];
final PsiType type = argument.getType();
final String argumentText = tracker.textWithComments(argument);
if (result.length() != 0) {
addNewlineIfNeeded(argument, true, result, commentsBefore);
boolean needConvertToString = result.length() == 0;
if (needConvertToString) {
PsiMethodCallExpression nextCall = ExpressionUtils.getCallForQualifier(methodCallExpression);
if (nextCall != null && "append".equals(nextCall.getMethodExpression().getReferenceName())) {
PsiExpression[] args = nextCall.getArgumentList().getExpressions();
needConvertToString = args.length != 1 || !TypeUtils.isJavaLangString(args[0].getType());
}
}
addNewlineIfNeeded(argument, result.length() > 0, result, commentsBefore);
if (!needConvertToString) {
if (ParenthesesUtils.getPrecedence(argument) > ParenthesesUtils.ADDITIVE_PRECEDENCE ||
(type instanceof PsiPrimitiveType && ParenthesesUtils.getPrecedence(argument) == ParenthesesUtils.ADDITIVE_PRECEDENCE)) {
result.append('(').append(argumentText).append(')');
}
else if (type instanceof PsiArrayType) {
result.append("String.valueOf(").append(argumentText).append(')');
}
else {
if (type instanceof PsiArrayType) {
result.append("String.valueOf(").append(argumentText).append(")");
}
else {
if (StringUtil.startsWithChar(argumentText, '+')) {
result.append(' ');
}
result.append(argumentText);
if (result.length() > 0 && StringUtil.startsWithChar(argumentText, '+')) {
result.append(' ');
}
result.append(argumentText);
}
}
else {
addNewlineIfNeeded(argumentList, false, result, commentsBefore);
if (type instanceof PsiPrimitiveType) {
if (argument instanceof PsiLiteralExpression) {
final PsiLiteralExpression literalExpression = (PsiLiteralExpression)argument;
@@ -369,18 +374,14 @@ public class StringBufferReplaceableByStringInspection extends BaseInspection {
result.append("String.valueOf(").append(argumentText).append(")");
}
}
else if (ParenthesesUtils.getPrecedence(argument) >= ParenthesesUtils.ADDITIVE_PRECEDENCE) {
result.append('(').append(argumentText).append(')');
}
else if (type != null && !type.equalsToText(CommonClassNames.JAVA_LANG_STRING)) {
result.append("String.valueOf(").append(argumentText).append(")");
}
else {
if (ParenthesesUtils.getPrecedence(argument) >= ParenthesesUtils.ADDITIVE_PRECEDENCE) {
result.append('(').append(argumentText).append(')');
}
else {
if (type != null && !type.equalsToText(CommonClassNames.JAVA_LANG_STRING)) {
result.append("String.valueOf(").append(argumentText).append(")");
}
else {
result.append(argumentText);
}
}
result.append(argumentText);
}
}
}
@@ -2,6 +2,6 @@ package com.siyeh.igfixes.style.replace_with_string;
class Escape {
{
String s = ("\"" + "bas");
String s = ("\"" + 0 + "bas");
}
}
@@ -2,6 +2,6 @@ package com.siyeh.igfixes.style.replace_with_string;
class Escape {
{
String s = (((((new <caret>StringBuilder()).append('"')).append("bas"))).toString());
String s = (((((new <caret>StringBuilder()).append('"')).append(0).append("bas"))).toString());
}
}
@@ -0,0 +1,6 @@
class StartsWithPrimitive2 {
String foo(String str) {
return 'L' + str + '<caret>;';
}
}
@@ -0,0 +1,6 @@
class StartsWithPrimitive2 {
String foo(String str) {
return new Strin<caret>gBuilder().append('L').append(str).append(';').toString();
}
}
@@ -0,0 +1,6 @@
class StartsWithPrimitive2 {
String foo(String str) {
return +1 + str + ';<caret>';
}
}
@@ -0,0 +1,6 @@
class StartsWithPrimitive2 {
String foo(String str) {
return new Strin<caret>gBuilder().append(+1).append(str).append(';').toString();
}
}
@@ -0,0 +1,6 @@
class StartsWithPrimitive2 {
String foo(String str) {
return String.va<caret>lueOf(+1) + 0 + str + ';';
}
}
@@ -0,0 +1,6 @@
class StartsWithPrimitive2 {
String foo(String str) {
return new Strin<caret>gBuilder().append(+1).append(0).append(str).append(';').toString();
}
}
@@ -2,6 +2,6 @@ package com.siyeh.igfixes.style.replace_with_string;
class Escape {
{
String s = "'" + "bas";
String s = "'" + 0 + "bas";
}
}
@@ -2,6 +2,6 @@ package com.siyeh.igfixes.style.replace_with_string;
class Escape {
{
String s = new StringBuilder<caret>().append('\'').append("bas").toString();
String s = new StringBuilder<caret>().append('\'').append(0).append("bas").toString();
}
}
@@ -33,6 +33,9 @@ public class StringBufferReplaceableByStringFixTest extends IGQuickFixesTestCase
public void testStringBufferVariable() { doTestFor("StringBuffer"); }
public void testStringBufferVariable2() { doTestFor("StringBuffer"); }
public void testStartsWithPrimitive() { doTestFor("StringBuffer"); }
public void testStartsWithPrimitive2() { doTestFor("StringBuilder"); }
public void testStartsWithPrimitive3() { doTestFor("StringBuilder"); }
public void testStartsWithPrimitive4() { doTestFor("StringBuilder"); }
public void testPrecedence() { doTestFor("StringBuilder"); }
public void testPrecedence2() { doTestFor("StringBuilder"); }
public void testPrecedence3() { doTestFor("StringBuilder"); }