mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
StringConcatenationInLoop: removed m_ignoreUnlessAssigned (always true now) (IDEA-CR-16590)
This commit is contained in:
+10
-23
@@ -16,7 +16,6 @@
|
||||
package com.siyeh.ig.performance;
|
||||
|
||||
import com.intellij.codeInspection.ProblemDescriptor;
|
||||
import com.intellij.codeInspection.ui.SingleCheckboxOptionsPanel;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.CodeStyleManager;
|
||||
@@ -39,7 +38,6 @@ import org.jetbrains.annotations.Nls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
@@ -48,9 +46,6 @@ import java.util.regex.Pattern;
|
||||
|
||||
public class StringConcatenationInLoopsInspection extends BaseInspection {
|
||||
|
||||
@SuppressWarnings("PublicField")
|
||||
public boolean m_ignoreUnlessAssigned = true;
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String getDisplayName() {
|
||||
@@ -63,18 +58,12 @@ public class StringConcatenationInLoopsInspection extends BaseInspection {
|
||||
return InspectionGadgetsBundle.message("string.concatenation.in.loops.problem.descriptor");
|
||||
}
|
||||
|
||||
@Override
|
||||
public JComponent createOptionsPanel() {
|
||||
return new SingleCheckboxOptionsPanel(InspectionGadgetsBundle.message("string.concatenation.in.loops.only.option"),
|
||||
this, "m_ignoreUnlessAssigned");
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseInspectionVisitor buildVisitor() {
|
||||
return new StringConcatenationInLoopsVisitor();
|
||||
}
|
||||
|
||||
private class StringConcatenationInLoopsVisitor extends BaseInspectionVisitor {
|
||||
private static class StringConcatenationInLoopsVisitor extends BaseInspectionVisitor {
|
||||
|
||||
@Override
|
||||
public void visitPolyadicExpression(PsiPolyadicExpression expression) {
|
||||
@@ -90,7 +79,7 @@ public class StringConcatenationInLoopsInspection extends BaseInspection {
|
||||
|
||||
if (ExpressionUtils.isEvaluatedAtCompileTime(expression)) return;
|
||||
|
||||
if (m_ignoreUnlessAssigned && !isAppendedRepeatedly(expression)) return;
|
||||
if (!isAppendedRepeatedly(expression)) return;
|
||||
final PsiJavaToken sign = expression.getTokenBeforeOperand(operands[1]);
|
||||
assert sign != null;
|
||||
registerError(sign, getAppendedVariable(expression));
|
||||
@@ -110,11 +99,9 @@ public class StringConcatenationInLoopsInspection extends BaseInspection {
|
||||
|
||||
if (!checkExpression(expression, lhs.getType())) return;
|
||||
|
||||
if (m_ignoreUnlessAssigned) {
|
||||
lhs = PsiUtil.skipParenthesizedExprDown(lhs);
|
||||
if (!(lhs instanceof PsiReferenceExpression)) {
|
||||
return;
|
||||
}
|
||||
lhs = PsiUtil.skipParenthesizedExprDown(lhs);
|
||||
if (!(lhs instanceof PsiReferenceExpression)) {
|
||||
return;
|
||||
}
|
||||
registerError(sign, getAppendedVariable(expression));
|
||||
}
|
||||
@@ -146,7 +133,7 @@ public class StringConcatenationInLoopsInspection extends BaseInspection {
|
||||
}
|
||||
|
||||
@Contract("null, _ -> false")
|
||||
private boolean flowBreaksLoop(PsiStatement statement, PsiLoopStatement loop) {
|
||||
private static boolean flowBreaksLoop(PsiStatement statement, PsiLoopStatement loop) {
|
||||
if(statement == null || statement == loop) return false;
|
||||
for(PsiStatement sibling = statement; sibling != null; sibling = PsiTreeUtil.getNextSiblingOfType(sibling, PsiStatement.class)) {
|
||||
if(sibling instanceof PsiContinueStatement) return false;
|
||||
@@ -203,7 +190,7 @@ public class StringConcatenationInLoopsInspection extends BaseInspection {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private PsiCodeBlock getSurroundingBlock(PsiElement expression) {
|
||||
private static PsiCodeBlock getSurroundingBlock(PsiElement expression) {
|
||||
PsiElement parent = PsiTreeUtil.getParentOfType(expression, PsiMethod.class, PsiClassInitializer.class, PsiLambdaExpression.class);
|
||||
if(parent instanceof PsiMethod) {
|
||||
return ((PsiMethod)parent).getBody();
|
||||
@@ -218,7 +205,7 @@ public class StringConcatenationInLoopsInspection extends BaseInspection {
|
||||
return null;
|
||||
}
|
||||
|
||||
private boolean containingStatementExits(PsiElement element) {
|
||||
private static boolean containingStatementExits(PsiElement element) {
|
||||
final PsiStatement newExpressionStatement = PsiTreeUtil.getParentOfType(element, PsiStatement.class);
|
||||
if (newExpressionStatement == null) {
|
||||
return false;
|
||||
@@ -227,7 +214,7 @@ public class StringConcatenationInLoopsInspection extends BaseInspection {
|
||||
return !ControlFlowUtils.statementMayCompleteNormally(parentStatement);
|
||||
}
|
||||
|
||||
private boolean isAppendedRepeatedly(PsiExpression expression) {
|
||||
private static boolean isAppendedRepeatedly(PsiExpression expression) {
|
||||
PsiElement parent = expression.getParent();
|
||||
while (parent instanceof PsiParenthesizedExpression || parent instanceof PsiPolyadicExpression) {
|
||||
parent = parent.getParent();
|
||||
@@ -253,7 +240,7 @@ public class StringConcatenationInLoopsInspection extends BaseInspection {
|
||||
return isAppended(variable, rhs);
|
||||
}
|
||||
|
||||
private boolean isAppended(PsiVariable variable, PsiExpression expression) {
|
||||
private static boolean isAppended(PsiVariable variable, PsiExpression expression) {
|
||||
expression = PsiUtil.skipParenthesizedExprDown(expression);
|
||||
if(expression instanceof PsiPolyadicExpression) {
|
||||
for(PsiExpression operand : ((PsiPolyadicExpression)expression).getOperands()) {
|
||||
|
||||
+2
-2
@@ -37,7 +37,7 @@ public class StringConcatenationInLoop
|
||||
}
|
||||
for(int i = 0; i < 5; i++)
|
||||
{
|
||||
baz( foo <warning descr="String concatenation '+' in loop">+</warning> " " + i);
|
||||
baz( foo + " " + i);
|
||||
}
|
||||
for(int i = 0; i < 5; i++)
|
||||
{
|
||||
@@ -162,7 +162,7 @@ public class StringConcatenationInLoop
|
||||
String s = "asdf";
|
||||
final int len = array.length;
|
||||
for (int k = 0; k < len; k++) {
|
||||
array[k] <warning descr="String concatenation '+=' in loop">+=</warning> "b";
|
||||
array[k] += "b";
|
||||
s <warning descr="String concatenation '+=' in loop">+=</warning> k;
|
||||
}
|
||||
}
|
||||
|
||||
+16
-3
@@ -1,3 +1,18 @@
|
||||
/*
|
||||
* Copyright 2000-2016 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;
|
||||
@@ -13,8 +28,6 @@ public class StringConcatenationInLoopsInspectionTest extends LightInspectionTes
|
||||
@Nullable
|
||||
@Override
|
||||
protected InspectionProfileEntry getInspection() {
|
||||
final StringConcatenationInLoopsInspection inspection = new StringConcatenationInLoopsInspection();
|
||||
inspection.m_ignoreUnlessAssigned = false;
|
||||
return inspection;
|
||||
return new StringConcatenationInLoopsInspection();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user