From 4352c5247a070f02254647fae73d2d1679ca9e19 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Wed, 20 Mar 2024 10:31:50 +0100 Subject: [PATCH] [java] add option to ignore single argument substring() calls (IDEA-345335) GitOrigin-RevId: 967ada74b5354561b8a32f00484f21bae2d05d51 --- .../messages/InspectionGadgetsBundle.properties | 1 + .../redundancy/RedundantStringOperationInspection.java | 8 ++++++-- .../StringOperationCanBeSimplified.html | 10 ++++++++++ .../RedundantStringOperationInspectionFixTest.java | 6 ++++-- .../RedundantStringOperationInspectionTest.java | 6 ++++-- 5 files changed, 25 insertions(+), 6 deletions(-) diff --git a/java/java-analysis-impl/resources/messages/InspectionGadgetsBundle.properties b/java/java-analysis-impl/resources/messages/InspectionGadgetsBundle.properties index acd7b1aa78e1..9223807aa9bb 100644 --- a/java/java-analysis-impl/resources/messages/InspectionGadgetsBundle.properties +++ b/java/java-analysis-impl/resources/messages/InspectionGadgetsBundle.properties @@ -2175,6 +2175,7 @@ inspection.redundant.string.new.array.message=#ref 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=#ref() call can be replaced with ''{0}()'' inspection.type.may.be.weakened.display.name=Type may be weakened diff --git a/java/java-impl/src/com/siyeh/ig/redundancy/RedundantStringOperationInspection.java b/java/java-impl/src/com/siyeh/ig/redundancy/RedundantStringOperationInspection.java index 4a4f2e53ced7..01efa9dbf2ce 100644 --- a/java/java-impl/src/com/siyeh/ig/redundancy/RedundantStringOperationInspection.java +++ b/java/java-impl/src/com/siyeh/ig/redundancy/RedundantStringOperationInspection.java @@ -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; diff --git a/java/java-impl/src/inspectionDescriptions/StringOperationCanBeSimplified.html b/java/java-impl/src/inspectionDescriptions/StringOperationCanBeSimplified.html index 6dec8ca018af..6c951324fa46 100644 --- a/java/java-impl/src/inspectionDescriptions/StringOperationCanBeSimplified.html +++ b/java/java-impl/src/inspectionDescriptions/StringOperationCanBeSimplified.html @@ -24,6 +24,16 @@ that can be replaced with a simpler expression. This will avoid changing the outcome of String comparisons with == or != after applying the quick-fix in code that uses new String() calls to guarantee a different object identity.

+

+ Use the Do not report single argument substring() calls option below to not report code like the following. +

+

+  stringBuilder.append(string.substring(5));
+
+

which can be replaced with the following. +


+  stringBuilder.append(string, 5, string.length());
+

New in 2018.1

\ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/impl/quickfix/RedundantStringOperationInspectionFixTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/impl/quickfix/RedundantStringOperationInspectionFixTest.java index 169a9be3a1d2..cd68742dceeb 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/impl/quickfix/RedundantStringOperationInspectionFixTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/impl/quickfix/RedundantStringOperationInspectionFixTest.java @@ -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 diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/impl/quickfix/RedundantStringOperationInspectionTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/impl/quickfix/RedundantStringOperationInspectionTest.java index 6324856ebf4a..c6423f589068 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/impl/quickfix/RedundantStringOperationInspectionTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/impl/quickfix/RedundantStringOperationInspectionTest.java @@ -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