mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
RedundantStringOperationInspection: fixes after review IDEA-CR-25339
1. messages cleanup
2. new StringBuilder(""). PrintStream.println("") are warned
This commit is contained in:
+6
@@ -0,0 +1,6 @@
|
||||
// "Delete element" "true"
|
||||
class Foo {
|
||||
public static void main(String[] args) {
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// "Remove redundant 'append' call" "true"
|
||||
// "Remove redundant 'append()' call" "true"
|
||||
class Foo {
|
||||
public static void main(String[] args) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
// "Delete element" "true"
|
||||
class Foo {
|
||||
public static void main(String[] args) {
|
||||
StringBuilder s = new StringBuilder();
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// "Remove redundant 'intern' call" "true"
|
||||
// "Remove redundant 'intern()' call" "true"
|
||||
class Foo {
|
||||
private static final String x = ("Hello "+"World"+'!');
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// "Remove redundant 'toString' call" "true"
|
||||
// "Remove redundant 'toString()' call" "true"
|
||||
class Foo {
|
||||
public static void main(String[] args) {
|
||||
/*valuable comment!!!*/
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// "Remove redundant 'toString' call" "false"
|
||||
// "Remove redundant 'toString()' call" "false"
|
||||
class Foo {
|
||||
public static void main(String[] args) {
|
||||
Object[] a = args;
|
||||
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
// "Delete element" "true"
|
||||
class Foo {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("<caret>");
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// "Remove redundant 'append' call" "true"
|
||||
// "Remove redundant 'append()' call" "true"
|
||||
class Foo {
|
||||
public static void main(String[] args) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
// "Delete element" "true"
|
||||
class Foo {
|
||||
public static void main(String[] args) {
|
||||
StringBuilder s = new StringBuilder(("<caret>"));
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// "Remove redundant 'intern' call" "true"
|
||||
// "Remove redundant 'intern()' call" "true"
|
||||
class Foo {
|
||||
private static final String x = ("Hello "+"World"+'!').inte<caret>rn();
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// "Remove redundant 'intern' call" "false"
|
||||
// "Remove redundant 'intern()' call" "false"
|
||||
class Foo {
|
||||
private static final String x = ("Hello "+"World".trim()+'!').inte<caret>rn();
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// "Remove redundant 'toString' call" "true"
|
||||
// "Remove redundant 'toString()' call" "true"
|
||||
class Foo {
|
||||
public static void main(String[] args) {
|
||||
String s = args[0].toString<caret>(/*valuable comment!!!*/);
|
||||
|
||||
+4
-3
@@ -2229,7 +2229,8 @@ return.of.field.with.mutable.type.problem.descriptor=Return of {0} field <code>{
|
||||
ignore.private.methods.option=Ignore assignments in and returns from private methods
|
||||
|
||||
inspection.redundant.string.operation.display.name=Redundant String operation
|
||||
inspection.redundant.string.remove.fix.name=Remove redundant ''{0}'' call
|
||||
inspection.redundant.string.remove.fix.name=Remove redundant ''{0}()'' call
|
||||
inspection.redundant.string.replace.with.qualifier.fix.family.name=Replace with qualifier
|
||||
inspection.redundant.string.message=<code>#ref</code> is redundant #loc
|
||||
inspection.redundant.string.intern.on.constant.message=<code>.#ref()</code> on compile-time constant is unnecessary #loc
|
||||
inspection.redundant.string.call.message=Call to <code>#ref</code> is redundant #loc
|
||||
inspection.redundant.string.argument.message=Unnecessary empty string argument
|
||||
inspection.redundant.string.intern.on.constant.message=Call to <code>#ref</code> on compile-time constant is unnecessary #loc
|
||||
|
||||
+36
-5
@@ -1,6 +1,7 @@
|
||||
// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package com.siyeh.ig.redundancy;
|
||||
|
||||
import com.intellij.codeInsight.daemon.impl.quickfix.DeleteElementFix;
|
||||
import com.intellij.codeInspection.*;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
@@ -32,6 +33,8 @@ public class RedundantStringOperationInspection extends AbstractBaseJavaLocalIns
|
||||
private static final CallMatcher STRING_BUILDER_APPEND =
|
||||
CallMatcher.instanceCall(CommonClassNames.JAVA_LANG_ABSTRACT_STRING_BUILDER, "append")
|
||||
.parameterTypes(JAVA_LANG_STRING);
|
||||
private static final CallMatcher PRINTSTREAM_PRINTLN = CallMatcher.instanceCall("java.io.PrintStream", "println")
|
||||
.parameterTypes(JAVA_LANG_STRING);
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
@@ -42,23 +45,51 @@ public class RedundantStringOperationInspection extends AbstractBaseJavaLocalIns
|
||||
PsiExpression qualifier = call.getMethodExpression().getQualifierExpression();
|
||||
if (qualifier != null) {
|
||||
if (STRING_TO_STRING.test(call)) {
|
||||
registerProblem(call, "inspection.redundant.string.message");
|
||||
registerProblem(call, "inspection.redundant.string.call.message");
|
||||
}
|
||||
else if (STRING_SUBSTRING.test(call)) {
|
||||
processSubstring(call);
|
||||
}
|
||||
else if (STRING_BUILDER_APPEND.test(call)) {
|
||||
PsiExpression arg = call.getArgumentList().getExpressions()[0];
|
||||
if (ExpressionUtils.isLiteral(PsiUtil.skipParenthesizedExprDown(arg), "")) {
|
||||
registerProblem(call, "inspection.redundant.string.message");
|
||||
if (getSingleEmptyStringArgument(call) != null) {
|
||||
registerProblem(call, "inspection.redundant.string.call.message");
|
||||
}
|
||||
}
|
||||
else if (STRING_INTERN.test(call) && PsiUtil.isConstantExpression(qualifier)) {
|
||||
registerProblem(call, "inspection.redundant.string.intern.on.constant.message");
|
||||
}
|
||||
else if (PRINTSTREAM_PRINTLN.test(call)) {
|
||||
checkUnnecessaryEmptyStringArgument(call);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitNewExpression(PsiNewExpression expression) {
|
||||
PsiJavaCodeReferenceElement classRef = expression.getClassReference();
|
||||
if (classRef == null) return;
|
||||
String className = classRef.getQualifiedName();
|
||||
if (CommonClassNames.JAVA_LANG_STRING_BUILDER.equals(className) || CommonClassNames.JAVA_LANG_STRING_BUFFER.equals(className)) {
|
||||
checkUnnecessaryEmptyStringArgument(expression);
|
||||
}
|
||||
}
|
||||
|
||||
private void checkUnnecessaryEmptyStringArgument(PsiCall call) {
|
||||
PsiExpression argument = getSingleEmptyStringArgument(call);
|
||||
if (argument != null) {
|
||||
holder.registerProblem(argument, InspectionGadgetsBundle.message("inspection.redundant.string.argument.message"),
|
||||
ProblemHighlightType.LIKE_UNUSED_SYMBOL, new DeleteElementFix(argument));
|
||||
}
|
||||
}
|
||||
|
||||
private PsiExpression getSingleEmptyStringArgument(PsiCall call) {
|
||||
PsiExpressionList argList = call.getArgumentList();
|
||||
if (argList == null) return null;
|
||||
PsiExpression[] args = argList.getExpressions();
|
||||
if (args.length != 1) return null;
|
||||
return ExpressionUtils.isLiteral(PsiUtil.skipParenthesizedExprDown(args[0]), "") ? args[0] : null;
|
||||
}
|
||||
|
||||
private void processSubstring(PsiMethodCallExpression call) {
|
||||
PsiExpression[] args = call.getArgumentList().getExpressions();
|
||||
if (!ExpressionUtils.isZero(args[0])) return;
|
||||
@@ -72,7 +103,7 @@ public class RedundantStringOperationInspection extends AbstractBaseJavaLocalIns
|
||||
return;
|
||||
}
|
||||
}
|
||||
registerProblem(call, "inspection.redundant.string.message");
|
||||
registerProblem(call, "inspection.redundant.string.call.message");
|
||||
}
|
||||
|
||||
private void registerProblem(PsiMethodCallExpression call, @NotNull @PropertyKey(resourceBundle = BUNDLE) String key) {
|
||||
Reference in New Issue
Block a user