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] AvoidArrayLoops could also check for list copy through iterated List.add() #2587

Closed
wuchiuwong opened this issue Jun 14, 2020 · 1 comment
Labels
an:enhancement An improvement on existing features / rules
Milestone

Comments

@wuchiuwong
Copy link

wuchiuwong commented Jun 14, 2020

Affects PMD Version:
6.22.0

Rule:
AvoidArrayLoops

Description:
It is recommended to have AvoidArrayLoops additionally check the case using that for loop + list.add() to copy a list
Similar to using for loop to manually copy data between two arrays, using for loop + list.add() to manually copy data between two list can also be simplified to Collections.addAll(dest, source)

Code Sample demonstrating the suggestion:
Example 1:

// can also be simplified to <code>Collections.addAll(rawData, dd)
double[] dd = sc.summaryStats();
for (double d : dd) {
	rawData.add(d);
}

Example 2:

// can also be simplified to <code>Collections.addAll(mbeans, beans)
for (int i = 0; i < beans.length; i++) {
	mbeans.add(beans[i]);
}

Type:
Suggestions for rule improvement

Running PMD through:
CLI

@wuchiuwong wuchiuwong added the an:enhancement An improvement on existing features / rules label Jun 14, 2020
@oowekyala oowekyala changed the title [java]It is recommended to have AvoidArrayLoops additionally check the case that using for loop + list.add() to copy a list [java] AvoidArrayLoops could also check for list copy through iterated List.add() Jun 14, 2020
@adangel adangel added this to the 7.0.0 milestone Dec 11, 2020
@adangel
Copy link
Member

adangel commented Dec 11, 2020

Fixed via #2899 for PMD 7.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
an:enhancement An improvement on existing features / rules
Projects
None yet
Development

No branches or pull requests

2 participants