From b178153d324c06b678290e81cfeb233f5e7f6921 Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Wed, 2 Oct 2019 12:03:43 +0700 Subject: [PATCH] IDEA-223821 `Catch block may ignore exception` inspection does not check for existence of local variable with `ignored` name GitOrigin-RevId: 719b50764c9988a499497d6f2d597b1e45c41e4a --- .../siyeh/InspectionGadgetsBundle.properties | 2 +- .../CatchMayIgnoreExceptionInspection.java | 30 +++++++++++++++---- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties index 557bd58275bf..ad16aafb6ae9 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties @@ -1595,7 +1595,7 @@ loop.with.implicit.termination.condition.display.name=Loop with implicit termina loop.with.implicit.termination.condition.dowhile.problem.descriptor=#ref-while loop with implicit termination condition #loc loop.with.implicit.termination.condition.problem.descriptor=#ref loop with implicit termination condition #loc loop.with.implicit.termination.condition.quickfix=Make condition explicit -rename.catch.parameter.to.ignored=Rename 'catch' parameter to 'ignored' +rename.catch.parameter.to.ignored=Rename ''catch'' parameter to ''{0}'' unnecessary.super.qualifier.display.name=Unnecessary 'super' qualifier unnecessary.super.qualifier.problem.descriptor=Qualifier #ref is unnecessary in this context #loc unnecessary.super.qualifier.quickfix=Remove unnecessary 'super' qualifier diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/errorhandling/CatchMayIgnoreExceptionInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/errorhandling/CatchMayIgnoreExceptionInspection.java index 512d4c372d07..7192b8e1d5d6 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/errorhandling/CatchMayIgnoreExceptionInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/errorhandling/CatchMayIgnoreExceptionInspection.java @@ -15,6 +15,7 @@ import com.intellij.codeInspection.dataFlow.value.DfaVariableValue; import com.intellij.codeInspection.ui.MultipleCheckboxOptionsPanel; import com.intellij.openapi.project.Project; import com.intellij.psi.*; +import com.intellij.psi.codeStyle.VariableKind; import com.intellij.psi.impl.light.LightParameter; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.psi.util.PsiUtil; @@ -24,7 +25,9 @@ import com.siyeh.ig.fixes.SuppressForTestsScopeFix; import com.siyeh.ig.psiutils.ControlFlowUtils; import com.siyeh.ig.psiutils.TestUtils; import com.siyeh.ig.psiutils.VariableAccessUtils; +import com.siyeh.ig.psiutils.VariableNameGenerator; import one.util.streamex.StreamEx; +import org.jetbrains.annotations.Nls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -56,7 +59,7 @@ public class CatchMayIgnoreExceptionInspection extends AbstractBaseJavaLocalInsp public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) { return new JavaElementVisitor() { @Override - public void visitTryStatement(@NotNull PsiTryStatement statement) { + public void visitTryStatement(PsiTryStatement statement) { super.visitTryStatement(statement); final PsiCatchSection[] catchSections = statement.getCatchSections(); for (final PsiCatchSection section : catchSections) { @@ -88,13 +91,13 @@ public class CatchMayIgnoreExceptionInspection extends AbstractBaseJavaLocalInsp SuppressForTestsScopeFix fix = SuppressForTestsScopeFix.build(CatchMayIgnoreExceptionInspection.this, section); if (ControlFlowUtils.isEmpty(block, m_ignoreCatchBlocksWithComments, true)) { holder.registerProblem(catchToken, InspectionGadgetsBundle.message("inspection.catch.ignores.exception.empty.message"), - new EmptyCatchBlockFix(), fix); + new EmptyCatchBlockFix(generateName(block)), fix); } else if (!VariableAccessUtils.variableIsUsed(parameter, section)) { if (!m_ignoreNonEmptyCatchBlock && (!m_ignoreCatchBlocksWithComments || PsiTreeUtil.getChildOfType(block, PsiComment.class) == null)) { holder.registerProblem(identifier, InspectionGadgetsBundle.message("inspection.catch.ignores.exception.unused.message"), - new RenameFix("ignored", false, false), fix); + new RenameFix(generateName(block), false, false), fix); } } else if (mayIgnoreVMException(parameter, block)) { @@ -142,6 +145,11 @@ public class CatchMayIgnoreExceptionInspection extends AbstractBaseJavaLocalInsp }; } + @NotNull + private static String generateName(PsiCodeBlock block) { + return new VariableNameGenerator(block, VariableKind.LOCAL_VARIABLE).byName(IGNORED_PARAMETER_NAME).generate(true); + } + static class IgnoredExceptionVisitor extends SideEffectVisitor { private final @NotNull PsiParameter myParameter; private final @NotNull PsiCodeBlock myBlock; @@ -183,11 +191,23 @@ public class CatchMayIgnoreExceptionInspection extends AbstractBaseJavaLocalInsp } private static class EmptyCatchBlockFix implements LocalQuickFix { + private final String myName; + + private EmptyCatchBlockFix(String name) { + myName = name; + } + + @Nls(capitalization = Nls.Capitalization.Sentence) + @NotNull + @Override + public String getName() { + return InspectionGadgetsBundle.message("rename.catch.parameter.to.ignored", myName); + } @Override @NotNull public String getFamilyName() { - return InspectionGadgetsBundle.message("rename.catch.parameter.to.ignored"); + return InspectionGadgetsBundle.message("rename.catch.parameter.to.ignored", IGNORED_PARAMETER_NAME); } @Override @@ -201,7 +221,7 @@ public class CatchMayIgnoreExceptionInspection extends AbstractBaseJavaLocalInsp final PsiIdentifier identifier = parameter.getNameIdentifier(); if (identifier == null) return; final PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory(); - final PsiIdentifier newIdentifier = factory.createIdentifier(IGNORED_PARAMETER_NAME); + final PsiIdentifier newIdentifier = factory.createIdentifier(myName); identifier.replace(newIdentifier); } }