Skip to content

[java] CloneMethodMustImplementCloneable: False positive with throw-via-local #6693

Closed

Description

@Chordrain

Affects PMD Version: 7.24.0

Rule: CloneMethodMustImplementCloneable — https://docs.pmd-code.org/latest/pmd_rules_java_errorprone.html#clonemethodmustimplementcloneable

Description:

The CloneMethodMustImplementCloneable rule flags classes that define a clone() method but do not implement the Cloneable interface. However, the rule correctly exempts classes that explicitly disallow cloning by immediately throwing a CloneNotSupportedException.

Currently, this exemption logic fails (resulting in a False Positive) if the CloneNotSupportedException is first assigned to a local variable and then thrown. Even though the semantics are identical to throwing the exception inline, PMD fails to recognize the pattern and incorrectly reports a violation on the throw e; line.

Code Sample demonstrating the issue:

public class SingletonClone {
    private SingletonClone() {}

    // Case 1: Correctly ignored by PMD (No violation)
    @Override
    protected Object clone() throws CloneNotSupportedException {
        throw new CloneNotSupportedException("Clone not supported");
    }

    // Case 2: Incorrectly flagged (False Positive)
    @Override
    protected Object clone() throws CloneNotSupportedException {
        CloneNotSupportedException e = new CloneNotSupportedException("Clone not supported");
        throw e; 
    }
}

Expected outcome: PMD should not report a violation for Case 2, as it explicitly throws CloneNotSupportedException to disable cloning, matching the behavior of Case 1. This is a false-positive.

Running PMD through: CLI

Activity

  1. added
    a:false-positive
    on May 17, 2026
  2. changed the title [-][java] CloneMethodMustImplementCloneable fires inconsistently between inline `throw new` and throw-via-local forms[/-] [+][java] CloneMethodMustImplementCloneable: False positive with throw-via-local form[/+] on Aug 14, 2026
  3. changed the title [-][java] CloneMethodMustImplementCloneable: False positive with throw-via-local form[/-] [+][java] CloneMethodMustImplementCloneable: False positive with throw-via-local[/+] on Aug 14, 2026
  4. adangel commented on Aug 14, 2026

    @adangel
    Member

    Hi @Chordrain , can you expand a bit on your problem? We are scratching heads over there (#6885) over the correct solution...

    Why are you using a local variable instead of directly throwing? While it is semantically identical, it is not what the rule requires - "only throw CloneNotSupportedException".

    How does your complete class/real case look?

    Depending on your answer, this might not even be bug, but works as designed.

    /cc @hexonal @zbynek @UncleOwen

  5. added this to the 7.28.0 milestone on Sep 6, 2026
  6. added a commit that references this issue on Sep 6, 2026
    305b820
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positive

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions