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

Indentation: false positives when switch expression is invocation target #11741

Open
ksobolew opened this issue Jun 14, 2022 · 6 comments
Open

Comments

@ksobolew
Copy link

ksobolew commented Jun 14, 2022

I have read check documentation: https://checkstyle.org/config_misc.html#Indentation
I have downloaded the latest checkstyle 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 scratch_1.java

%  cat config.xml 
<?xml version="1.0" ?>
<!DOCTYPE module PUBLIC "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN" "https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
    <!-- Annotations -->
    <!-- Allow suppressing with an annotation -->
    <module name="TreeWalker">
        <module name="Indentation"/>
    </module>
</module>

%  cat scratch_1.java 
class Scratch
{
    public static void main(String[] args)
    {
        (switch (args.length) {
            case 1 -> args[0];
            case 2 -> args[1];
            default -> throw new IllegalArgumentException();
        }).length();

        String s = switch (args.length) {
            case 1 -> args[0];
            case 2 -> args[1];
            default -> throw new IllegalArgumentException();
        };
        s.length();
    }
}

%  RUN_LOCALE="-Duser.language=en -Duser.country=US"

%  java -jar checkstyle-10.3-all.jar -c config.xml scratch_1.java
Starting audit...
[ERROR] /Users/kudivuhadi/Library/Application Support/JetBrains/IntelliJIdea2022.2/scratches/scratch_1.java:6:13: 'case' child has incorrect indentation level 12, expected level should be 16. [Indentation]
[ERROR] /Users/kudivuhadi/Library/Application Support/JetBrains/IntelliJIdea2022.2/scratches/scratch_1.java:7:13: 'case' child has incorrect indentation level 12, expected level should be 16. [Indentation]
[ERROR] /Users/kudivuhadi/Library/Application Support/JetBrains/IntelliJIdea2022.2/scratches/scratch_1.java:8:13: 'case' child has incorrect indentation level 12, expected level should be 16. [Indentation]
[ERROR] /Users/kudivuhadi/Library/Application Support/JetBrains/IntelliJIdea2022.2/scratches/scratch_1.java:9:9: 'switch rcurly' has incorrect indentation level 8, expected level should be 12. [Indentation]
Audit done.
Checkstyle ends with 4 errors.

The main method contains two switch expressions: the first is immediately used as a target of method invocation, the second does the same, but its value is first assigned to an intermediate variable. Indentation reports errors for the first switch, but not the second. This is similar to #8929, which (I believe) fixed it for the second switch.

@ksobolew
Copy link
Author

I also just noticed that the same thing happens when the switch is yieldded from an outer switch:

int result = switch (something) {
    case CASE -> {
        yield switch (somethingElse) {
            case CASE -> 1;
            default -> 2;
        }
    };
    default -> 3;
}

@DmitriySerobyan
Copy link

DmitriySerobyan commented Aug 10, 2022

I have the same problem with nested switch and yield

@JarvisCraft
Copy link

Having the same issue when using Checkstyle with Java 17 sources containing new switch expression constructs.

@SharplEr
Copy link

On my work project we use JDK 17, and also faced with that problem with nested switch expressions.

@sherman
Copy link

sherman commented Aug 10, 2022

Voted for this issue. Switched to JDK 17 where the switch expression significantly improved and used widely.

@Kevin222004
Copy link
Contributor

I am on it.

Kevin222004 added a commit to Kevin222004/checkstyle that referenced this issue Dec 21, 2023
Kevin222004 added a commit to Kevin222004/checkstyle that referenced this issue Dec 21, 2023
Kevin222004 added a commit to Kevin222004/checkstyle that referenced this issue Jan 2, 2024
Kevin222004 added a commit to Kevin222004/checkstyle that referenced this issue Jan 2, 2024
Kevin222004 added a commit to Kevin222004/checkstyle that referenced this issue Jan 3, 2024
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

7 participants