Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[java] UnnecessaryLocalBeforeReturn - false positive with catch clause #4239

Closed
ben-manes opened this issue Nov 26, 2022 · 2 comments
Closed
Labels
a:false-positive PMD flags a piece of code that is not problematic
Milestone

Comments

@ben-manes
Copy link

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

@ben-manes ben-manes added the a:false-positive PMD flags a piece of code that is not problematic label Nov 26, 2022
@adangel adangel changed the title UnnecessaryLocalBeforeReturn - false positive with catch clause [java] UnnecessaryLocalBeforeReturn - false positive with catch clause Jan 19, 2023
@kaleemsagard
Copy link

kaleemsagard commented Jul 24, 2023

Hi. Is there a workaround to handle this issue until we get the final solution? I have exactly the same scenario as @ben-manes.

@ben-manes
Copy link
Author

You can annotate the method with @SuppressWarnings("PMD.UnnecessaryLocalBeforeReturn").

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:false-positive PMD flags a piece of code that is not problematic
Projects
None yet
Development

No branches or pull requests

4 participants