Skip to content

[java] UnnecessaryCast false-positive for integer operations in floating-point context #5523

@toKrause

Description

@toKrause

Affects PMD Version:
7.10.0

Rule:
UnnecessaryCast

Description:
Division (or multiplication) of primitive integer and boxed integer in floating-point context is detected as unnecessary cast.

Code Sample demonstrating the issue:

Code:

class Scratch {
    public static void main(String[] args) {
        long x = 100;
        Integer y = 75;
        System.out.println(Math.ceil((double) x / y)); // <-- Unnecessary cast (double).
        System.out.println(Math.ceil(x / y));
    }
}

Output (the cast in question makes a difference and is therefore not unnecessary):

2.0
1.0

Expected outcome:

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

Running PMD through:
Maven

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions