From c318f97d18fd72b2505541c584d21bb955aab594 Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Sun, 24 Nov 2019 10:32:15 +0700 Subject: [PATCH] ignoreStringConstructor option GitOrigin-RevId: 6c6e4501fd537d2f9b4143e6307cd139ce0ee83c --- .../siyeh/InspectionGadgetsBundle.properties | 1 + .../RedundantStringOperationInspection.java | 40 ++++++++++++++----- .../StringOperationCanBeSimplified.html | 5 ++- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties index 663c3763f818..b7698f813cc4 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties @@ -2216,6 +2216,7 @@ inspection.redundant.string.intern.on.constant.message=Call to #ref inspection.redundant.string.constructor.message=#ref is redundant #loc 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 constructors inspection.type.may.be.weakened.display.name=Type may be weakened inspection.type.may.be.weakened.problem.descriptor=Type of variable #ref may be weakened to {0} #loc diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/redundancy/RedundantStringOperationInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/redundancy/RedundantStringOperationInspection.java index 378898dabbb8..f0b80a1284ac 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/redundancy/RedundantStringOperationInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/redundancy/RedundantStringOperationInspection.java @@ -4,6 +4,7 @@ package com.siyeh.ig.redundancy; import com.intellij.codeInsight.BlockUtils; import com.intellij.codeInsight.daemon.impl.quickfix.DeleteElementFix; import com.intellij.codeInspection.*; +import com.intellij.codeInspection.ui.SingleCheckboxOptionsPanel; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.TextRange; import com.intellij.psi.*; @@ -20,6 +21,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.PropertyKey; +import javax.swing.*; import java.util.Collections; import java.util.List; import java.util.Objects; @@ -54,6 +56,16 @@ public class RedundantStringOperationInspection extends AbstractBaseJavaLocalIns private static final CallMatcher STRING_INDEX_OF_ONE_ARG = exactInstanceCall(JAVA_LANG_STRING, "indexOf").parameterCount(1); private static final CallMatcher STRING_EQUALS = exactInstanceCall(JAVA_LANG_STRING, "equals").parameterTypes(JAVA_LANG_OBJECT); + public boolean ignoreStringConstructor = false; + + @Nullable + @Override + public JComponent createOptionsPanel() { + return new SingleCheckboxOptionsPanel( + InspectionGadgetsBundle.message("inspection.redundant.string.option.do.not.report.string.constructors"), this, + "ignoreStringConstructor"); + } + @Nls @NotNull @Override @@ -70,7 +82,7 @@ public class RedundantStringOperationInspection extends AbstractBaseJavaLocalIns @NotNull @Override public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) { - return new RedundantStringOperationVisitor(holder, isOnTheFly); + return new RedundantStringOperationVisitor(holder, isOnTheFly, this); } private static class RedundantStringOperationVisitor extends JavaElementVisitor { @@ -88,10 +100,12 @@ public class RedundantStringOperationInspection extends AbstractBaseJavaLocalIns private final InspectionManager myManager; private final ProblemsHolder myHolder; private final boolean myIsOnTheFly; + private final RedundantStringOperationInspection myInspection; - RedundantStringOperationVisitor(ProblemsHolder holder, boolean isOnTheFly) { + RedundantStringOperationVisitor(ProblemsHolder holder, boolean isOnTheFly, RedundantStringOperationInspection inspection) { myHolder = holder; myIsOnTheFly = isOnTheFly; + myInspection = inspection; myManager = myHolder.getManager(); } @@ -109,7 +123,7 @@ public class RedundantStringOperationInspection extends AbstractBaseJavaLocalIns if (ConstructionUtils.isReferenceTo(classRef, CommonClassNames.JAVA_LANG_STRING_BUILDER, CommonClassNames.JAVA_LANG_STRING_BUFFER)) { descriptor = getRedundantArgumentProblem(getSingleEmptyStringArgument(expression)); } - else if (ConstructionUtils.isReferenceTo(classRef, JAVA_LANG_STRING)) { + else if (ConstructionUtils.isReferenceTo(classRef, JAVA_LANG_STRING) && !myInspection.ignoreStringConstructor) { descriptor = getStringConstructorProblem(expression); } if (descriptor != null) { @@ -121,20 +135,28 @@ public class RedundantStringOperationInspection extends AbstractBaseJavaLocalIns PsiExpressionList args = expression.getArgumentList(); if (args == null) return null; if (args.isEmpty()) { - return myManager.createProblemDescriptor(expression, InspectionGadgetsBundle.message( - "inspection.redundant.string.constructor.message"), - new StringConstructorFix(true), - ProblemHighlightType.GENERIC_ERROR_OR_WARNING, myIsOnTheFly); + LocalQuickFix[] fixes = { + new StringConstructorFix(true), + new SetInspectionOptionFix( + myInspection, "ignoreStringConstructor", + InspectionGadgetsBundle.message("inspection.redundant.string.option.do.not.report.string.constructors"), true)}; + return myManager.createProblemDescriptor(expression, (TextRange)null, + InspectionGadgetsBundle.message("inspection.redundant.string.constructor.message"), + ProblemHighlightType.GENERIC_ERROR_OR_WARNING, myIsOnTheFly, fixes); } if (args.getExpressionCount() == 1) { PsiExpression arg = args.getExpressions()[0]; if (TypeUtils.isJavaLangString(arg.getType()) && (PsiUtil.isLanguageLevel7OrHigher(expression) || !STRING_SUBSTRING.matches(arg))) { TextRange range = new TextRange(0, args.getStartOffsetInParent()); + LocalQuickFix[] fixes = { + new StringConstructorFix(false), + new SetInspectionOptionFix( + myInspection, "ignoreStringConstructor", + InspectionGadgetsBundle.message("inspection.redundant.string.option.do.not.report.string.constructors"), true)}; return myManager.createProblemDescriptor(expression, range, InspectionGadgetsBundle.message("inspection.redundant.string.constructor.message"), - ProblemHighlightType.LIKE_UNUSED_SYMBOL, myIsOnTheFly, - new StringConstructorFix(false)); + ProblemHighlightType.LIKE_UNUSED_SYMBOL, myIsOnTheFly, fixes); } } return null; diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/StringOperationCanBeSimplified.html b/plugins/InspectionGadgets/src/inspectionDescriptions/StringOperationCanBeSimplified.html index 7161e22db4df..d2da5e4b078b 100644 --- a/plugins/InspectionGadgets/src/inspectionDescriptions/StringOperationCanBeSimplified.html +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/StringOperationCanBeSimplified.html @@ -1,9 +1,10 @@ -Reports a variety of redundant String-related operations like calling String.toString() or String.substring(0). +Reports a variety of redundant String-related operations like calling string.toString() or string.substring(0). Also, reports usage of redundant String constructors like new String() (equivalent to "") or new String(anotherString) (equivalent to anotherString). Such code, when fixed, may change the program semantics if String referential equality is -important (which is commonly considered as a bad practice). In this case, consider suppressing the warning. +important (which is commonly considered as a bad practice). In this case, consider suppressing the warning or using the inspection option +to disable reporting such problems.

New in 2018.1