-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Comments
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. |
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. |
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:
Steps to reproduce:
Please provide detailed steps for how we can reproduce the bug.
The text was updated successfully, but these errors were encountered: