From aca900dbfd03e6feb1dff098ddff913d02299496 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Thu, 14 Mar 2024 16:40:28 +0100 Subject: [PATCH] [java] Report more unnecessary substring() calls (IDEA-345335) GitOrigin-RevId: 2620edfb2707c8bc5bdf62a6cbfd7911ed4bf386 --- .../redundancy/RedundantStringOperationInspection.java | 10 ++++++++-- .../RedundantStringOperation.java | 4 ++++ 2 files changed, 12 insertions(+), 2 deletions(-) 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 cdc2e8696e90..23b612e67b08 100644 --- a/java/java-impl/src/com/siyeh/ig/redundancy/RedundantStringOperationInspection.java +++ b/java/java-impl/src/com/siyeh/ig/redundancy/RedundantStringOperationInspection.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.siyeh.ig.redundancy; import com.intellij.codeInsight.BlockUtils; @@ -57,10 +57,15 @@ public final class RedundantStringOperationInspection extends AbstractBaseJavaLo private static final CallMatcher STRING_INTERN = exactInstanceCall(JAVA_LANG_STRING, "intern").parameterCount(0); private static final CallMatcher STRING_LENGTH = exactInstanceCall(JAVA_LANG_STRING, HardcodedMethodConstants.LENGTH).parameterCount(0); private static final CallMatcher STRING_SUBSTRING_ONE_ARG = exactInstanceCall(JAVA_LANG_STRING, "substring").parameterTypes("int"); + private static final CallMatcher STRING_BUILDER_SUBSTRING_ONE_ARG = exactInstanceCall(JAVA_LANG_ABSTRACT_STRING_BUILDER, "substring").parameterTypes("int"); private static final CallMatcher STRING_SUBSTRING_TWO_ARG = exactInstanceCall(JAVA_LANG_STRING, "substring").parameterTypes("int", "int"); + private static final CallMatcher STRING_BUILDER_SUBSTRING_TWO_ARG = exactInstanceCall(JAVA_LANG_ABSTRACT_STRING_BUILDER, "substring").parameterTypes("int", "int"); private static final CallMatcher STRING_SUBSTRING = anyOf(STRING_SUBSTRING_ONE_ARG, STRING_SUBSTRING_TWO_ARG); + private static final CallMatcher STRING_BUILDER_SUBSTRING = anyOf(STRING_BUILDER_SUBSTRING_ONE_ARG, STRING_BUILDER_SUBSTRING_TWO_ARG); private static final CallMatcher STRING_BUILDER_APPEND = instanceCall(JAVA_LANG_ABSTRACT_STRING_BUILDER, "append").parameterTypes(JAVA_LANG_STRING); + private static final CallMatcher SINGLE_ARG_STRING_BUILDER_APPEND = + instanceCall(JAVA_LANG_ABSTRACT_STRING_BUILDER, "append").parameterCount(1); private static final CallMatcher STRING_BUILDER_TO_STRING = instanceCall(JAVA_LANG_ABSTRACT_STRING_BUILDER, TO_STRING).parameterCount(0); private static final CallMatcher PRINTSTREAM_PRINTLN = instanceCall("java.io.PrintStream", "println") .parameterTypes(JAVA_LANG_STRING); @@ -103,6 +108,7 @@ public final class RedundantStringOperationInspection extends AbstractBaseJavaLo private final CallMapper myProcessors = new CallMapper() .register(STRING_TO_STRING, call -> getProblem(call, "inspection.redundant.string.call.message")) .register(STRING_SUBSTRING, this::getSubstringProblem) + .register(STRING_BUILDER_SUBSTRING, this::getSubstringProblem) .register(STRING_BUILDER_APPEND, this::getAppendProblem) .register(STRING_BUILDER_TO_STRING, this::getStringBuilderToStringProblem) .register(STRING_INTERN, this::getInternProblem) @@ -598,7 +604,7 @@ public final class RedundantStringOperationInspection extends AbstractBaseJavaLo } PsiElement parent = PsiUtil.skipParenthesizedExprUp(call.getParent()); if (parent instanceof PsiExpressionList list && list.getExpressionCount() == 1 && - parent.getParent() instanceof PsiMethodCallExpression parentCall && STRING_BUILDER_APPEND.test(parentCall)) { + parent.getParent() instanceof PsiMethodCallExpression parentCall && SINGLE_ARG_STRING_BUILDER_APPEND.test(parentCall)) { PsiElement nameElement = Objects.requireNonNull(call.getMethodExpression().getReferenceNameElement()); return myManager.createProblemDescriptor(nameElement, InspectionGadgetsBundle.message("inspection.redundant.string.call.message"), diff --git a/java/java-tests/testData/inspection/redundantStringOperation/RedundantStringOperation.java b/java/java-tests/testData/inspection/redundantStringOperation/RedundantStringOperation.java index 30b26f819a86..305bc4e656c8 100644 --- a/java/java-tests/testData/inspection/redundantStringOperation/RedundantStringOperation.java +++ b/java/java-tests/testData/inspection/redundantStringOperation/RedundantStringOperation.java @@ -20,4 +20,8 @@ class Substring { } + + void m(StringBuilder sb, StringBuilder chars) { + sb.append(chars.substring(1, 3)); + } } \ No newline at end of file