[java-inspection] RedundantStringOperationInspection: make messages more clear

GitOrigin-RevId: 19cff59158d3647024a7c7fb585c43f88466b10f
This commit is contained in:
Andrey.Cherkasov
2020-12-04 00:09:03 +00:00
committed by intellij-monorepo-bot
parent cc53837b01
commit 1fb8a884ac
7 changed files with 81 additions and 18 deletions
@@ -0,0 +1,13 @@
class Main {
void foo1() {
System.out.println(<warning descr="Unnecessary empty string argument">""</warning>);
}
void foo2() {
StringBuilder stringBuilder = new StringBuilder(<warning descr="Unnecessary empty string argument">""</warning>);
}
void foo3() {
StringBuffer stringBuffer = new StringBuffer(<warning descr="Unnecessary empty string argument">""</warning>);
}
}
@@ -0,0 +1,9 @@
class Main {
void foo1(String str) {
str.lastIndexOf("42", <warning descr="Unnecessary string length argument">str.length()</warning>);
}
void foo2(String str) {
str.lastIndexOf("42", <warning descr="Unnecessary string length argument">str.length() - 1</warning>);
}
}
@@ -0,0 +1,9 @@
class Main {
void foo1(String str) {
str.indexOf("42", <warning descr="Unnecessary zero argument">0</warning>);
}
void foo2(String str) {
str.startsWith("42", <warning descr="Unnecessary zero argument">0</warning>);
}
}
@@ -0,0 +1,20 @@
// Copyright 2000-2017 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.intellij.java.codeInsight.daemon.quickFix;
import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase;
import com.intellij.codeInspection.LocalInspectionTool;
import com.siyeh.ig.redundancy.RedundantStringOperationInspection;
import org.jetbrains.annotations.NotNull;
public class RedundantStringOperationInspectionFixTest extends LightQuickFixParameterizedTestCase {
@Override
protected LocalInspectionTool @NotNull [] configureLocalInspectionTools() {
return new LocalInspectionTool[]{new RedundantStringOperationInspection()};
}
@Override
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/redundantStringOperation";
}
}
@@ -1,20 +1,25 @@
// Copyright 2000-2017 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.
// Copyright 2000-2020 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.intellij.java.codeInsight.daemon.quickFix;
import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase;
import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.JavaTestUtil;
import com.intellij.codeInspection.InspectionProfileEntry;
import com.siyeh.ig.LightJavaInspectionTestCase;
import com.siyeh.ig.redundancy.RedundantStringOperationInspection;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class RedundantStringOperationInspectionTest extends LightQuickFixParameterizedTestCase {
public class RedundantStringOperationInspectionTest extends LightJavaInspectionTestCase {
@Nullable
@Override
protected LocalInspectionTool @NotNull [] configureLocalInspectionTools() {
return new LocalInspectionTool[]{new RedundantStringOperationInspection()};
protected InspectionProfileEntry getInspection() {
return new RedundantStringOperationInspection();
}
public void testEmptyStringArgument() {doTest();}
public void testStringLengthArgument() {doTest();}
public void testZeroArgument() {doTest();}
@Override
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/redundantStringOperation";
return JavaTestUtil.getRelativeJavaTestDataPath() + "/inspection/redundantStringOperation/";
}
}
@@ -2193,7 +2193,9 @@ inspection.redundant.string.operation.display.name=Redundant String operation
inspection.redundant.string.remove.fix.name=Remove redundant ''{0}()'' call
inspection.redundant.string.fix.family.name=Remove redundant call
inspection.redundant.string.call.message=Call to <code>#ref()</code> is redundant #loc
inspection.redundant.string.argument.message=Redundant argument
inspection.redundant.empty.string.argument.message=Unnecessary empty string argument
inspection.redundant.string.length.argument.message=Unnecessary string length argument
inspection.redundant.zero.argument.message=Unnecessary zero argument
inspection.redundant.string.remove.argument.fix.name=Remove argument
inspection.redundant.string.intern.on.constant.message=Call to <code>#ref()</code> on compile-time constant is unnecessary #loc
inspection.redundant.string.constructor.message=<code>#ref</code> is redundant #loc
@@ -95,7 +95,8 @@ public class RedundantStringOperationInspection extends AbstractBaseJavaLocalIns
.register(STRING_BUILDER_APPEND, this::getAppendProblem)
.register(STRING_BUILDER_TO_STRING, this::getRedundantStringBuilderToStringProblem)
.register(STRING_INTERN, this::getInternProblem)
.register(PRINTSTREAM_PRINTLN, call -> getRedundantArgumentProblem(getSingleEmptyStringArgument(call)))
.register(PRINTSTREAM_PRINTLN, call ->
getRedundantArgumentProblem(getSingleEmptyStringArgument(call), "inspection.redundant.empty.string.argument.message"))
.register(METHOD_WITH_REDUNDANT_ZERO_AS_SECOND_PARAMETER, this::getRedundantZeroAsSecondParameterProblem)
.register(STRING_LAST_INDEX_OF, this::getLastIndexOfProblem)
.register(STRING_IS_EMPTY, this::getRedundantCaseChangeProblem)
@@ -125,7 +126,8 @@ public class RedundantStringOperationInspection extends AbstractBaseJavaLocalIns
PsiJavaCodeReferenceElement classRef = expression.getClassReference();
ProblemDescriptor descriptor = null;
if (ConstructionUtils.isReferenceTo(classRef, JAVA_LANG_STRING_BUILDER, JAVA_LANG_STRING_BUFFER)) {
descriptor = getRedundantArgumentProblem(getSingleEmptyStringArgument(expression));
String key = "inspection.redundant.empty.string.argument.message";
descriptor = getRedundantArgumentProblem(getSingleEmptyStringArgument(expression), key);
}
else if (ConstructionUtils.isReferenceTo(classRef, JAVA_LANG_STRING) && !myInspection.ignoreStringConstructor) {
descriptor = getStringConstructorProblem(expression);
@@ -422,26 +424,28 @@ public class RedundantStringOperationInspection extends AbstractBaseJavaLocalIns
stripped = binOp.getLOperand();
}
}
return isLengthOf(stripped, call.getMethodExpression().getQualifierExpression()) ? getRedundantArgumentProblem(secondArg) : null;
PsiExpression qualifier = call.getMethodExpression().getQualifierExpression();
if (!isLengthOf(stripped, qualifier)) return null;
return getRedundantArgumentProblem(secondArg, "inspection.redundant.string.length.argument.message");
}
@Nullable
private ProblemDescriptor getRedundantZeroAsSecondParameterProblem(PsiMethodCallExpression call) {
PsiExpression secondArg = call.getArgumentList().getExpressions()[1];
if (ExpressionUtils.isLiteral(PsiUtil.skipParenthesizedExprDown(secondArg), 0)) {
return getRedundantArgumentProblem(secondArg);
return getRedundantArgumentProblem(secondArg, "inspection.redundant.zero.argument.message");
}
return null;
}
@Nullable
private ProblemDescriptor getRedundantArgumentProblem(@Nullable PsiExpression argument) {
private ProblemDescriptor getRedundantArgumentProblem(@Nullable PsiExpression argument,
@NotNull @PropertyKey(resourceBundle = BUNDLE) String key) {
if (argument == null) return null;
LocalQuickFix fix =
new DeleteElementFix(argument, InspectionGadgetsBundle.message("inspection.redundant.string.remove.argument.fix.name"));
return myManager.createProblemDescriptor(argument,
InspectionGadgetsBundle.message(
"inspection.redundant.string.argument.message"),
InspectionGadgetsBundle.message(key),
myIsOnTheFly,
new LocalQuickFix[]{fix},
ProblemHighlightType.LIKE_UNUSED_SYMBOL);
@@ -470,8 +474,9 @@ public class RedundantStringOperationInspection extends AbstractBaseJavaLocalIns
}
DeleteElementFix fix =
new DeleteElementFix(args[1], InspectionGadgetsBundle.message("inspection.redundant.string.remove.argument.fix.name"));
final String message = InspectionGadgetsBundle.message("inspection.redundant.string.length.argument.message");
return myManager.createProblemDescriptor(args[1],
InspectionGadgetsBundle.message("inspection.redundant.string.argument.message"),
message,
fix, ProblemHighlightType.LIKE_UNUSED_SYMBOL, myIsOnTheFly);
}