diff --git a/java/java-analysis-impl/resources/messages/InspectionGadgetsBundle.properties b/java/java-analysis-impl/resources/messages/InspectionGadgetsBundle.properties index a485318bbc6a..3907a17e9e22 100644 --- a/java/java-analysis-impl/resources/messages/InspectionGadgetsBundle.properties +++ b/java/java-analysis-impl/resources/messages/InspectionGadgetsBundle.properties @@ -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 #ref() is redundant #loc 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 9e04017116d7..ff6ee63c96ff 100644 --- a/java/java-impl/src/com/siyeh/ig/redundancy/RedundantStringOperationInspection.java +++ b/java/java-impl/src/com/siyeh/ig/redundancy/RedundantStringOperationInspection.java @@ -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); + } } } } diff --git a/java/java-tests/testData/inspection/redundantStringOperation/RedundantStrTemplateProcessorFix.java b/java/java-tests/testData/inspection/redundantStringOperation/RedundantStrTemplateProcessorFix.java index 5199757b9014..52c3511a1f57 100644 --- a/java/java-tests/testData/inspection/redundantStringOperation/RedundantStrTemplateProcessorFix.java +++ b/java/java-tests/testData/inspection/redundantStringOperation/RedundantStrTemplateProcessorFix.java @@ -1,7 +1,7 @@ // "Fix all 'Redundant 'String' operation' problems in file" "true" class Foo { void test() { - String str1 = STR.""" + String str1 = STR.""" my long simle @@ -16,9 +16,9 @@ class Foo { string """; - String str3 = StringTemplate.STR."Simple string"; + String str3 = StringTemplate.STR."Simple string"; String str4 = StringTemplate.STR."Simple \{template}"; - String str5 = STR."Simple \{template} " + STR."Simple string" + "str"; + String str5 = STR."Simple \{template} " + STR."Simple string" + "str"; } } \ No newline at end of file