A RetroSearch Logo

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

Search Query:

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

[java] EmptyControlStatement should not allow empty try with concise resources · Issue #4930 · pmd/pmd · GitHub

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:

void method(InputStream in) { try (in) { } }

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:

try (ClientResponse ignored = execute(() -> target.request(mediaTypes).delete(), DELETE, new ExpectedResponse(status, required))) { }

Here an explicit ignored name is given to an expression. Even if it could be written

ClientResponse response = execute(() -> target.request(mediaTypes).delete(), DELETE, new ExpectedResponse(status, required)));
response.close();

this code would pollute the enclosing scope with a useless variable name.

Another case to keep in mind is the following:

try (in) {
} catch (Foo e) {
  // do something
}

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]


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