diff --git a/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/PostHighlightingVisitor.java b/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/PostHighlightingVisitor.java index dd123450434e..dd20ef32c99a 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/PostHighlightingVisitor.java +++ b/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/PostHighlightingVisitor.java @@ -38,6 +38,7 @@ import com.intellij.profile.codeInspection.InspectionProjectProfileManager; import com.intellij.psi.*; import com.intellij.psi.codeStyle.JavaCodeStyleManager; import com.intellij.psi.impl.PsiClassImplUtil; +import com.intellij.psi.impl.source.tree.java.PsiDeconstructionPatternVariableImpl; import com.intellij.psi.search.searches.OverridingMethodsSearch; import com.intellij.psi.search.searches.SuperMethodsSearch; import com.intellij.psi.util.*; @@ -146,8 +147,7 @@ class PostHighlightingVisitor { List elements = CollectHighlightsUtil.getElementsInRange(psiRoot, 0, myFile.getTextLength()); for (PsiElement element : elements) { ProgressManager.checkCanceled(); - if (element instanceof PsiIdentifier) { - PsiIdentifier identifier = (PsiIdentifier)element; + if (element instanceof PsiIdentifier identifier) { HighlightInfo info = processIdentifier(identifier, progress, globalUsageHelper); if (info != null) { errorFound |= info.getSeverity() == HighlightSeverity.ERROR; @@ -405,8 +405,7 @@ class PostHighlightingVisitor { if (PsiUtil.isIgnoredName(parameter.getName())) return null; PsiElement declarationScope = parameter.getDeclarationScope(); QuickFixFactory quickFixFactory = QuickFixFactory.getInstance(); - if (declarationScope instanceof PsiMethod) { - PsiMethod method = (PsiMethod)declarationScope; + if (declarationScope instanceof PsiMethod method) { if (PsiUtilCore.hasErrorElementChild(method)) return null; if ((method.isConstructor() || method.hasModifierProperty(PsiModifier.PRIVATE) || @@ -436,7 +435,12 @@ class PostHighlightingVisitor { HighlightInfo highlightInfo = checkUnusedParameter(parameter, identifier, null); if (highlightInfo != null) { if (declarationScope.getParent() instanceof PsiSwitchBlock) { - QuickFixAction.registerQuickFixAction(highlightInfo, quickFixFactory.createRenameToIgnoredFix(parameter, false)); + if (variable instanceof PsiDeconstructionPatternVariableImpl) { + QuickFixAction.registerQuickFixAction(highlightInfo, quickFixFactory.createDeleteFix(parameter)); + } + else { + QuickFixAction.registerQuickFixAction(highlightInfo, quickFixFactory.createRenameToIgnoredFix(parameter, false)); + } } else if (!(variable.getPattern() instanceof PsiTypeTestPattern pattern && pattern.getParent() instanceof PsiDeconstructionList)) { QuickFixAction.registerQuickFixAction(highlightInfo, quickFixFactory.createDeleteFix(parameter)); diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/removeUnusedVariable/afterPatternVariable4.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/removeUnusedVariable/afterPatternVariable4.java new file mode 100644 index 000000000000..14c2a4ae8fce --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/removeUnusedVariable/afterPatternVariable4.java @@ -0,0 +1,13 @@ +// "Remove pattern variable" "true-preview" +class Test { + record Point(double x, double y) {} + + record Rect(Point point1, Point point2) {} + + void foo(Object obj) { + switch (obj) { + case Rect(Point(double x, double y) point1, Point point2) -> {} + default -> {} + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/removeUnusedVariable/afterPatternVariable5.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/removeUnusedVariable/afterPatternVariable5.java new file mode 100644 index 000000000000..8f54e7cd2de8 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/removeUnusedVariable/afterPatternVariable5.java @@ -0,0 +1,13 @@ +// "Remove pattern variable" "true-preview" +class Test { + record Point(double x, double y) {} + + record Rect(Point point1, Point point2) {} + + void foo(Object obj) { + switch (obj) { + case Rect(Point(double x, double y), Point point2) rect -> {} + default -> {} + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/removeUnusedVariable/beforePatternVariable4.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/removeUnusedVariable/beforePatternVariable4.java index 254a20d25e19..d4e9f462989a 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/removeUnusedVariable/beforePatternVariable4.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/removeUnusedVariable/beforePatternVariable4.java @@ -1,10 +1,13 @@ -// "Remove pattern variable" "false" +// "Remove pattern variable" "true-preview" class Test { record Point(double x, double y) {} record Rect(Point point1, Point point2) {} void foo(Object obj) { - if (obj instanceof Rect(Point(double x, double y) point1, Point point2) rect) {} + switch (obj) { + case Rect(Point(double x, double y) point1, Point point2) rect -> {} + default -> {} + } } } \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/removeUnusedVariable/beforePatternVariable5.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/removeUnusedVariable/beforePatternVariable5.java new file mode 100644 index 000000000000..1704796aacbf --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/removeUnusedVariable/beforePatternVariable5.java @@ -0,0 +1,13 @@ +// "Remove pattern variable" "true-preview" +class Test { + record Point(double x, double y) {} + + record Rect(Point point1, Point point2) {} + + void foo(Object obj) { + switch (obj) { + case Rect(Point(double x, double y) point1, Point point2) rect -> {} + default -> {} + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/removeUnusedVariable/beforePatternVariable6.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/removeUnusedVariable/beforePatternVariable6.java new file mode 100644 index 000000000000..c43df37d7d06 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/removeUnusedVariable/beforePatternVariable6.java @@ -0,0 +1,9 @@ +// "Remove pattern variable" "false" +class X { + public void test(Object object) { + switch (object) { + case String string -> {} + default -> {} + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/removeUnusedVariable/beforePatternVariable7.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/removeUnusedVariable/beforePatternVariable7.java new file mode 100644 index 000000000000..254a20d25e19 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/removeUnusedVariable/beforePatternVariable7.java @@ -0,0 +1,10 @@ +// "Remove pattern variable" "false" +class Test { + record Point(double x, double y) {} + + record Rect(Point point1, Point point2) {} + + void foo(Object obj) { + if (obj instanceof Rect(Point(double x, double y) point1, Point point2) rect) {} + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/removeUnusedVariable/beforePatternVariable8.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/removeUnusedVariable/beforePatternVariable8.java new file mode 100644 index 000000000000..8344ebcbe068 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/removeUnusedVariable/beforePatternVariable8.java @@ -0,0 +1,13 @@ +// "Remove pattern variable" "false" +class Test { + record Point(double x, double y) {} + + record Rect(Point point1, Point point2) {} + + void foo(Object obj) { + switch (obj) { + case Rect(Point(double x, double y) point1, Point point2) rect -> {} + default -> {} + } + } +} \ No newline at end of file