[java, inspections, fix] IDEA-323920 IJ-CR-114719

- added check to prevent red code highlighting
- fixed memory leak
- renamed fix a message to be more descriptive
- changed LocalQuickFix to PsiUpdateModCommandQuickFix for consistency

GitOrigin-RevId: 2e96e40fa9e9ddd8596bcf34332d95af785a2ce0
This commit is contained in:
Aleksey Dobrynin
2023-09-13 11:49:01 +00:00
committed by intellij-monorepo-bot
parent 92290b3c81
commit ef437cfdab
3 changed files with 18 additions and 17 deletions
@@ -2178,7 +2178,7 @@ ignore.private.methods.option=Ignore assignments in and returns from private met
inspection.replace.on.literal.display.name=Replacement operation has no effect
inspection.redundant.string.operation.display.name=Redundant 'String' operation
inspection.redundant.embedded.expression.display.name=Redundant embedded expression in string template
inspection.redundant.string.fix.remove.str.processor.description='STR' template processor can be converted to a string literal
inspection.redundant.string.fix.remove.str.processor.description=String template can be converted to a plain string literal
inspection.redundant.string.remove.fix.name=Remove redundant ''{0}()'' call
inspection.redundant.string.fix.family.name=Remove redundant call
inspection.redundant.string.call.message=Call to <code>#ref()</code> is redundant #loc
@@ -3,6 +3,7 @@ package com.siyeh.ig.redundancy;
import com.intellij.codeInsight.BlockUtils;
import com.intellij.codeInsight.daemon.QuickFixBundle;
import com.intellij.codeInsight.daemon.impl.analysis.HighlightingFeature;
import com.intellij.codeInsight.daemon.impl.quickfix.DeleteElementFix;
import com.intellij.codeInspection.*;
import com.intellij.codeInspection.options.OptPane;
@@ -151,8 +152,12 @@ public class RedundantStringOperationInspection extends AbstractBaseJavaLocalIns
@Override
public void visitTemplateExpression(@NotNull PsiTemplateExpression element) {
if (element.getProcessor() != null && element.getLiteralExpression() != null) {
myHolder.registerProblem(element.getProcessor(), InspectionGadgetsBundle.message("inspection.redundant.string.fix.remove.str.processor.description"), new RemoveStrTemplateProcessorFix(element));
if (HighlightingFeature.STRING_TEMPLATES.isAvailable(element)) {
if (element.getProcessor() != null && element.getLiteralExpression() != null) {
myHolder.registerProblem(element.getProcessor(),
InspectionGadgetsBundle.message("inspection.redundant.string.fix.remove.str.processor.description"),
new RemoveStrTemplateProcessorFix());
}
}
}
@@ -1166,23 +1171,19 @@ public class RedundantStringOperationInspection extends AbstractBaseJavaLocalIns
}
}
private static final class RemoveStrTemplateProcessorFix implements LocalQuickFix {
private final PsiTemplateExpression myTemplate;
public RemoveStrTemplateProcessorFix(@NotNull final PsiTemplateExpression template) {
myTemplate = template;
}
private static final class RemoveStrTemplateProcessorFix extends PsiUpdateModCommandQuickFix {
@Override
public @NotNull String getFamilyName() {
return QuickFixBundle.message("remove.redundant.str.processor");
}
@Override
public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
final PsiLiteralExpression literal = myTemplate.getLiteralExpression();
if (literal != null) {
myTemplate.replace(literal);
protected void applyFix(@NotNull Project project, @NotNull PsiElement element, @NotNull ModPsiUpdater updater) {
if (element.getParent() instanceof PsiTemplateExpression template) {
final PsiLiteralExpression literal = template.getLiteralExpression();
if (literal != null) {
template.replace(literal);
}
}
}
}
@@ -1,7 +1,7 @@
// "Fix all 'Redundant 'String' operation' problems in file" "true"
class Foo {
void test() {
String str1 = <warning descr="'STR' template processor can be converted to a string literal">STR<caret></warning>."""
String str1 = <warning descr="String template can be converted to a plain string literal">STR<caret></warning>."""
my
long
simle
@@ -16,9 +16,9 @@ class Foo {
string
""";
String str3 = <warning descr="'STR' template processor can be converted to a string literal">StringTemplate.STR</warning>."Simple string";
String str3 = <warning descr="String template can be converted to a plain string literal">StringTemplate.STR</warning>."Simple string";
String str4 = StringTemplate.STR."Simple \{template}";
String str5 = STR."Simple \{template} " + <warning descr="'STR' template processor can be converted to a string literal">STR</warning>."Simple string" + "str";
String str5 = STR."Simple \{template} " + <warning descr="String template can be converted to a plain string literal">STR</warning>."Simple string" + "str";
}
}