A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/pmd/pmd/issues/4516 below:

false-negative with try-with-resources · Issue #4516 · pmd/pmd · GitHub

Affects PMD Version: 6.x

Rule: UnusedLocalVariable

Description:

If a try-with-resources defines a local variable but it is not used in the try-block, the rule should flag this variable as unused. If some weird action is happening on creation and closing (because close() is automatically executed by the try-with-resources) and the code can't be removed, then the variable should be named like unused - then it will be ignored by the rule.

Note: This is fixed already with PMD 7.0.0-rc1.
Found via #3123.

Code Sample demonstrating the issue:

import java.io.InputStream;
public class UnusedUsedLocalVar {
    public boolean run() {
        boolean canRead = false;
        try(InputStream resource = open()) { // violation expected
            canRead = true;
        } catch (Throwable ignore) {}
        return canRead;
    }
    private InputStream open() { return null; }
}

Expected outcome:

PMD should report a violation at line 5, but doesn't. This is a false-negative.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4