Skip to content

[java] PrematureDeclaration rule false-positive on variable declared to measure time #1208

@mkordas

Description

@mkordas

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positivePMD flags a piece of code that is not problematic

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions