IG: teach "Non-constant String should be StringBuilder" inspection about polyadic expressions

This commit is contained in:
Bas Leijdekkers
2015-05-15 16:38:36 +02:00
parent 872c3acb4e
commit 623dc5cab2
4 changed files with 85 additions and 32 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2008 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2015 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.
@@ -81,16 +81,15 @@ class StringVariableIsAppendedToVisitor extends JavaRecursiveElementVisitor {
return variable.equals(referent);
}
if (expression instanceof PsiParenthesizedExpression) {
final PsiExpression body =
((PsiParenthesizedExpression)expression).getExpression();
final PsiParenthesizedExpression parenthesizedExpression = (PsiParenthesizedExpression)expression;
final PsiExpression body = parenthesizedExpression.getExpression();
return isConcatenation(body);
}
if (expression instanceof PsiBinaryExpression) {
final PsiBinaryExpression binaryExpression =
(PsiBinaryExpression)expression;
final PsiExpression lhs = binaryExpression.getLOperand();
final PsiExpression rhs = binaryExpression.getROperand();
return isConcatenation(lhs) || isConcatenation(rhs);
if (expression instanceof PsiPolyadicExpression) {
final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression)expression;
for (PsiExpression operand : polyadicExpression.getOperands()) {
if (isConcatenation(operand)) return true;
}
}
return false;
}
@@ -1,23 +0,0 @@
package com.siyeh.igtest.performance;
public class StringReplaceableByStringBufferInspection {
public void foo()
{
String buffer = "bar";
buffer += "foo";
System.out.println(buffer);
}
public void foobar()
{
String buffer = "bar";
buffer = buffer + "foo";
System.out.println(buffer);
}
public void foobaz()
{
final String buffer = "bar";
System.out.println(buffer);
}
}
@@ -0,0 +1,41 @@
package com.siyeh.igtest.performance.string_replaceable_by_string_buffer;
public class StringReplaceableByStringBuffer {
public void foo()
{
String buffer = "bar";
buffer += "foo";
System.out.println(buffer);
}
public void foo2()
{
String <warning descr="Non-constant 'String buffer' should probably be declared as ''StringBuilder''">buffer</warning> = "bar";
for (int i = 0; i < 10; i++) {
buffer += "foo";
}
System.out.println(buffer);
}
public void foobar()
{
String buffer = "bar";
buffer = buffer + "foo" + buffer;
System.out.println(buffer);
}
public void foobar2()
{
String <warning descr="Non-constant 'String buffer' should probably be declared as ''StringBuilder''">buffer</warning> = "bar";
for (int i = 0; i < 10; i++) {
buffer = buffer + "foo" + buffer;
}
System.out.println(buffer);
}
public void foobaz()
{
final String buffer = "bar";
System.out.println(buffer);
}
}
@@ -0,0 +1,36 @@
/*
* Copyright 2000-2015 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.LightInspectionTestCase;
import org.jetbrains.annotations.Nullable;
/**
* @author Bas Leijdekkers
*/
public class StringReplaceableByStringBufferInspectionTest extends LightInspectionTestCase {
public void testStringReplaceableByStringBuffer() {
doTest();
}
@Nullable
@Override
protected InspectionProfileEntry getInspection() {
return new StringReplaceableByStringBufferInspection();
}
}