[java] add option to ignore single argument substring() calls (IDEA-345335)

GitOrigin-RevId: 967ada74b5354561b8a32f00484f21bae2d05d51
This commit is contained in:
Bas Leijdekkers
2024-03-21 11:05:13 +00:00
committed by intellij-monorepo-bot
parent a96178541e
commit 4352c5247a
5 changed files with 25 additions and 6 deletions
@@ -2175,6 +2175,7 @@ inspection.redundant.string.new.array.message=<code>#ref</code> is redundant #lo
inspection.redundant.string.replace.with.arg.fix.name=Replace with argument
inspection.redundant.string.replace.with.empty.fix.name=Replace with empty string
inspection.redundant.string.option.do.not.report.string.constructors=Do not report String constructor calls
inspection.redundant.string.option.do.not.report.single.argument.substring=Do not report redundant single argument substring() calls
inspection.x.call.can.be.replaced.with.y=<code>#ref()</code> call can be replaced with ''{0}()''
inspection.type.may.be.weakened.display.name=Type may be weakened
@@ -85,12 +85,15 @@ public final class RedundantStringOperationInspection extends AbstractBaseJavaLo
exactInstanceCall(JAVA_LANG_STRING, "strip", "stripLeading", "stripTrailing").parameterCount(0);
public boolean ignoreStringConstructor = false;
public boolean ignoreSingleArgSubstring = true;
@Override
public @NotNull OptPane getOptionsPane() {
return pane(
checkbox("ignoreStringConstructor",
InspectionGadgetsBundle.message("inspection.redundant.string.option.do.not.report.string.constructors")));
InspectionGadgetsBundle.message("inspection.redundant.string.option.do.not.report.string.constructors")),
checkbox("ignoreSingleArgSubstring",
InspectionGadgetsBundle.message("inspection.redundant.string.option.do.not.report.single.argument.substring")));
}
@NotNull
@@ -572,7 +575,8 @@ public final class RedundantStringOperationInspection extends AbstractBaseJavaLo
PsiExpression[] args = call.getArgumentList().getExpressions();
PsiExpression stringExpression = call.getMethodExpression().getQualifierExpression();
if (args.length == 1) {
if (ExpressionUtils.isZero(args[0]) ||
if (myInspection.ignoreSingleArgSubstring ||
ExpressionUtils.isZero(args[0]) ||
isLengthOf(args[0], stringExpression) ||
!(PsiUtil.deparenthesizeExpression(stringExpression) instanceof PsiReferenceExpression)) {
return null;
@@ -24,6 +24,16 @@ that can be replaced with a simpler expression.
This will avoid changing the outcome of String comparisons with <code>==</code> or <code>!=</code> after applying
the quick-fix in code that uses <code>new String()</code> calls to guarantee a different object identity.
</p>
<p>
Use the <b>Do not report single argument substring() calls</b> option below to not report code like the following.
</p>
<pre><code>
stringBuilder.append(string.substring(5));
</code></pre>
<p>which can be replaced with the following.
<pre><code>
stringBuilder.append(string, 5, string.length());
</code></pre>
<p><small>New in 2018.1</small></p>
</body>
</html>
@@ -1,4 +1,4 @@
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase;
@@ -12,7 +12,9 @@ import org.jetbrains.annotations.NotNull;
public class RedundantStringOperationInspectionFixTest extends LightQuickFixParameterizedTestCase {
@Override
protected LocalInspectionTool @NotNull [] configureLocalInspectionTools() {
return new LocalInspectionTool[]{new RedundantStringOperationInspection()};
RedundantStringOperationInspection inspection = new RedundantStringOperationInspection();
inspection.ignoreSingleArgSubstring = false;
return new LocalInspectionTool[]{inspection};
}
@Override
@@ -1,4 +1,4 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.JavaTestUtil;
@@ -16,7 +16,9 @@ public class RedundantStringOperationInspectionTest extends LightJavaInspectionT
@Nullable
@Override
protected InspectionProfileEntry getInspection() {
return new RedundantStringOperationInspection();
RedundantStringOperationInspection inspection = new RedundantStringOperationInspection();
inspection.ignoreSingleArgSubstring = false;
return inspection;
}
@Override