A RetroSearch Logo

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

Search Query:

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

[java] UnnecessaryLocalBeforeReturn - false positive with catch clause · Issue #4239 · pmd/pmd · GitHub

Affects PMD Version:

Rule:
UnnecessaryLocalBeforeReturn: Consider simply returning the value vs storing it in local variable 'e'

Description:
When upgrading to 6.52.0 (from 6.51.0), this incorrectly flagged a caught exception that was returned. This is certainly not the nicest code or a good style, but was necessary for conforming to a service provider's interface. If not for constraints and being one-off code, a Try data type have been a nicer abstraction for this case, but I digress.

Code Sample demonstrating the issue:
A simplified version of the code that flags e as unnecessary.

private @Nullable Exception attempt(Runnable task) {
  try {
    task.run();
    return null;
  } catch (Exception e) {
    return e;
  }
}

Expected outcome:

PMD reports a violation at line return e, but that's wrong. That's a false positive.

Running PMD through: Gradle


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