mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[java-inspections] IDEA-326718 skip not reassigned variables
GitOrigin-RevId: acdfa3cd6fd701f6a61ca0a461d179b1bda107c7
This commit is contained in:
committed by
intellij-monorepo-bot
parent
85cc6d5895
commit
2c12bf0ee5
+6
-2
@@ -111,11 +111,15 @@ public class DefUseInspection extends AbstractBaseJavaLocalInspectionTool {
|
||||
context instanceof PsiPostfixExpression && REPORT_POSTFIX_EXPRESSIONS) {
|
||||
holder.registerProblem(context, JavaBundle.message("inspection.unused.assignment.problem.descriptor4"));
|
||||
}
|
||||
else if (REPORT_PATTERN_VARIABLE && psiVariable instanceof PsiPatternVariable) {
|
||||
else if (REPORT_PATTERN_VARIABLE && psiVariable instanceof PsiPatternVariable &&
|
||||
//case is covered with `Java | Declaration redundancy | Unused declaration`
|
||||
info.isWriteOutsideDeclaration()) {
|
||||
holder.registerProblem(psiVariable.getNameIdentifier(), JavaBundle.message("inspection.unused.assignment.problem.descriptor5"));
|
||||
}
|
||||
else if (REPORT_FOR_EACH_PARAMETER && context instanceof PsiForeachStatement foreachStatement &&
|
||||
foreachStatement.getIterationParameter() == psiVariable && psiVariable.getNameIdentifier() != null) {
|
||||
foreachStatement.getIterationParameter() == psiVariable && psiVariable.getNameIdentifier() != null &&
|
||||
//case is covered with `Java | Declaration redundancy | Unused declaration`
|
||||
info.isWriteOutsideDeclaration()) {
|
||||
holder.registerProblem(psiVariable.getNameIdentifier(), JavaBundle.message("inspection.unused.assignment.problem.descriptor6"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,10 +31,13 @@ public final class DefUseUtil {
|
||||
private final PsiElement myContext;
|
||||
private final boolean myIsRead;
|
||||
|
||||
public Info(@NotNull PsiVariable variable, @NotNull PsiElement context, boolean read) {
|
||||
private final boolean myWriteOutsideDeclaration;
|
||||
|
||||
public Info(@NotNull PsiVariable variable, @NotNull PsiElement context, boolean read, boolean writeOutsideDeclaration) {
|
||||
myVariable = variable;
|
||||
myContext = context;
|
||||
myIsRead = read;
|
||||
myWriteOutsideDeclaration = writeOutsideDeclaration;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -50,6 +53,9 @@ public final class DefUseUtil {
|
||||
public boolean isRead() {
|
||||
return myIsRead;
|
||||
}
|
||||
public boolean isWriteOutsideDeclaration() {
|
||||
return myWriteOutsideDeclaration;
|
||||
}
|
||||
}
|
||||
|
||||
private static class InstructionState implements Comparable<InstructionState> {
|
||||
@@ -143,6 +149,7 @@ public final class DefUseUtil {
|
||||
}
|
||||
|
||||
Set<PsiVariable> assignedVariables = new HashSet<>();
|
||||
Set<PsiVariable> assignedSeveralTimes = new HashSet<>();
|
||||
Set<PsiVariable> readVariables = new HashSet<>();
|
||||
for (int i = 0; i < instructions.size(); i++) {
|
||||
Instruction instruction = instructions.get(i);
|
||||
@@ -153,7 +160,9 @@ public final class DefUseUtil {
|
||||
context = PsiTreeUtil.getParentOfType(context, PsiStatement.class, false);
|
||||
PsiVariable psiVariable = writeInstruction.variable;
|
||||
if (context != null && !(context instanceof PsiDeclarationStatement && psiVariable.getInitializer() == null)) {
|
||||
assignedVariables.add(psiVariable);
|
||||
if (!assignedVariables.add(psiVariable)) {
|
||||
assignedSeveralTimes.add(psiVariable);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (instruction instanceof ReadVariableInstruction) {
|
||||
@@ -241,11 +250,11 @@ public final class DefUseUtil {
|
||||
if (context != null) {
|
||||
if (context instanceof PsiDeclarationStatement && psiVariable.getInitializer() == null) {
|
||||
if (!assignedVariables.contains(psiVariable)) {
|
||||
unusedDefs.add(new Info(psiVariable, context, false));
|
||||
unusedDefs.add(new Info(psiVariable, context, false, assignedSeveralTimes.contains(psiVariable)));
|
||||
}
|
||||
}
|
||||
else {
|
||||
unusedDefs.add(new Info(psiVariable, context, readVariables.contains(psiVariable)));
|
||||
unusedDefs.add(new Info(psiVariable, context, readVariables.contains(psiVariable), assignedSeveralTimes.contains(psiVariable)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@ class Test {
|
||||
arg = "test";
|
||||
System.out.println(arg);
|
||||
}
|
||||
for (String ignored : args) {
|
||||
}
|
||||
|
||||
for (String arg : args) {
|
||||
if (args.length == 1) {
|
||||
|
||||
@@ -4,6 +4,8 @@ class Test {
|
||||
s = "hello"; // not reported
|
||||
System.out.println(s);
|
||||
}
|
||||
if (object instanceof String ignored) {
|
||||
}
|
||||
|
||||
if (object instanceof R(var x, var <warning descr="The value of pattern variable 'y' is never used">y</warning>)) {
|
||||
if (1 == 1) {
|
||||
|
||||
Reference in New Issue
Block a user