mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IG: teach "Non-constant String should be StringBuilder" inspection about polyadic expressions
This commit is contained in:
+8
-9
@@ -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;
|
||||
}
|
||||
|
||||
-23
@@ -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);
|
||||
}
|
||||
}
|
||||
+41
@@ -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);
|
||||
}
|
||||
}
|
||||
+36
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user