Java: Unused Declaration - recognize operator assignment as read access (IDEA-330149)

GitOrigin-RevId: 5561bd503c9f7bfe0fc5e6eca209d0f272ed50ec
This commit is contained in:
Bas Leijdekkers
2023-11-10 14:43:25 +01:00
committed by intellij-monorepo-bot
parent 9ca17ef673
commit 6631881072
4 changed files with 33 additions and 15 deletions

View File

@@ -645,19 +645,14 @@ public class RefJavaUtilImpl extends RefJavaUtil {
return Integer.compare(getAccessNumber(a1), getAccessNumber(a2));
}
private static int getAccessNumber(String a) {
if (PsiModifier.PRIVATE.equals(a)) {
return 0;
}
if (PsiModifier.PACKAGE_LOCAL.equals(a)) {
return 1;
}
if (PsiModifier.PROTECTED.equals(a)) {
return 2;
}
if (PsiModifier.PUBLIC.equals(a)) return 3;
return -1;
private static int getAccessNumber(String modifier) {
return switch (modifier) {
case PsiModifier.PRIVATE -> 0;
case PsiModifier.PACKAGE_LOCAL -> 1;
case PsiModifier.PROTECTED -> 2;
case PsiModifier.PUBLIC -> 3;
default -> -1;
};
}
@Override
@@ -723,7 +718,7 @@ public class RefJavaUtilImpl extends RefJavaUtil {
private static boolean isAccessedForReading(@NotNull UElement expression) {
UElement parent = skipParentheses(expression);
return !(parent instanceof UBinaryExpression binaryExpression) ||
!(binaryExpression.getOperator() instanceof UastBinaryOperator.AssignOperator) ||
binaryExpression.getOperator() != UastBinaryOperator.ASSIGN ||
UastUtils.isUastChildOf(binaryExpression.getRightOperand(), expression, false);
}

View File

@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems/>

View File

@@ -0,0 +1,17 @@
class ShiftParam {
static int getDepth(int parallelism, int size) {
int depth = 0;
while ((parallelism >>= 3) > 0 && (size >>= 2) > 0) {
depth -= 2;
}
return depth;
}
}
class Run {
public static void main(String[] args) {
System.out.println(ShiftParam.getDepth(15, 80));
}
}

View File

@@ -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.java.codeInspection;
import com.intellij.JavaTestUtil;
@@ -45,6 +45,10 @@ public class UnusedMethodParameterTest extends JavaInspectionTestCase {
doTest();
}
public void testMethodParametersOperatorAssignment() {
doTest();
}
public void testEntryPointUnusedParameter() {
doTest("unusedMethodParameter/" + getTestName(true), new UnusedDeclarationInspection(), true, true);
}