Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[java] New Rule: AvoidCloningObjectsInLoops #2323

Open
numeralnathan opened this issue Mar 2, 2020 · 0 comments
Open

[java] New Rule: AvoidCloningObjectsInLoops #2323

numeralnathan opened this issue Mar 2, 2020 · 0 comments
Labels
a:new-rule Proposal to add a new built-in rule

Comments

@numeralnathan
Copy link

numeralnathan commented Mar 2, 2020

Affects PMD Version: 6.21.0

Rule: AvoidCloningObjectsInLoops

Description:

The rule AvoidInstantiatingObjectsInLoops flags a problem if the code creates an object in a loop using the new operator. I suggest creating a rule called AvoidCloningObjectsInLoops that flags a problem if the code calls Cloneable.clone() in a loop since this call will create a new object.

Code Sample demonstrating the issue:

Here is a code sample. The original code was adapted from AvoidInstantiatingObjectsInLoops.

public class Something {
    public static void main( String as[] ) {
        for (int i = 0; i < 10; i++) {
            String copy[] = as.clone(); // Avoid this whenever you can it's really expensive
        }
    }
}

Running PMD through: Eclipse

@adangel adangel added the a:new-rule Proposal to add a new built-in rule label Mar 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:new-rule Proposal to add a new built-in rule
Projects
None yet
Development

No branches or pull requests

2 participants