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

Closed
nickelaway opened this issue Nov 27, 2024 · 7 comments · Fixed by #16131
Closed

yield fails Indentation check in switch expressions #15967

nickelaway opened this issue Nov 27, 2024 · 7 comments · Fixed by #16131

Comments

@nickelaway
Copy link

nickelaway commented Nov 27, 2024

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] IndentationFoo.java:9:13: 'block' child has incorrect indentation level 12, expected level should be 16. [Indentation]
[ERROR] IndentationFoo.java:10:13: 'yield' has incorrect indentation level 12, expected level should be 16. [Indentation]
[ERROR] IndentationFoo.java:13:17: 'yield' has incorrect indentation level 16, expected level should be 12. [Indentation]
[ERROR] 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.

@mohitsatr
Copy link
Contributor

@Karim-Ashraf1 are you on it?

@romani
Copy link
Member

romani commented Jan 3, 2025

If no PR sent, it means not in progress. Everyone can take it.

@Karim-Ashraf1
Copy link

@Karim-Ashraf1 are you on it?

I am currently not working on it you can take it bro

mohitsatr added a commit to mohitsatr/checkstyle that referenced this issue Jan 8, 2025
mohitsatr added a commit to mohitsatr/checkstyle that referenced this issue Jan 10, 2025
mohitsatr added a commit to mohitsatr/checkstyle that referenced this issue Jan 10, 2025
mohitsatr added a commit to mohitsatr/checkstyle that referenced this issue Jan 11, 2025
mohitsatr added a commit to mohitsatr/checkstyle that referenced this issue Jan 12, 2025
mohitsatr added a commit to mohitsatr/checkstyle that referenced this issue Jan 13, 2025
mohitsatr added a commit to mohitsatr/checkstyle that referenced this issue Jan 19, 2025
mohitsatr added a commit to mohitsatr/checkstyle that referenced this issue Jan 26, 2025
@github-actions github-actions bot added this to the 10.21.3 milestone Jan 29, 2025
@nrmancuso nrmancuso added the bug label Jan 29, 2025
@nickelaway
Copy link
Author

Thank you @mohitsatr for the fix 🙂

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

Successfully merging a pull request may close this issue.

5 participants