Skip to content

Issue #11103: Fix Indentation check for lambda in enum constant argum…#18687

Merged
romani merged 1 commit into
checkstyle:masterfrom
vivek-0509:fixIndentationCheckLambda
Jan 25, 2026
Merged

Issue #11103: Fix Indentation check for lambda in enum constant argum…#18687
romani merged 1 commit into
checkstyle:masterfrom
vivek-0509:fixIndentationCheckLambda

Conversation

@vivek-0509

@vivek-0509 vivek-0509 commented Jan 19, 2026

Copy link
Copy Markdown
Member

fixes #11103
fixes #13539

Added handling in LambdaHandler.getIndentImpl() to detect lambdas inside enum constant arguments and calculate their expected indentation based on the enum constant's position.

@vivek-0509 vivek-0509 marked this pull request as draft January 19, 2026 13:24
@vivek-0509

Copy link
Copy Markdown
Member Author

Github, generate report for Indentation/all-examples-in-one

@vivek-0509 vivek-0509 force-pushed the fixIndentationCheckLambda branch from 016e96a to 3fd9df1 Compare January 19, 2026 14:09
@github-actions

Copy link
Copy Markdown
Contributor

@vivek-0509

Copy link
Copy Markdown
Member Author

Investigating the cases

@vivek-0509

Copy link
Copy Markdown
Member Author

Hi @romani
I have reviewed the diff report and believe the fix is working correctly. The newly flagged violations appear to be genuine indentation issues that were previously being missed due to the bug in LambdaHandler.getIndentImpl().

For example, the new violation in https://checkstyle-diff-reports.s3.us-east-2.amazonaws.com/016e96a76d3ef484bf86b85a78df95e7cb1034fd_2026143035/reports/diff/openjdk25/index.html#A1:

        FILE_ASSOCIATIONS ("file-associations",
                OptionCategories.PROPERTY, () -> {
            Map<String, ? super Object> args = new HashMap<>();  // column 12

The lambda () -> { appears inline after OptionCategories.PROPERTY which starts at column 16. Therefore, the lambda body should be at 16+4=20, but it's at column 12. This is genuinely incorrect indentation that was being overlooked before.

The current fix uses ENUM_CONSTANT_DEF as the base, which calculates expected body indentation as 16 (enum constant 8 + lineWrapping 4 + basicOffset 4). For inline lambdas, this is slightly lower than the visually ideal 20. However, with forceStrictCondition=false (default), both 16 and 20 are acceptable. Once we align on this fix and confirm there are no false positives, I can explore adding stricter handling for inline lambdas as a follow up enhancement.

Please review the diff report and let me know if any of the newly flagged violations appear to be false positives or if I am missing something.

@vivek-0509 vivek-0509 marked this pull request as ready for review January 19, 2026 21:28
@vivek-0509

Copy link
Copy Markdown
Member Author

Additionally, the fix also resolves false positives for correctly indented code.
I tested a block lambda with statements in an enum constant on the master branch

public enum TestLambdaEnumBlock {
    VALUE(
        () -> {
            System.out.println("test");
        }
    );

    TestLambdaEnumBlock(Runnable runnable) {
    }
}

Result on master (without fix):

Starting audit...
[ERROR] TestLambdaEnumBlock.java:4:9: 'lambda arguments' has incorrect indentation level 8, expected level should be 12. [Indentation]
[ERROR] TestLambdaEnumBlock.java:5:13: 'block' child has incorrect indentation level 12, expected level should be 16. [Indentation]
[ERROR] TestLambdaEnumBlock.java:6:9: 'block rcurly' has incorrect indentation level 8, expected level should be 12. [Indentation]
Audit done.
Checkstyle ends with 3 errors.

However, this code Is correctly indented:

  • Lambda at column 8 = enum constant (4) + lineWrappingIndentation (4)
  • Body at column 12 = lambda (8) + basicOffset (4)
  • Closing brace at column 8 = matches lambda

This example shows that the original code produces false positives for block lambdas with statements in enum constants.

@romani

romani commented Jan 20, 2026

Copy link
Copy Markdown
Member

Please trigger diff test on all examples config

@vivek-0509

vivek-0509 commented Jan 20, 2026

Copy link
Copy Markdown
Member Author

@romani I have already triggered Github, generate report for Indentation/all-examples-in-one.
Here is the report link: https://checkstyle-diff-reports.s3.us-east-2.amazonaws.com/016e96a76d3ef484bf86b85a78df95e7cb1034fd_2026143035/reports/diff/index.html

Could you please clarify which specific config you'd like me to test with? Is there a different command I should use?

@vivek-0509

Copy link
Copy Markdown
Member Author

@romani ping

@romani romani left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good, I did not notice diff report, all good.
thanks a lot.

@romani romani merged commit 0cc1e73 into checkstyle:master Jan 25, 2026
119 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Checkstyle requires inconsistent indentation for lambda arguments Indentation: Lambda in Enum causes error loop

2 participants