[Java] remove "'StringBuilder.toString()' in concatenation" inspection (IDEA-349211)

GitOrigin-RevId: 067a8544b08d392bb8f758fd53acd974355c298e
This commit is contained in:
Bas Leijdekkers
2024-03-18 00:08:43 +00:00
committed by intellij-monorepo-bot
parent aca900dbfd
commit 842f3972ac
6 changed files with 0 additions and 177 deletions
@@ -698,7 +698,6 @@ volatile.array.field.display.name=Volatile array field
literal.as.arg.to.string.equals.display.name=String literal may be 'equals()' qualifier
inner.class.may.be.static.display.name=Inner class may be 'static'
redundant.field.initialization.display.name=Redundant field initialization
string.buffer.to.string.in.concatenation.display.name='StringBuilder.toString()' in concatenation
utility.class.with.public.constructor.display.name=Utility class with 'public' constructor
for.loop.replaceable.by.while.display.name='for' loop may be replaced by 'while' loop
missing.deprecated.annotation.display.name=Missing '@Deprecated' annotation
@@ -1052,7 +1051,6 @@ collections.must.have.initial.capacity.problem.descriptor=<code>new #ref()</code
string.concatenation.in.loops.problem.descriptor=String concatenation <code>#ref</code> in loop #loc
string.concatenation.inside.string.buffer.append.problem.descriptor=String concatenation as argument to <code>{0}.#ref()</code> call #loc
boolean.constructor.problem.descriptor=Boolean constructor call #loc
string.buffer.to.string.in.concatenation.problem.descriptor=Call to <code>{0}.#ref()</code> in concatenation #loc
tail.recursion.problem.descriptor=Tail recursive call <code>#ref()</code> #loc
string.equals.empty.string.problem.descriptor=<code>#ref("")</code> can be replaced with 'length()==0' #loc
string.equals.empty.string.is.empty.problem.descriptor=<code>#ref("")</code> can be replaced with 'isEmpty()' #loc
@@ -1,107 +0,0 @@
/*
* Copyright 2003-2018 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.performance;
import com.intellij.codeInspection.CommonQuickFixBundle;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.modcommand.ModPsiUpdater;
import com.intellij.modcommand.PsiUpdateModCommandQuickFix;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.siyeh.HardcodedMethodConstants;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.PsiReplacementUtil;
import com.siyeh.ig.psiutils.CommentTracker;
import com.siyeh.ig.psiutils.ExpressionUtils;
import org.jetbrains.annotations.NotNull;
public final class StringBufferToStringInConcatenationInspection extends BaseInspection {
@Override
@NotNull
protected String buildErrorString(Object... infos) {
final String className = (String)infos[0];
return InspectionGadgetsBundle.message("string.buffer.to.string.in.concatenation.problem.descriptor", className);
}
@Override
public BaseInspectionVisitor buildVisitor() {
return new StringBufferToStringVisitor();
}
@Override
public LocalQuickFix buildFix(Object... infos) {
return new StringBufferToStringFix();
}
private static class StringBufferToStringFix extends PsiUpdateModCommandQuickFix {
@Override
@NotNull
public String getFamilyName() {
return CommonQuickFixBundle.message("fix.remove", "toString()");
}
@Override
protected void applyFix(@NotNull Project project, @NotNull PsiElement methodNameToken, @NotNull ModPsiUpdater updater) {
final PsiElement methodCallExpression = methodNameToken.getParent();
assert methodCallExpression != null;
final PsiMethodCallExpression methodCall = (PsiMethodCallExpression)methodCallExpression.getParent();
assert methodCall != null;
final PsiReferenceExpression expression = methodCall.getMethodExpression();
final PsiExpression qualifier = expression.getQualifierExpression();
assert qualifier != null;
CommentTracker commentTracker = new CommentTracker();
final String newExpression = commentTracker.text(qualifier);
PsiReplacementUtil.replaceExpression(methodCall, newExpression, commentTracker);
}
}
private static class StringBufferToStringVisitor extends BaseInspectionVisitor {
@Override
public void visitMethodCallExpression(@NotNull PsiMethodCallExpression expression) {
super.visitMethodCallExpression(expression);
if (!ExpressionUtils.isStringConcatenationOperand(expression)) {
return;
}
final PsiReferenceExpression methodExpression = expression.getMethodExpression();
final String referenceName = methodExpression.getReferenceName();
if (!HardcodedMethodConstants.TO_STRING.equals(referenceName)) {
return;
}
final PsiMethod method = expression.resolveMethod();
if (method == null) {
return;
}
final PsiParameterList parameterList = method.getParameterList();
if (!parameterList.isEmpty()) {
return;
}
final PsiClass aClass = method.getContainingClass();
if (aClass == null) {
return;
}
final String className = aClass.getQualifiedName();
if (!CommonClassNames.JAVA_LANG_STRING_BUFFER.equals(className) && !CommonClassNames.JAVA_LANG_STRING_BUILDER.equals(className)) {
return;
}
registerMethodCallError(expression, aClass.getName());
}
}
}
@@ -1883,10 +1883,6 @@
bundle="messages.InspectionGadgetsBundle" key="string.buffer.must.have.initial.capacity.display.name"
groupBundle="messages.InspectionsBundle" groupKey="group.names.performance.issues" enabledByDefault="false"
level="WARNING" implementationClass="com.siyeh.ig.performance.StringBufferMustHaveInitialCapacityInspection"/>
<localInspection groupPath="Java" language="JAVA" shortName="StringBufferToStringInConcatenation" bundle="messages.InspectionGadgetsBundle"
key="string.buffer.to.string.in.concatenation.display.name" groupBundle="messages.InspectionsBundle"
groupKey="group.names.performance.issues" enabledByDefault="false" level="WARNING"
implementationClass="com.siyeh.ig.performance.StringBufferToStringInConcatenationInspection"/>
<localInspection groupPath="Java" language="JAVA" suppressId="StringConcatenationInLoop" alternativeId="StringContatenationInLoop" shortName="StringConcatenationInLoops"
bundle="messages.InspectionGadgetsBundle" key="string.concatenation.in.loops.display.name"
groupBundle="messages.InspectionsBundle" groupKey="group.names.performance.issues" enabledByDefault="true"
@@ -1,8 +0,0 @@
<html>
<body>
Reports <code>StringBuffer.toString()</code> or <code>StringBuilder.toString()</code> calls in string concatenations.
Such calls are unnecessary when concatenating and can be removed, saving a method call and an object allocation,
which may improve performance.
<!-- tooltip end -->
</body>
</html>
@@ -1,23 +0,0 @@
package com.siyeh.igtest.performance.string_buffer_to_string_in_concatenation;
import java.io.IOException;
import java.util.*;
public class StringBufferToStringInConcatenation
{
public void foo() {
final StringBuffer buffer = new StringBuffer(3);
String out = "foo" + buffer.<warning descr="Call to 'StringBuffer.toString()' in concatenation">toString</warning>();
String in = 3 + buffer.toString();
System.out.println("out = " + out);
}
public void bar() {
final StringBuilder builder = new StringBuilder();
String one = "bar" + builder.<warning descr="Call to 'StringBuilder.toString()' in concatenation">toString</warning>();
String two = 6 + builder.toString();
String three = builder.<warning descr="Call to 'StringBuilder.toString()' in concatenation">toString</warning>() + " ";
String s = 1 + 2 + "as df" + builder.<warning descr="Call to 'StringBuilder.toString()' in concatenation">toString</warning>() + 1 + "asdf";
}
}
@@ -1,33 +0,0 @@
/*
* Copyright 2000-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.performance;
import com.intellij.codeInspection.InspectionProfileEntry;
import com.siyeh.ig.LightJavaInspectionTestCase;
import org.jetbrains.annotations.Nullable;
public class StringBufferToStringInConcatenationInspectionTest extends LightJavaInspectionTestCase {
public void testStringBufferToStringInConcatenation() {
doTest();
}
@Nullable
@Override
protected InspectionProfileEntry getInspection() {
return new StringBufferToStringInConcatenationInspection();
}
}