LengthOneStringInIndexOf: unit-test, fix for parentheses, unescape "\"" -> '"'; cleanup warning message

This commit is contained in:
Tagir Valeev
2018-10-01 14:54:35 +07:00
parent e1e1ed9bef
commit 27b2efd106
4 changed files with 73 additions and 20 deletions
@@ -1196,6 +1196,7 @@ non.reproducible.math.call.replace.quickfix=Replace with 'StrictMath' call
overly.complex.arithmetic.expression.max.number.option=Maximum number of terms:
expression.can.be.replaced.problem.descriptor=<code>#ref</code> can be replaced with ''{0}'' #loc
method.complexity.limit.option=Method complexity limit:
expression.can.be.replaced.no.quotes.problem.descriptor={0} can be replaced with {1}
cyclomatic.complexity.problem.descriptor=Overly complex method <code>#ref()</code> (cyclomatic complexity = {0}) #loc
method.coupling.limit.option=Method coupling limit:
method.coupling.problem.descriptor=<code>#ref</code> is overly coupled (# referenced classes = {0}) #loc
@@ -17,8 +17,8 @@ package com.siyeh.ig.performance;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiUtil;
import com.siyeh.HardcodedMethodConstants;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
@@ -26,6 +26,7 @@ import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.InspectionGadgetsFix;
import com.siyeh.ig.PsiReplacementUtil;
import com.siyeh.ig.psiutils.TypeUtils;
import org.intellij.lang.annotations.Pattern;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
@@ -39,6 +40,7 @@ public class LengthOneStringInIndexOfInspection
"length.one.string.in.indexof.display.name");
}
@Pattern(VALID_ID_PATTERN)
@Override
@NotNull
public String getID() {
@@ -48,11 +50,9 @@ public class LengthOneStringInIndexOfInspection
@Override
@NotNull
public String buildErrorString(Object... infos) {
final String string = (String)infos[0];
final String escapedString = StringUtil.escapeStringCharacters(string);
return InspectionGadgetsBundle.message(
"expression.can.be.replaced.problem.descriptor",
escapedString);
final PsiExpression literal = (PsiExpression)infos[0];
final String replacement = getReplacement(literal);
return InspectionGadgetsBundle.message("expression.can.be.replaced.no.quotes.problem.descriptor", literal.getText(), replacement);
}
@Override
@@ -77,22 +77,27 @@ public class LengthOneStringInIndexOfInspection
@Override
public void doFix(Project project, ProblemDescriptor descriptor) {
final PsiExpression expression =
(PsiExpression)descriptor.getPsiElement();
final String text = expression.getText();
final int length = text.length();
final String character = text.substring(1, length - 1);
final String charLiteral;
if ("\'".equals(character)) {
charLiteral = "'\\''";
}
else {
charLiteral = '\'' + character + '\'';
}
final PsiExpression expression = (PsiExpression)descriptor.getPsiElement();
final String charLiteral = getReplacement(expression);
PsiReplacementUtil.replaceExpression(expression, charLiteral);
}
}
@NotNull
private static String getReplacement(PsiExpression expression) {
final String text = expression.getText();
final int length = text.length();
final String character = text.substring(1, length - 1);
switch (character) {
case "\'":
return "'\\''";
case "\\\"":
return "'\"'";
default:
return '\'' + character + '\'';
}
}
private static class LengthOneStringsInIndexOfVisitor
extends BaseInspectionVisitor {
@@ -111,11 +116,11 @@ public class LengthOneStringInIndexOfInspection
if (!isArgumentOfIndexOf(expression)) {
return;
}
registerError(expression, value);
registerError(expression, expression);
}
static boolean isArgumentOfIndexOf(PsiExpression expression) {
final PsiElement parent = expression.getParent();
final PsiElement parent = PsiUtil.skipParenthesizedExprUp(expression.getParent());
if (parent == null) {
return false;
}
@@ -0,0 +1,11 @@
class Test {
void simple(String s) {
if(s.indexOf(<warning descr="\"x\" can be replaced with 'x'">"x"</warning>) > 0) {}
if(s.indexOf((<warning descr="\"x\" can be replaced with 'x'">"x"</warning>)) > 0) {}
if(s.indexOf(((<warning descr="\"x\" can be replaced with 'x'">"x"</warning>))) > 0) {}
if(s.indexOf(<warning descr="\"\'\" can be replaced with '\''">"\'"</warning>) > 0) {}
if(s.indexOf(<warning descr="\"'\" can be replaced with '\''">"'"</warning>) > 0) {}
if(s.indexOf("//") > 0) {}
}
}
@@ -0,0 +1,36 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.siyeh.ig.performance;
import com.intellij.codeHighlighting.HighlightDisplayLevel;
import com.intellij.codeInsight.daemon.HighlightDisplayKey;
import com.intellij.profile.codeInspection.ProjectInspectionProfileManager;
import com.intellij.testFramework.LightProjectDescriptor;
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
import com.siyeh.ig.LightInspectionTestCase;
import org.jetbrains.annotations.NotNull;
public class LengthOneStringInIndexOfInspectionTest extends LightCodeInsightFixtureTestCase {
@Override
protected String getBasePath() {
return LightInspectionTestCase.INSPECTION_GADGETS_TEST_DATA_PATH + "com/siyeh/igtest/performance/length_one_strings_in_indexof";
}
@NotNull
@Override
protected LightProjectDescriptor getProjectDescriptor() {
return JAVA_8;
}
private void doTest() {
LengthOneStringInIndexOfInspection inspection = new LengthOneStringInIndexOfInspection();
myFixture.enableInspections(inspection);
ProjectInspectionProfileManager.getInstance(myFixture.getProject()).getCurrentProfile()
.setErrorLevel(HighlightDisplayKey.find(inspection.getShortName()), HighlightDisplayLevel.WARNING, myFixture.getProject());
myFixture.testHighlighting(getTestName(false) + ".java");
}
public void testLengthOneStringInIndexOf() {
doTest();
}
}