Skip to content

[java] UnusedAssignment false-positive with try-with-resources #4127

@boris-petrov

Description

@boris-petrov

Affects PMD Version:
6.49.0

Rule:
UnusedAssignment
https://pmd.sourceforge.io/pmd-6.49.0/pmd_rules_java_bestpractices.html#unusedassignment

Description:
Check the code sample

Code Sample demonstrating the issue:

Lock lock;
synchronized (locks) {
  lock = locks.get(0);
}
try (lock) {
  ...
}

For some type Lock that is AutoCloseable.

Expected outcome:
PMD reports a violation at line lock = locks.get(0);, but that's wrong. That's a false positive.

Running PMD through: Gradle

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positivePMD flags a piece of code that is not problematic

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions