Skip to content

[java] CloneMethodMustImplementCloneable: False positive when Cloneable is a super interface #6345

Closed as not planned

Description

@mebigfatguy

Affects PMD Version: 7.19.0

Rule: CloneMethodMustImplementCloneable

Description:

if a class implements the clone method and implements an interface, and that interface has as a super interface Cloneable, then PMD reports it has failing this rule, when in fact it does inherit from Cloneable

Code Sample demonstrating the issue:

public interface MyC extends Cloneable {
}

public class MyExample implements MyC {
    public MyExample clone() {
           try {
                 return (MyExample) super.clone();
           } catch (CloneNotSupportedException e) {
                 throw new Error();
           }
    }
}

Expected outcome:

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

Running PMD through: [CLI | Ant | Maven | Gradle | Designer | Other]
ant

Activity

  1. added
    a:false-positive
    on Dec 20, 2025
  2. mdhamed238 commented on Dec 28, 2025

    @mdhamed238
    Contributor

    I'm on it.

  3. mdhamed238 commented on Dec 29, 2025

    @mdhamed238
    Contributor

    Hi @mebigfatguy,

    I have verified this code sample against PMD 7.20.0 and can confirm that the issue is no longer reproducible. The rule now correctly identifies the usage, and no false positive is reported.

    I recommend upgrading to the latest version to resolve this.

  4. pzygielo commented on Dec 29, 2025

    @pzygielo
    Contributor

    I recommend upgrading to the latest version to resolve this.

    Just to confirm - at this very moment 7.19.0 IS the latest version, right?

  5. pzygielo commented on Dec 29, 2025

    @pzygielo
    Contributor

    I have verified this code sample against PMD 7.20.0 and can confirm that the issue is no longer reproducible. The rule now correctly identifies the usage, and no false positive is reported.

    Is it possible to clarify which code change in https://github.com/pmd/pmd/releases/tag/pmd_releases%2F7.20.0-SNAPSHOT changed/fixed that?

  6. mdhamed238 commented on Dec 29, 2025

    @mdhamed238
    Contributor

    Hi @pzygielo, sorry for the inconvenience. I downloaded the official release 7.20.0, and this appears to be a confirmed false positive.

    pmd check \
      -d MyExample.java \
      -R category/java/errorprone.xml,category/java/bestpractices.xml \
      -f text
    
    ---
    
    [WARN] Progressbar rendering conflicts with reporting to STDOUT. No progressbar will be shown. Try running with argument -r <file> to output the report to a file instead.
    [WARN] This analysis could be faster, please consider using Incremental Analysis: https://docs.pmd-code.org/latest/pmd_userdocs_incremental_analysis.html
    MyExample.java:5:        MissingOverride:    The method 'clone()' is missing an @Override annotation.
    MyExample.java:9:    PreserveStackTrace:  Thrown exception does not preserve the stack trace of exception 'e' on all code paths

    It turns out that I didn’t reproduce the issue correctly locally. I’ll get back to you as soon as I do

  7. mdhamed238 commented on Dec 29, 2025

    @mdhamed238
    Contributor

    Update: The bug occurs when the interface and class are in separate files.

    MyC.java:

    public interface MyC extends Cloneable {
    }

    MyExample.java:

    public class MyExample implements MyC {
        public MyExample clone() {
            try {
                return (MyExample) super.clone();
            } catch (CloneNotSupportedException e) {
                throw new Error();
            }
        }
    }

    Output:

    MyExample.java:3: CloneMethodMustImplementCloneable: clone() method should be implemented only if implementing Cloneable interface
    

    This is a false positive. The rule fails to recognize that MyExample implements Cloneable transitively through MyC when they're defined in separate files.

    My initial test had both declarations in a single file, which is why I couldn't reproduce it originally. The issue is confirmed in PMD 7.20.0.

    @pzygielo - Is checking interface hierarchies across separate files the expected behavior for this rule, or should it require auxclasspath configuration?

  8. jsotuyod commented on Dec 29, 2025

    @jsotuyod
    Member

    @mdhamed238 are you sure you are providing a complete auxclasspath? the fact it works when using a single source files makes me believe you are not…

    Without a complete auxclasspath, PMD simply can't know what is in MyC, or wether it implements Cloneable or not.

    The original report by @mebigfatguy mentions he is using Ant, which does support the param, but requires manual setting of it + ensuring the code is built before running PMD (unlike Maven and Gradle that manages everything automatically).

  9. mdhamed238 commented on Dec 29, 2025

    @mdhamed238
    Contributor

    @jsotuyod Thanks for the clarification. So --aux-classpath is needed to tell PMD where the compiled classes are, allowing it to resolve the interface hierarchy (MyC → Cloneable) across separate files.

  10. changed the title [-][java] CloneMethodMustImplementCloneable doesn't look for Cloneable as a Super interface[/-] [+][java] CloneMethodMustImplementCloneable: False positive when Cloneable is a super interface[/+] on Jan 16, 2026
  11. UncleOwen commented on Apr 22, 2026

    @UncleOwen
    Member

    This exact feature has been implemented in #1532 on sourceforge and has tests that verify it works.

    I just tried the exact code from the first post in current main and didn't get a warning. Closing as can't reproduce.

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

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions