[java] EmptyControlStatement should not allow empty try with concise resources #4930
Labels
a:false-negative
PMD doesn't flag a problematic piece of code
Milestone
Affects PMD Version: 7.0.0
Rule: EmptyControlStatement
Description:
Since #432 was closed EmptyControlStatement allows empty try-with-resources, provided the resources are named with a name like
ignored
.I think this should only be done for resources that don't use shorthand form. For instance in our tests:
pmd/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/EmptyControlStatement.xml
Lines 138 to 141 in f87f24e
This looks like an anti pattern to me. If you're going to use an empty try statement for this you should write
in.close()
because it has the same effect but is clearer.On the other hand the following should probably still be allowed:
pmd/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/EmptyControlStatement.xml
Lines 121 to 122 in f87f24e
Here an explicit
ignored
name is given to an expression. Even if it could be writtenthis code would pollute the enclosing scope with a useless variable name.
Another case to keep in mind is the following:
If there is at least one catch block then it should be ok to keep the try block empty, even if the resource is unnamed.
Probably the rule should actually not care about whether the variables are named
ignored
or the like. If the variable is not used it will be flagged by UnusedVariable already.Expected outcome:
PMD should report a violation at line ..., but doesn't. This is a false-negative.
Running PMD through: [CLI | Ant | Maven | Gradle | Designer | Other]
The text was updated successfully, but these errors were encountered: