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

yield fails Indentation check in switch expressions #15967

Open
nickelaway opened this issue Nov 27, 2024 · 3 comments
Open

yield fails Indentation check in switch expressions #15967

nickelaway opened this issue Nov 27, 2024 · 3 comments

Comments

@nickelaway
Copy link

I have read check documentation: https://checkstyle.sourceforge.io/checks/misc/indentation.html
I have downloaded the latest cli from: https://checkstyle.org/cmdline.html#Download_and_Run
I have executed the cli and showed it below, as cli describes the problem better than 1,000 words

$ javac IndentationFoo.java
$
$ cat checkstyle-foo.xml 
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
        "https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
    <module name="TreeWalker">
        <module name="Indentation">
            <property name="forceStrictCondition" value="true"/>
        </module>
    </module>
</module>
$ cat IndentationFoo.java 
import java.time.DayOfWeek;
import java.time.LocalDate;

public class IndentationFoo {
    public static void main(final String[] args) {
        final DayOfWeek today = LocalDate.now().getDayOfWeek();
        final boolean isWeekend = switch (today) {
            case SATURDAY:
            System.out.println("Saturday"); // Warning: expected indentation: 16
            yield true; // Warning: expected indentation: 16.
            case SUNDAY:
                System.out.println("Sunday"); // (no warning)
                yield true; // Warning: expected indentation: 12
            default:
yield false; // Warning: expected indentation: 12
        };
        System.out.println("isWeekend = " + isWeekend);
    }
}
$ java -jar checkstyle-10.20.1-all.jar -c checkstyle-foo.xml IndentationFoo.java 
Starting audit...
[ERROR] /home/dan/projects/aeron/aeron-insights/insights/src/main/java/IndentationFoo.java:9:13: 'block' child has incorrect indentation level 12, expected level should be 16. [Indentation]
[ERROR] /home/dan/projects/aeron/aeron-insights/insights/src/main/java/IndentationFoo.java:10:13: 'yield' has incorrect indentation level 12, expected level should be 16. [Indentation]
[ERROR] /home/dan/projects/aeron/aeron-insights/insights/src/main/java/IndentationFoo.java:13:17: 'yield' has incorrect indentation level 16, expected level should be 12. [Indentation]
[ERROR] /home/dan/projects/aeron/aeron-insights/insights/src/main/java/IndentationFoo.java:15:1: 'yield' has incorrect indentation level 0, expected level should be 12. [Indentation]
Audit done.
Checkstyle ends with 4 errors.

The Indentation check rejects all yield statements in switch expressions when forceStrictCondition is set to true. In the example above, it rejects anything that is not 12, saying 12 is the correct level. If you set the indentation level to 12, it rejects it saying it should be 16.

This is also inconsistent with the required indentation level for other statements, which require an indentation of 16.

I would expect 16 to be accepted as the correct indentation for all of these statements.

@nrmancuso
Copy link
Member

@nickelaway thanks for the detailed report, issue is approved

@Karim-Ashraf1
Copy link

Hi @nickelaway, I want to contribute to this issue could you please assign it to me ?

@nrmancuso
Copy link
Member

Hi @nickelaway, I want to contribute to this issue could you please assign it to me ?

@Karim-Ashraf1 we do not typically assign issues, please see our contribution documentation for how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants