You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[java] CloneMethodMustImplementCloneable: False positive with throw-via-local #6693
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:
publicclassSingletonClone {
privateSingletonClone() {}
// Case 1: Correctly ignored by PMD (No violation)@OverrideprotectedObjectclone() throwsCloneNotSupportedException {
thrownewCloneNotSupportedException("Clone not supported");
}
// Case 2: Incorrectly flagged (False Positive)@OverrideprotectedObjectclone() throwsCloneNotSupportedException {
CloneNotSupportedExceptione = newCloneNotSupportedException("Clone not supported");
throwe;
}
}
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.
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
changed the title [-][java] CloneMethodMustImplementCloneable: False positive with throw-via-local form[/-][+][java] CloneMethodMustImplementCloneable: False positive with throw-via-local[/+]on Aug 14, 2026
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.
Activity
[-][java] CloneMethodMustImplementCloneable fires inconsistently between inline `throw new` and throw-via-local forms[/-][+][java] CloneMethodMustImplementCloneable: False positive with throw-via-local form[/+][-][java] CloneMethodMustImplementCloneable: False positive with throw-via-local form[/-][+][java] CloneMethodMustImplementCloneable: False positive with throw-via-local[/+]adangel commentedon Aug 14, 2026
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
Merge remote-tracking branch 'upstream/main' into fix-clone-method-th…
[java] Fix pmd#6693: CloneMethodMustImplementCloneable false positive…