Branch: refs/heads/main
Home: https://github.com/pmd/pmd
Commit: 305b82017654b4d18030854240f1470e4071207a
https://github.com/pmd/pmd/commit/305b82017654b4d18030854240f1470e4071207a
Author: hexonal <[email protected]>
Date: 2026-09-06 (Sun, 06 Sep 2026)
Changed paths:
M docs/pages/release_notes.md
M
pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/CloneMethodMustImplementCloneableRule.java
M
pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/CloneMethodMustImplementCloneable.xml
Log Message:
-----------
[java] Fix #6693: CloneMethodMustImplementCloneable false positive for
local-var throw (#6885)
* fix(java): CloneMethodMustImplementCloneable false positive for local-var
throw
justThrowsCloneNotSupported() only recognized the exemption when the
clone() body was the single statement "throw new
CloneNotSupportedException();". A clone() that assigns the exception to
a local variable first and then throws it,
CloneNotSupportedException e = new CloneNotSupportedException();
throw e;
is semantically identical but has a body of size 2, so the check failed
and the rule reported a violation even though the class still doesn't
implement Cloneable on purpose.
Add throwsLocalCloneNotSupported() to recognize this two-statement form:
the first statement must declare a single local variable of type
CloneNotSupportedException, and the second must throw that same
variable.
Fixes #6693
* fix(java): generalize CloneMethodMustImplementCloneable exemption
Walk the whole clone() method body instead of pattern-matching one
specific two-statement shape. Exempt bodies with no return statement
where every throw is (a subtype of) CloneNotSupportedException and at
least one throw is present, so a no-op override isn't wrongly exempted.
* [java] Add test for multi-branch throw form of
CloneMethodMustImplementCloneable
Covers a clone() body where an if/else has a CloneNotSupportedException
throw in each branch (no fall-through, no single unconditional throw),
exercising the "every throw statement" part of the broadened exemption
in a shape not covered by the existing tests.
* Make the no-throw test case compile
The snippet added for "no throw statement at all is not exempted" didn't
compile, on two counts: the method was package-private, which assigns weaker
access than the inherited protected Object.clone(), and the body fell off the
end of a method declared to return Object.
`protected final Object clone()` with a `while (true)` body fixes both while
keeping the case exactly as pointed: no return statement, no throw statement,
non-Cloneable class, so the exemption's "at least one throw" condition is what
decides it. Still reports 1.
Leaves the pre-existing `void clone()` cases alone -- those predate this PR and
zbynek offered to handle them separately.
* Move new tests to the end.
Remove misleading test.
---------
Co-authored-by: Sören Glimm <[email protected]>
To unsubscribe from these emails, change your notification settings at
https://github.com/pmd/pmd/settings/notifications
_______________________________________________
Pmd-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/pmd-commits