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] [doc] AvoidArrayLoops flags copy assignment in same array as sub-optimal #2692

Closed
linusjf opened this issue Aug 2, 2020 · 2 comments · Fixed by #4159
Closed

[java] [doc] AvoidArrayLoops flags copy assignment in same array as sub-optimal #2692

linusjf opened this issue Aug 2, 2020 · 2 comments · Fixed by #4159
Labels
in:documentation Affects the documentation
Milestone

Comments

@linusjf
Copy link

linusjf commented Aug 2, 2020

Affects PMD Version:
6.25.0

Description:
AvoidArrayLoops flags array copy asignment within loop as sub-optimal. An example can be added how to resolve the violation.

Code Sample demonstrating the issue:

package ds;

public class InsertionSort extends AbstractSort {

  @Override
  protected void sort(long[] a, int length) {
    int in;
    int out;
    resetCounts();

    for (out = 1; out < length; out++) {
      long temp = a[out];
      in = out;
      while (in > 0 && a[in - 1] >= temp) {
        a[in] = a[in - 1];
        --in;
        comparisonCount++;
        copyCount++;
      }
      if (in > 0) comparisonCount++;
      a[in] = temp;
      copyCount++;
    }
  }
}

The optimised loop , no longer flagged by the rule, is as follows:

in = out;
while (in > 0 && a[in - 1] >= temp) {
--in;
++comparisonCount;
++copyCount;
}
System.arraycopy(a, in, a, in + 1, out - in);

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

@linusjf linusjf added the a:bug PMD crashes or fails to analyse a file. label Aug 2, 2020
@linusjf linusjf changed the title [java] False positive: AvoidArrayLoops flags copy assignment in same array as suboptimal [java] False positive: AvoidArrayLoops flags copy assignment in same array as sub-optimal Aug 2, 2020
@linusjf linusjf closed this as completed Aug 3, 2020
@linusjf
Copy link
Author

linusjf commented Aug 3, 2020

This can be reopened as a documentation issue. An example can be added to this rule.

@linusjf linusjf reopened this Aug 3, 2020
@linusjf linusjf changed the title [java] False positive: AvoidArrayLoops flags copy assignment in same array as sub-optimal [java] [documentation] AvoidArrayLoops flags copy assignment in same array as sub-optimal Aug 3, 2020
@oowekyala oowekyala added in:documentation Affects the documentation and removed a:bug PMD crashes or fails to analyse a file. labels Aug 3, 2020
@linusjf
Copy link
Author

linusjf commented Aug 14, 2020

It's not obvious from the examples or the documentation that copying of array elements as assignment should be avoided totally.

http://www.javapractices.com/topic/TopicAction.do?Id=3

@adangel adangel changed the title [java] [documentation] AvoidArrayLoops flags copy assignment in same array as sub-optimal [java] [doc] AvoidArrayLoops flags copy assignment in same array as sub-optimal Oct 14, 2022
adangel added a commit to adangel/pmd that referenced this issue Oct 14, 2022
@adangel adangel added this to the 6.51.0 milestone Oct 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in:documentation Affects the documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants