Skip to content

All checks that use AbstractCheck#getLine() should check code points instead of characters for spacing #10924

Open

Description

@nrmancuso

Affected checks:
https://checkstyle.sourceforge.io/config_misc.html#CommentsIndentation
https://checkstyle.sourceforge.io/config_blocks.html#RightCurly
https://checkstyle.sourceforge.io/config_blocks.html#LeftCurly
https://checkstyle.sourceforge.io/config_whitespace.html#GenericWhitespace
https://checkstyle.sourceforge.io/config_whitespace.html#NoWhitespaceBefore
https://checkstyle.sourceforge.io/config_whitespace.html#SingleSpaceSeparator
https://checkstyle.sourceforge.io/config_whitespace.html#NoWhitespaceAfter

➜  checks git:(issue-10920) grep -R 'getLine(' | grep -v "indentCheck\|\.getLine(\|int"
indentation/CommentsIndentationCheck.java:        final String targetSourceLine = getLine(singleLineComment.getLineNo() - 1);
indentation/CommentsIndentationCheck.java:        final String commentLine = getLine(blockComment.getLineNo() - 1);
blocks/RightCurlyCheck.java:        else if (shouldBeAloneOnLine(option, details, getLine(details.rcurly.getLineNo() - 1))) {
blocks/LeftCurlyCheck.java:        final String braceLine = getLine(brace.getLineNo() - 1);
whitespace/WhitespaceAfterCheck.java:            final String line = getLine(targetAST.getLineNo() - 1);
whitespace/WhitespaceAfterCheck.java:            final String line = getLine(ast.getLineNo() - 1);
whitespace/GenericWhitespaceCheck.java:        final String line = getLine(ast.getLineNo() - 1);
whitespace/GenericWhitespaceCheck.java:        final String line = getLine(ast.getLineNo() - 1);
whitespace/NoWhitespaceBeforeCheck.java:        final String line = getLine(ast.getLineNo() - 1);
whitespace/WhitespaceAroundCheck.java:            final String line = getLine(ast.getLineNo() - 1);
whitespace/SingleSpaceSeparatorCheck.java:                            getLine(currentNode.getLineNo() - 1),
whitespace/NoWhitespaceAfterCheck.java:        final String line = getLine(astLineNo - 1);

Noticed in #10837 and #10920, any check where we use AbstractCheck#getLine() (extracting a line of code from a source file) should be converted to using code points instead of characters. Now that Checkstyle supports Unicode in variable names and counts Unicode surrogate pairs correctly, we should make sure that all affected checks use code points instead of character arrays to ensure that we are indexing the correct character.

Activity

  1. nrmancuso commented on Nov 4, 2021

    @nrmancuso
    ContributorAuthor

    Another option to consider is to have AbstractCheck#getLine() return an int array of code points, but this would be a significant breaking change for users.

  2. rnveach commented on Nov 4, 2021

    @rnveach
    Contributor

    Why not add a getCodePoints() method?

  3. nrmancuso commented on Nov 4, 2021

    @nrmancuso
    ContributorAuthor

    Why not add a getCodePoints() method?

    In AbstractCheck ? Great idea.

  4. MUzairS15 commented on Nov 16, 2021

    @MUzairS15
    Contributor

    Can I work on this?

  5. nrmancuso commented on Nov 16, 2021

    @nrmancuso
    ContributorAuthor

    @MUzairS15 sure, please be aware that we will need to generate check regression reports for all checks listed in the issue description.

  6. MUzairS15 commented on Nov 17, 2021

    @MUzairS15
    Contributor

    Thank you for your response.

  7. MUzairS15 commented on Nov 20, 2021

    @MUzairS15
    Contributor

    @nmancus1 Need help.
    I created a method getCodePoint() in AbstractCheck, then I tried doing the required change but when I am running the tests related to that files, they are failing.
    For eg: GenericWhiteSpaceCheck and GenericWhiteSpaceCheckTest 3 of 11 checks are failing.
    Should I create a draft PR, to let you take a look at my changes?

  8. nrmancuso commented on Nov 22, 2021

    @nrmancuso
    ContributorAuthor

    @MUzairS15 circling back on this issue, I see that you edited your comment.

    Should I create a draft PR, to let you take a look at my changes?

    Yes, please do.

  9. added a commit that references this issue on Nov 23, 2021
  10. MUzairS15 commented on Nov 23, 2021

    @MUzairS15
    Contributor

    @MUzairS15 circling back on this issue, I see that you edited your comment.

    Should I create a draft PR, to let you take a look at my changes?

    Yes, please do.

    I have created a draft PR, pls check when you find time,
    Yes, I edited because I thought I will manage but I was not able to.

  11. nrmancuso commented on Nov 23, 2021

    @nrmancuso
    ContributorAuthor

    @MUzairS15 a good strategy for this update might be:

    1. Create AbstractCheck#getCodePoints
    2. Make changes in already updated checks (WhitespaceAround, NoWhitespaceBefore) to use this method
    3. Update checks that require simpler changes, such as *CurlyChecks and SingleSpaceSeparatorCheck
    4. GenericWhitespaceCheck seems to be the most complicated update, so I would do this last.

    This process may help to give you a better feel for the reason why we are doing this update, and provide some insight that will be helpful when you return to GenericWhitespaceCheck.

    Here is an article(and additional links) from StackOverflow that helped me to better understand code points: https://stackoverflow.com/questions/23979676/java-what-are-characters-code-points-and-surrogates-what-difference-is-there/23980020 ; perhaps this can also aid you in your work on this issue.

  12. added 2 commits that reference this issue on Nov 26, 2021
  13. nrmancuso commented on Dec 4, 2021

    @nrmancuso
    ContributorAuthor

    We have missed a bunch of instances where we use getLines()[i], which has the same effect: #11001

    ➜  checks git:(master) grep -R 'getLines(' | grep -v "indentCheck\|\.getLine(\|int"
    AvoidEscapedUnicodeCharactersCheck.java:                final String line = getLines()[lineNo - 1];
    indentation/ArrayInitHandler.java:        final String line = getIndentCheck().getLines()[lineNo - 1];
    indentation/CommentsIndentationCheck.java:        final String[] lines = getLines();
    indentation/CommentsIndentationCheck.java:        final char[] line = getLines()[lineNo - 1].toCharArray();
    indentation/AnnotationArrayInitHandler.java:        final String line = getIndentCheck().getLines()[lineNo - 1];
    TrailingCommentCheck.java:        final String line = getLines()[lineNo - 1];
    TrailingCommentCheck.java:        String line = getLines()[lineNo - 1];
    TrailingCommentCheck.java:        final String lineBefore = getLines()[lineNo - 1].substring(0, ast.getColumnNo());
    blocks/EmptyBlockCheck.java:        final String[] lines = getLines();
    imports/CustomImportOrderCheck.java:        final String[] lines = getLines();
    coding/FallThroughCheck.java:        final String[] lines = getLines();
    whitespace/MethodParamPadCheck.java:            final String line = getLines()[parenAST.getLineNo() - 1];
    whitespace/EmptyForIteratorPadCheck.java:            final String line = getLines()[semi.getLineNo() - 1];
    whitespace/EmptyLineSeparatorCheck.java:            final String prePreviousLine = getLines()[lineNo - number];
    whitespace/EmptyLineSeparatorCheck.java:            final String lineBefore = getLines()[lineNo - 2];
    whitespace/EmptyLineSeparatorCheck.java:            final String lineWithComment = getLines()[comment.getLineNo() - 1].trim();
    whitespace/SeparatorWrapCheck.java:        final String currentLine = getLines()[lineNo - 1];
    whitespace/AbstractParenPadCheck.java:        final String line = getLines()[ast.getLineNo() - 1];
    whitespace/AbstractParenPadCheck.java:            final String line = getLines()[ast.getLineNo() - 1];
    whitespace/EmptyForInitializerPadCheck.java:            final String line = getLines()[semiLineIdx];
    

    These checks will also need to be updated.

  14. 258 remaining items

  15. romani commented on Nov 1, 2026

    @romani
    Member

    refactored to CodePointUtil.hasWhitespaceBefore for consistency

    yes, it will help to force others and New checks to use proper aproach.

  16. Krish-876 commented on Nov 2, 2026

    @Krish-876
    Contributor

    Thanks, I'll send a PR for 1.

  17. added a commit that references this issue on Nov 2, 2026
  18. sarabbose commented on Nov 10, 2026

    @sarabbose
    Contributor

    I am working on the following remaining #10924 slices:

    • ClassHeaderWrapOpenjdkCheck
    • JavadocLeadingAsteriskAlignCheck

    I reproduced both on current master with supplementary Unicode:

    • ClassHeaderWrapOpenjdkCheck: false positive in the wrapped-header decision due to AST code-point columns being used with UTF-16 string slicing/length calculation.
    • JavadocLeadingAsteriskAlignCheck: false alignment violations due to a code-point column being used with UTF-16 charAt.

    I’ll handle these as separate focused changes/PRs.

  19. arshil42 commented on Nov 11, 2026

    @arshil42
    Contributor

    I am on JavadocUtilizingTrailingSpaceCheck: it adds the code point column of a Javadoc node to the UTF-16 length of its text, so a supplementary character counts twice. A line of exactly lineLimit code points with an emoji is reported as too long (found 81 for 80), and an under-utilized line is not reported when the next line with an emoji would fit. Reproduced on current master; I will measure the content in code points.

  20. arshil42 commented on Nov 11, 2026

    @arshil42
    Contributor

    I am on NoWhitespaceBeforeCaseDefaultColonCheck: it adds the code point column of the last token before the colon to the UTF-16 length of its text, so case "😀": and case 𝒜: (enum constant) are reported as ':' preceded with whitespace although there is none. Reproduced on current master.

  21. arshil42 commented on Nov 11, 2026

    @arshil42
    Contributor

    I am on NoWhitespaceAfterCheck: getPositionAfter adds the UTF-16 length of the token text to its code point column, so after an identifier with supplementary characters it looks at the wrong position. int 𝒜 []; is not reported, and int 𝒜𝒜[]; is reported although it has no whitespace. Reproduced on current master.

  22. arshil42 commented on Nov 11, 2026

    @arshil42
    Contributor

    I am on PreferLiteralJavadocInlineTagCheck: it logs an HTML entity at the text node's code point column plus the UTF-16 index from String.indexOf, so the column is too far right when the text before the entity has supplementary characters (Emoji 😀 text < is reported one column late, 𝒜𝒜 < two columns late). Reproduced on current master.

  23. added a commit that references this issue on Nov 11, 2026
  24. arshil42 commented on Nov 11, 2026

    @arshil42
    Contributor

    I am on RegexpCheck: with ignoreComments = true it compares the match position from FileText.lineColumn (UTF-16 chars) with comment positions (code points), so on a line with supplementary characters a match right before a comment is taken as inside the comment and silently ignored. Reproduced on current master.

  25. arshil42 commented on Nov 11, 2026

    @arshil42
    Contributor

    I am on CommonUtil.lengthExpandedTabs and ApiUtil.lengthExpandedTabs (used by AbstractCheck#log and several checks): they walk UTF-16 chars up to a code point index, so a tab after a supplementary character is not expanded. For example String b = "😀";<TAB>int B1; reports MemberName at column 28 instead of 29. Reproduced on current master.

  26. Hrushikesh-ramilla commented on Nov 12, 2026

    @Hrushikesh-ramilla
    Contributor

    I am on WhitespaceBeforeEmptyBodyCheck: isSpaceBetweenTokens adds firstToken.getColumnNo() (code points) to tokenText.length() (UTF-16 units), so empty bodies after supplementary-character identifiers (such as class 𝒜 {} or interface 𝒞 {}) are falsely reported as missing whitespace before {. Reproduced on current master.

  27. added 2 commits that reference this issue on Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions