Skip to content

[java] CloseResourceRule does not recognize multiple assignment done to resource #2764

@pbrajesh1

Description

@pbrajesh1

Affects PMD Version: PMD ??? (1.2.2 is an invalid version...)

Rule: CloseResourceRule

Description:
The CloseResourceRule does not identify in case the resource variable got re assigned by developer

Code Sample demonstrating the issue:

public class Bar {
    public void withSQL() {
        Connection c = pool.getConnection();
        try {
            // do stuff
            // oops, should not have assgined the connection again to the variable 
            c = pool.getConnection();
        } catch (SQLException ex) {
           // handle exception
        } finally {
            // oops, one connection leak'!
            c.close();
        }
    }  
}

Expected outcome:

  • It is expected that PMD reports a violation of resource re assignment. The other rules DataflowAnomalyAnalysisRule and java/bestpractices/UnusedAssignment is not effective also to detect this .
  • Is PMD missing to report a violation, where there should be one? -> false-negative

Running PMD through: [CLI | Ant | Maven | Gradle | Designer | Other]

Metadata

Metadata

Assignees

Labels

a:false-negativePMD doesn't flag a problematic piece of code

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions