[java-inspections] 'Remove pattern variable' fix is not available on unused record pattern variables in switch'es

IDEA-302981

GitOrigin-RevId: 7b350efb5bf6010efacd5330e7a6e526aa9acfae
This commit is contained in:
Andrey.Cherkasov
2022-10-04 21:55:11 +04:00
committed by intellij-monorepo-bot
parent 8ad4946564
commit a9a92e5dca
8 changed files with 85 additions and 7 deletions

View File

@@ -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<PsiElement> 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));

View File

@@ -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 -> {}
}
}
}

View File

@@ -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 -> {}
}
}
}

View File

@@ -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<caret>, double y) point1, Point point2) rect) {}
switch (obj) {
case Rect(Point(double x, double y) point1, Point point2) rect<caret> -> {}
default -> {}
}
}
}

View File

@@ -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<caret>, Point point2) rect -> {}
default -> {}
}
}
}

View File

@@ -0,0 +1,9 @@
// "Remove pattern variable" "false"
class X {
public void test(Object object) {
switch (object) {
case String string<caret> -> {}
default -> {}
}
}
}

View File

@@ -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<caret>, double y) point1, Point point2) rect) {}
}
}

View File

@@ -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<caret>) point1, Point point2) rect -> {}
default -> {}
}
}
}