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] PrematureDeclaration rule false-positive on variable declared to measure time #1208

Closed
mkordas opened this issue Jun 25, 2018 · 2 comments · Fixed by #3199
Closed
Labels
a:false-positive PMD flags a piece of code that is not problematic
Projects
Milestone

Comments

@mkordas
Copy link
Contributor

mkordas commented Jun 25, 2018

Affects PMD Version:
6.4.0

Rule:
PrematureDeclaration

Description:
PrematureDeclaration shouldn't trigger on classes which are specific to time measurement. E.g. it fails when stopwatch is used to measure time to invoke entire method (and I'm interested only in sunny-day scenario when exit points are not triggered).

Code Sample demonstrating the issue:

Stopwatch stopwatch = Stopwatch.createStarted();
// some code with exit points
return stopwatch.elapsed(MILLISECONDS);

It fails on first line with [PrematureDeclaration] Avoid declaring a variable if it is unreferenced before a possible exit point.

@Andy-2639
Copy link

Andy-2639 commented Aug 26, 2019

I would even extend this to exclude all calls with side effects

Object o = callWithSideEffects();
// code with exit points
doSomeThing(o);

from this rule. On the one hand, system calls (like reading time) cannot be moved without changing the behavior of the program. On the other hand, callWithSideEffects can do some work/change some state which is needed for the execution of code with exit points.

@oowekyala oowekyala added the a:false-positive PMD flags a piece of code that is not problematic label Apr 4, 2021
oowekyala added a commit to oowekyala/pmd that referenced this issue Apr 5, 2021
@oowekyala oowekyala added this to Done in PMD 7 Apr 5, 2021
@oowekyala oowekyala added this to the 7.0.0 milestone Apr 5, 2021
@adangel adangel linked a pull request Apr 10, 2021 that will close this issue
4 tasks
@adangel adangel mentioned this issue Jan 23, 2023
55 tasks
@adangel
Copy link
Member

adangel commented Apr 22, 2023

This has been fixed with PMD 7.0.0-rc1.

@adangel adangel closed this as completed Apr 22, 2023
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
No open projects
PMD 7
  
Done
Development

Successfully merging a pull request may close this issue.

4 participants