diff --git a/java/java-analysis-impl/resources/messages/InspectionGadgetsBundle.properties b/java/java-analysis-impl/resources/messages/InspectionGadgetsBundle.properties index 47a14f7ca03a..acd7b1aa78e1 100644 --- a/java/java-analysis-impl/resources/messages/InspectionGadgetsBundle.properties +++ b/java/java-analysis-impl/resources/messages/InspectionGadgetsBundle.properties @@ -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=new #ref()#ref in loop #loc string.concatenation.inside.string.buffer.append.problem.descriptor=String concatenation as argument to {0}.#ref() call #loc boolean.constructor.problem.descriptor=Boolean constructor call #loc -string.buffer.to.string.in.concatenation.problem.descriptor=Call to {0}.#ref() in concatenation #loc tail.recursion.problem.descriptor=Tail recursive call #ref() #loc string.equals.empty.string.problem.descriptor=#ref("") can be replaced with 'length()==0' #loc string.equals.empty.string.is.empty.problem.descriptor=#ref("") can be replaced with 'isEmpty()' #loc diff --git a/java/java-analysis-impl/src/com/siyeh/ig/performance/StringBufferToStringInConcatenationInspection.java b/java/java-analysis-impl/src/com/siyeh/ig/performance/StringBufferToStringInConcatenationInspection.java deleted file mode 100644 index f13d8bbada34..000000000000 --- a/java/java-analysis-impl/src/com/siyeh/ig/performance/StringBufferToStringInConcatenationInspection.java +++ /dev/null @@ -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()); - } - } -} \ No newline at end of file diff --git a/java/java-impl/src/META-INF/InspectionGadgets.xml b/java/java-impl/src/META-INF/InspectionGadgets.xml index cd4e3fb4679c..964fcccfcfc2 100644 --- a/java/java-impl/src/META-INF/InspectionGadgets.xml +++ b/java/java-impl/src/META-INF/InspectionGadgets.xml @@ -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"/> - - -Reports StringBuffer.toString() or StringBuilder.toString() 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. - - - \ No newline at end of file diff --git a/java/java-tests/testData/ig/com/siyeh/igtest/performance/string_buffer_to_string_in_concatenation/StringBufferToStringInConcatenation.java b/java/java-tests/testData/ig/com/siyeh/igtest/performance/string_buffer_to_string_in_concatenation/StringBufferToStringInConcatenation.java deleted file mode 100644 index 8274e42379de..000000000000 --- a/java/java-tests/testData/ig/com/siyeh/igtest/performance/string_buffer_to_string_in_concatenation/StringBufferToStringInConcatenation.java +++ /dev/null @@ -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.toString(); - String in = 3 + buffer.toString(); - System.out.println("out = " + out); - } - - public void bar() { - final StringBuilder builder = new StringBuilder(); - String one = "bar" + builder.toString(); - String two = 6 + builder.toString(); - String three = builder.toString() + " "; - String s = 1 + 2 + "as df" + builder.toString() + 1 + "asdf"; - } -} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/siyeh/ig/performance/StringBufferToStringInConcatenationInspectionTest.java b/java/java-tests/testSrc/com/siyeh/ig/performance/StringBufferToStringInConcatenationInspectionTest.java deleted file mode 100644 index f8994c4fe3d4..000000000000 --- a/java/java-tests/testSrc/com/siyeh/ig/performance/StringBufferToStringInConcatenationInspectionTest.java +++ /dev/null @@ -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(); - } -} \ No newline at end of file