mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-19 21:11:28 +07:00
IDEA-229896 Unused variables: adapt for pattern variables
GitOrigin-RevId: 78dfe45581f1ed84050635410f834f3195dabfe1
This commit is contained in:
committed by
intellij-monorepo-bot
parent
52ba5a3e10
commit
99e82547b1
@@ -394,6 +394,13 @@ class PostHighlightingVisitor {
|
||||
return highlightInfo;
|
||||
}
|
||||
}
|
||||
else if (parameter instanceof PsiPatternVariable) {
|
||||
HighlightInfo highlightInfo = checkUnusedParameter(parameter, identifier);
|
||||
if (highlightInfo != null) {
|
||||
QuickFixAction.registerQuickFixAction(highlightInfo, QuickFixFactory.getInstance().createRemoveUnusedVariableFix(parameter));
|
||||
return highlightInfo;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
@@ -401,7 +408,8 @@ class PostHighlightingVisitor {
|
||||
private HighlightInfo checkUnusedParameter(@NotNull PsiParameter parameter,
|
||||
@NotNull PsiIdentifier identifier) {
|
||||
if (!myRefCountHolder.isReferenced(parameter) && !UnusedSymbolUtil.isImplicitUsage(myProject, parameter)) {
|
||||
String message = JavaErrorBundle.message("parameter.is.not.used", identifier.getText());
|
||||
String message = JavaErrorBundle.message(parameter instanceof PsiPatternVariable ?
|
||||
"pattern.variable.is.not.used" : "parameter.is.not.used", identifier.getText());
|
||||
return UnusedSymbolUtil.createUnusedSymbolInfo(identifier, message, myDeadCodeInfoType);
|
||||
}
|
||||
return null;
|
||||
|
||||
@@ -72,7 +72,23 @@ public class RemoveUnusedVariableFix implements IntentionAction {
|
||||
@Override
|
||||
public void invoke(@NotNull Project project, Editor editor, PsiFile file) {
|
||||
if (!FileModificationService.getInstance().prepareFileForWrite(myVariable.getContainingFile())) return;
|
||||
removeVariableAndReferencingStatements(editor);
|
||||
if (myVariable instanceof PsiPatternVariable) {
|
||||
removePatternVariable((PsiPatternVariable)myVariable);
|
||||
} else {
|
||||
removeVariableAndReferencingStatements(editor);
|
||||
}
|
||||
}
|
||||
|
||||
private static void removePatternVariable(PsiPatternVariable variable) {
|
||||
Runnable action = () -> {
|
||||
PsiPattern pattern = variable.getPattern();
|
||||
if (pattern instanceof PsiTypeTestPattern) {
|
||||
variable.replace(variable.getTypeElement());
|
||||
return;
|
||||
}
|
||||
throw new UnsupportedOperationException("Unable to remove pattern variable " + variable.getName());
|
||||
};
|
||||
ApplicationManager.getApplication().runWriteAction(action);
|
||||
}
|
||||
|
||||
private void removeVariableAndReferencingStatements(Editor editor) {
|
||||
|
||||
@@ -304,6 +304,7 @@ field.is.not.used=Field ''{0}'' is never used
|
||||
private.field.is.not.used.for.reading=Private field ''{0}'' is assigned but never accessed
|
||||
private.field.is.not.assigned=Private field ''{0}'' is never assigned
|
||||
parameter.is.not.used=Parameter ''{0}'' is never used
|
||||
pattern.variable.is.not.used=Pattern variable ''{0}'' is never used
|
||||
private.method.is.not.used=Private method ''{0}'' is never used
|
||||
method.is.not.used=Method ''{0}'' is never used
|
||||
constructor.is.not.used=Constructor ''{0}'' is never used
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
class X {
|
||||
void expressions(Object obj) {
|
||||
if (obj instanceof String <warning descr="Pattern variable 's' is never used">s</warning>) {
|
||||
|
||||
}
|
||||
if (obj instanceof Integer integer) {
|
||||
System.out.println(integer);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// "Remove pattern variable 'string'" "true"
|
||||
class X {
|
||||
public void test(Object object) {
|
||||
if (object instanceof String) {}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// "Remove pattern variable 'string'" "true"
|
||||
class X {
|
||||
public void test(Object object) {
|
||||
if (object instanceof String str<caret>ing) {}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
package com.intellij.java.codeInsight.daemon;
|
||||
|
||||
import com.intellij.JavaTestUtil;
|
||||
import com.intellij.codeInspection.deadCode.UnusedDeclarationInspection;
|
||||
import com.intellij.testFramework.LightProjectDescriptor;
|
||||
import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -33,6 +34,10 @@ public class LightPatternsHighlightingTest extends LightJavaCodeInsightFixtureTe
|
||||
public void testReassignPatternVariable() {
|
||||
doTest();
|
||||
}
|
||||
public void testUnusedPatternVariable() {
|
||||
myFixture.enableInspections(new UnusedDeclarationInspection());
|
||||
doTest();
|
||||
}
|
||||
|
||||
private void doTest() {
|
||||
myFixture.configureByFile(getTestName(false) + ".java");
|
||||
|
||||
Reference in New Issue
Block a user