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] Inconsistent line range in RuleViolation for CognitiveComplexity rule between PMD 6.54.0 and 7.0.0-rc1 #4439

Closed
chenlingmin opened this issue Mar 28, 2023 · 2 comments
Labels
a:bug PMD crashes or fails to analyse a file.

Comments

@chenlingmin
Copy link

chenlingmin commented Mar 28, 2023

Affects PMD Version: 6.54.0 and 7.0.0-rc1

Description:

When using the CognitiveComplexity rule for Java in PMD 6.54.0, the RuleViolation returns the line range covering the entire method, from the start to the end (e.g., 3-7 in the provided example). However, when using PMD 7.0.0-rc1, it only returns the line number of the method signature (e.g., 3-3 in the provided example).

Code Sample demonstrating the issue:

public class Example {

    public void sampleMethod() { // Line 3
        if (true) {
            System.out.println("Hello, world!");
        }
    } // Line 7
}

Steps to reproduce:

Please provide detailed steps for how we can reproduce the bug.

  1. Run PMD 6.54.0 with the CognitiveComplexity rule on the provided code sample.
  2. Observe the RuleViolation line range covering the entire method (3-7).
  3. Run PMD 7.0.0-rc1 with the CognitiveComplexity rule on the same code sample.
  4. Observe the RuleViolation line range only covering the method signature (3-3).
@chenlingmin chenlingmin added the a:bug PMD crashes or fails to analyse a file. label Mar 28, 2023
@oowekyala
Copy link
Member

This is actually by design, according to #730. Similarly, violations reported on classes now are scoped down to only the class name.

Another reason why pmd 7 behaves that way is that we uphold the principle that a parent AST node textually contains all its children. However in pmd 7, the java node for a method or class declaration contains the annotations on the declaration, contrary to pmd 6. Since annotations are usually placed on lines above the actual declaration, the declaration node's start line would be on the first annotation. This messes with those renderers that only show the first line of the violation, as in that case they would most likely show a line with a single annotation. Many renderers are like that, so they're well served by #730.

@oowekyala oowekyala closed this as not planned Won't fix, can't repro, duplicate, stale Mar 28, 2023
@chenlingmin
Copy link
Author

chenlingmin commented Mar 29, 2023

Would it be possible to provide a design that supports both modes? I have implemented an extension based on PMD that scans the cognitive complexity of code in every git commit, comparing if the cognitive complexity has increased. To do this, I parse the added line numbers in the commit diff and then match them against the line numbers in PMD RuleViolation. For example, in the code snippet:

public class Example {

    public void sampleMethod() { // Line 3
        if (true) {
           + System.out.println("Hello, world!"); // new line line 5
            System.out.println("Hello, world!");
        }
    } // Line 8
}

The range 3-8 should be returned to match the new line at line 5. Therefore, I suggest that supporting both modes would allow for better integration with extensions such as mine.

In addition, I believe that PMD should objectively return the range of lines affected by the detection results. Considering the issue of yellow bars in IDEs such as Eclipse, it may be possible to provide separate adaptations for IDEs.

@adangel adangel changed the title [Java] Inconsistent line range in RuleViolation for CognitiveComplexity rule between PMD 6.54.0 and 7.0.0-rc1 [java] Inconsistent line range in RuleViolation for CognitiveComplexity rule between PMD 6.54.0 and 7.0.0-rc1 Apr 23, 2023
adangel added a commit to adangel/pmd that referenced this issue Apr 23, 2023
adangel added a commit to adangel/pmd that referenced this issue Apr 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:bug PMD crashes or fails to analyse a file.
Projects
None yet
Development

No branches or pull requests

2 participants