Skip to content

Commit 9cf985d

Browse files
SONARJAVA-5504 Documentation to explain the behavior of the UnusedLocalVariableCheck in unresolved variable cases (#5097)
1 parent c8ef9f9 commit 9cf985d

2 files changed

Lines changed: 3 additions & 1 deletion

File tree

java-checks/src/main/java/org/sonar/java/checks/helpers/UnresolvedIdentifiersVisitor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
public class UnresolvedIdentifiersVisitor extends BaseTreeVisitor {
3131

32-
private Set<String> unresolvedIdentifierNames = new HashSet<>();
32+
private final Set<String> unresolvedIdentifierNames = new HashSet<>();
3333

3434
@Override
3535
public void visitMemberSelectExpression(MemberSelectExpressionTree tree) {
@@ -72,6 +72,7 @@ private Set<String> unresolvedNames() {
7272
return Collections.unmodifiableSet(unresolvedIdentifierNames);
7373
}
7474

75+
/** Returns whether an identifier with the same name as the candidate is unresolved. */
7576
public boolean isUnresolved(String candidate) {
7677
return unresolvedIdentifierNames.contains(candidate);
7778
}

java-checks/src/main/java/org/sonar/java/checks/unused/UnusedLocalVariableCheck.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public void leaveNode(Tree tree) {
7474
VariableTree variable = (VariableTree) tree;
7575
IdentifierTree simpleName = variable.simpleName();
7676
if (!simpleName.isUnnamedVariable()) {
77+
// If a variable with the same name as this is unresolved, we can't be sure it's not a usage of this. So we can't raise an issue.
7778
boolean unresolved = UNRESOLVED_IDENTIFIERS_AND_SWITCH_CASE_VISITOR.isUnresolved(simpleName.name());
7879
if (!unresolved && isProperLocalVariable(variable) && isUnused(variable.symbol()) && canBeReplaced(variable)) {
7980
QuickFixHelper.newIssue(context)

0 commit comments

Comments
 (0)