IDEA-274612 - added "rename to ignore" quick-fix for pattern variables in switch

GitOrigin-RevId: 7397ad192bea6d73b1c19371fd7a1b54747f7369
This commit is contained in:
Ilyas Selimov
2021-08-03 17:48:36 +07:00
committed by intellij-monorepo-bot
parent 957cdebe27
commit 41a28868e6
3 changed files with 9 additions and 6 deletions

View File

@@ -406,10 +406,15 @@ class PostHighlightingVisitor {
return highlightInfo;
}
}
else if (parameter instanceof PsiPatternVariable) {
else if (parameter instanceof PsiPatternVariable && !PsiUtil.isIgnoredName(parameter.getName())) {
HighlightInfo highlightInfo = checkUnusedParameter(parameter, identifier, null);
if (highlightInfo != null) {
QuickFixAction.registerQuickFixAction(highlightInfo, QuickFixFactory.getInstance().createDeleteFix(parameter));
if (parameter.getDeclarationScope().getParent() instanceof PsiSwitchBlock) {
QuickFixAction.registerQuickFixAction(highlightInfo, QuickFixFactory.getInstance().createRenameToIgnoredFix(parameter));
}
else {
QuickFixAction.registerQuickFixAction(highlightInfo, QuickFixFactory.getInstance().createDeleteFix(parameter));
}
return highlightInfo;
}
}
@@ -420,9 +425,6 @@ class PostHighlightingVisitor {
private HighlightInfo checkUnusedParameter(@NotNull PsiParameter parameter,
@NotNull PsiIdentifier identifier,
@Nullable PsiMethod declarationMethod) {
if (parameter instanceof PsiPatternVariable && parameter.getDeclarationScope().getParent() instanceof PsiSwitchBlock) {
return null;
}
if (!myRefCountHolder.isReferenced(parameter) && !UnusedSymbolUtil.isImplicitUsage(myProject, parameter)) {
String message = JavaErrorBundle.message(parameter instanceof PsiPatternVariable ?
"pattern.variable.is.not.used" : "parameter.is.not.used", identifier.getText());

View File

@@ -1,7 +1,7 @@
class Test {
void insideSwitch(Object o) {
switch (o){
case /*unused*/ Object s /*unused*/ -> System.out.println();
case /*unused*/ Object <warning descr="Pattern variable 's' is never used"><caret>s</warning> /*unused*/ -> System.out.println();
}
switch (o){
case Object s -> System.out.println(s);

View File

@@ -102,6 +102,7 @@ public class LightPatternsForSwitchHighlightingTest extends LightJavaCodeInsight
public void testUnusedPatternVariable() {
myFixture.enableInspections(new UnusedDeclarationInspection());
doTest();
assertNotNull(myFixture.getAvailableIntention("Rename 's' to 'ignored'"));
}
private void doTest() {