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 false positive on double assignment #1167

Closed
Androbin opened this issue Jun 1, 2018 · 2 comments · Fixed by #4159
Closed

[java] AvoidArrayLoops false positive on double assignment #1167

Androbin opened this issue Jun 1, 2018 · 2 comments · Fixed by #4159
Labels
a:false-positive PMD flags a piece of code that is not problematic
Milestone

Comments

@Androbin
Copy link

Androbin commented Jun 1, 2018

Affects PMD Version: 5.8.0

Rule: AvoidArrayLoops

Description:
The rule sees an assignment to foo[ i ] and an add-assignment from foo[ i ].
The rule thinks it sees an assignment from foo[ i ] to foo[ i ].

Code Sample demonstrating the issue:

public class Test {
  public static void main( String[] args ) {
    double[] foo = new double[ 100 ];
    double bar = 0.0;
    
    for ( int i = 0; i < exps.length; i++ ) {
      double value = Math.random();
      foo[ i ] = Math.exp( value );
      bar += foo[ i ];
    }
  }
}

Running PMD through: Eclipse Plugin 4.0.15

@jsotuyod jsotuyod added the a:false-positive PMD flags a piece of code that is not problematic label Jun 1, 2018
@linusjf
Copy link

linusjf commented Jul 28, 2019

package pmdtests;

import java.util.Random;

public enum ArrayCopy {
  ;
  private static String[] words = {"the",
                            "hello",
                            "goodbye",
                            "packt",
                            "java",
                            "thread",
                            "pool",
                            "random",
                            "class",
                            "main"};

  public static void main(String[] args) {
    String[][] document = new String[10][100];
    Random random = new Random();

    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 100; j++) {
        int index = random.nextInt(words.length);
        document[i][j] = words[index];
      }
    }
  }
}

False positive for the above code as well.
PMD version: 6.16

@Java8753
Copy link

another false positive similar to these, which solarlint correctly ignores:
(eclipse plugin: PMD Plug-in 4.39.0.v20220930-1439)

    for ( i = 1 ; i < 10 ; i++ )
    {
        temp [ i ] += temp [ i-1 ] ;
    }

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
a:false-positive PMD flags a piece of code that is not problematic
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants