mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[java] Report more unnecessary substring() calls (IDEA-345335)
GitOrigin-RevId: 2620edfb2707c8bc5bdf62a6cbfd7911ed4bf386
This commit is contained in:
committed by
intellij-monorepo-bot
parent
9c12176ddb
commit
aca900dbfd
@@ -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<ProblemDescriptor> myProcessors = new CallMapper<ProblemDescriptor>()
|
||||
.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"),
|
||||
|
||||
+4
@@ -20,4 +20,8 @@ class Substring {
|
||||
|
||||
|
||||
}
|
||||
|
||||
void m(StringBuilder sb, StringBuilder chars) {
|
||||
sb.append(chars.<warning descr="Call to 'substring()' is redundant">substring</warning>(1, 3));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user