mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-19 21:11:28 +07:00
[java-inspections] LocalCanBeFinal: check class for pattern variables
IDEA-309250 IJ-CR-100646 GitOrigin-RevId: 42f402c8521e889f9734abeed1363ef82f9bbe22
This commit is contained in:
committed by
intellij-monorepo-bot
parent
866636446f
commit
0ed3d0e811
@@ -1,4 +1,4 @@
|
||||
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
package com.intellij.codeInspection.localCanBeFinal;
|
||||
|
||||
import com.intellij.codeInspection.*;
|
||||
@@ -210,6 +210,9 @@ public class LocalCanBeFinal extends AbstractBaseJavaLocalInspectionTool impleme
|
||||
public void visitPatternVariable(@NotNull PsiPatternVariable variable) {
|
||||
super.visitPatternVariable(variable);
|
||||
if (!REPORT_PATTERN_VARIABLES) return;
|
||||
if (PsiTreeUtil.getParentOfType(variable, PsiClass.class) != PsiTreeUtil.getParentOfType(body, PsiClass.class)) {
|
||||
return;
|
||||
}
|
||||
PsiElement context = PsiTreeUtil.getParentOfType(variable,
|
||||
PsiInstanceOfExpression.class,
|
||||
PsiCaseLabelElementList.class,
|
||||
|
||||
@@ -4,10 +4,69 @@ class Main {
|
||||
|
||||
record Rect(Point point1, Point point2) {
|
||||
}
|
||||
|
||||
void test1(final Object obj) {
|
||||
if (obj instanceof Point(int <warning descr="Parameter 'x' can have 'final' modifier">x</warning>, int y)) {
|
||||
System.out.println(x);
|
||||
y = 42;
|
||||
}
|
||||
}
|
||||
|
||||
void test2(final Object obj) {
|
||||
if (obj instanceof Rect(Point(int <warning descr="Parameter 'x1' can have 'final' modifier">x1</warning>, int y1), Point point2) && (y1 = 42) == x1 && (point2 = null) == null) {
|
||||
System.out.println(x1);
|
||||
}
|
||||
}
|
||||
|
||||
void test3(final Object obj) {
|
||||
switch (obj) {
|
||||
case Rect(Point(int <warning descr="Parameter 'x1' can have 'final' modifier">x1</warning>, int y1), Point point2) when (y1 = 42) == x1 -> {
|
||||
point2 = new Point(0, 0);
|
||||
}
|
||||
default -> {}
|
||||
}
|
||||
}
|
||||
|
||||
void test4(final Object obj) {
|
||||
if (obj instanceof Point(int <warning descr="Parameter 'x' can have 'final' modifier">x</warning>, int y) && (y = 42) == x) {
|
||||
System.out.println(x);
|
||||
}
|
||||
}
|
||||
|
||||
void test5(final Point[] points) {
|
||||
for (Point(int <warning descr="Variable 'x' can have 'final' modifier">x</warning>, int y) : points) {
|
||||
System.out.println(x);
|
||||
y = 42;
|
||||
}
|
||||
}
|
||||
|
||||
void test6(final Rect[] rects) {
|
||||
for (Rect(Point(int x1, int <warning descr="Variable 'y1' can have 'final' modifier">y1</warning>), Point <warning descr="Variable 'point2' can have 'final' modifier">point2</warning>) : rects) {
|
||||
x1 = 42;
|
||||
}
|
||||
}
|
||||
|
||||
void test7(final Object obj) {
|
||||
switch (obj) {
|
||||
case Point point:
|
||||
point = new Point(0, 0);
|
||||
break;
|
||||
case Rect <warning descr="Parameter 'rect' can have 'final' modifier">rect</warning>:
|
||||
System.out.println("rectangle");
|
||||
break;
|
||||
default:
|
||||
System.out.println("default");
|
||||
}
|
||||
}
|
||||
|
||||
void test8(final Object obj) {
|
||||
final I i = new I() {
|
||||
void foo() {
|
||||
if (obj instanceof Point(int <warning descr="Parameter 'x' can have 'final' modifier">x</warning>, int <warning descr="Parameter 'y' can have 'final' modifier">y</warning>)) {}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
interface I {}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user