Skip to content

AstTreeStringPrinter should use 1-based column and consider tab size  #5877

@romani

Description

@romani
$ cat config.xml 
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
          "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
          "http://www.puppycrawl.com/dtds/configuration_1_3.dtd">

<module name="Checker">
    <property name="charset" value="UTF-8"/>

    <module name="TreeWalker">
        <module name="CyclomaticComplexityCheck">
            <property name="max" value="0" />
        </module>
    </module>
</module>

$ cat TestClass.java 
package org.checkstyle.suppressionxpathfilter.cyclomaticcomplexity;

public class SuppressionXpathRegressionCyclomaticOne {
    public void test(int a, int b) { //warn
        if (a > b) {

        } else {

        }
    }

$ java -jar checkstyle-8.10-all.jar -c config.xml TestClass.java 
Starting audit...
[ERROR] TestClass.java:4:5: Cyclomatic Complexity is 2 (max allowed is 0). [CyclomaticComplexity]
Audit done.
Checkstyle ends with 1 errors.

$ java -jar checkstyle-8.10-all.jar -t TestClass.java | grep "4:5"

$ java -jar checkstyle-8.10-all.jar -t TestClass.java | grep "4:4"
    |--METHOD_DEF -> METHOD_DEF [4:4]
    |   |--MODIFIERS -> MODIFIERS [4:4]
    |   |   `--LITERAL_PUBLIC -> public [4:4]

First of all about different column numbers
The reason of this difference, that user messages consider tabs and start with 1, when AstTreeStringPrinter prints 0-based column numbers without calculating tabs.

So If you want numbers to be the same, we should pass tabwidth to AstTreeStringPrinter and little change one line and thats all

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions