From 27b2efd1068f927ea4b9a045853cc725e145bcbf Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Mon, 1 Oct 2018 13:43:19 +0700 Subject: [PATCH] LengthOneStringInIndexOf: unit-test, fix for parentheses, unescape "\"" -> '"'; cleanup warning message --- .../siyeh/InspectionGadgetsBundle.properties | 1 + .../LengthOneStringInIndexOfInspection.java | 45 ++++++++++--------- .../LengthOneStringInIndexOf.java | 11 +++++ ...engthOneStringInIndexOfInspectionTest.java | 36 +++++++++++++++ 4 files changed, 73 insertions(+), 20 deletions(-) create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igtest/performance/length_one_strings_in_indexof/LengthOneStringInIndexOf.java create mode 100644 plugins/InspectionGadgets/testsrc/com/siyeh/ig/performance/LengthOneStringInIndexOfInspectionTest.java diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties index b92a15c23301..08e161f95f72 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties @@ -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=#ref 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 #ref() (cyclomatic complexity = {0}) #loc method.coupling.limit.option=Method coupling limit: method.coupling.problem.descriptor=#ref is overly coupled (# referenced classes = {0}) #loc diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/LengthOneStringInIndexOfInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/LengthOneStringInIndexOfInspection.java index 68a4df5d1509..8c85a8c5b987 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/LengthOneStringInIndexOfInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/LengthOneStringInIndexOfInspection.java @@ -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; } diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/performance/length_one_strings_in_indexof/LengthOneStringInIndexOf.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/performance/length_one_strings_in_indexof/LengthOneStringInIndexOf.java new file mode 100644 index 000000000000..adadbaba9455 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/performance/length_one_strings_in_indexof/LengthOneStringInIndexOf.java @@ -0,0 +1,11 @@ +class Test { + void simple(String s) { + if(s.indexOf("x") > 0) {} + if(s.indexOf(("x")) > 0) {} + if(s.indexOf((("x"))) > 0) {} + if(s.indexOf("\'") > 0) {} + if(s.indexOf("'") > 0) {} + + if(s.indexOf("//") > 0) {} + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/performance/LengthOneStringInIndexOfInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/performance/LengthOneStringInIndexOfInspectionTest.java new file mode 100644 index 000000000000..c2c383d580d9 --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/performance/LengthOneStringInIndexOfInspectionTest.java @@ -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(); + } + +}