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

InnerAssignmentCheck failed for one line code in Java 14 switch expression #13086

Closed
HaelC opened this issue May 23, 2023 · 2 comments · Fixed by #14804
Closed

InnerAssignmentCheck failed for one line code in Java 14 switch expression #13086

HaelC opened this issue May 23, 2023 · 2 comments · Fixed by #14804

Comments

@HaelC
Copy link

HaelC commented May 23, 2023

I have read check documentation: https://checkstyle.org/config_coding.html#InnerAssignment
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

/var/tmp $ javac TestInnerAssignmentCheck.java

/var/tmp $ 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">
  <module name="TreeWalker">
    <module name="InnerAssignment"/>
  </module>
</module>

/var/tmp $ cat TestInnerAssignmentCheck.java
public class TestInnerAssignmentCheck {
    private boolean flag;

    void getOperation(final String operation) {
        switch (operation) {
            case "Y" -> flag = true;  // violation 
            case "N" -> {
                flag = false;
            }
            default -> throw new UnsupportedOperationException();
        }
    }
}

/var/tmp $ RUN_LOCALE="-Duser.language=en -Duser.country=US"
/var/tmp $ java $RUN_LOCALE -jar checkstyle-10.11.0-all.jar -c config.xml TestInnerAssignmentCheck.java
Starting audit...
[ERROR] /Users/hael/Code/Learn/Tools/checkstyle/TestInnerAssignmentCheck.java:6:30:
     Inner assignments should be avoided. [InnerAssignment]
Audit done.
Checkstyle ends with 1 errors.

Describe what you expect in detail.

Checkstyle behaves differently between one line code and code block for Java 14's Switch Expression. In the given code above, checkstyle will complain about case "Y" -> flag = true; but not case "N" -> {flag = false;}. I'm afraid that it's because flag = true; is equivalent to return flag = true;? But the return value is not used here in the switch expression, we just want to do the value assignment.

Expected: no violations on such code.

Andromeda227799 pushed a commit to Andromeda227799/checkstyle that referenced this issue May 25, 2023
Andromeda227799 pushed a commit to Andromeda227799/checkstyle that referenced this issue May 27, 2023
Andromeda227799 pushed a commit to Andromeda227799/checkstyle that referenced this issue May 27, 2023
Andromeda227799 pushed a commit to Andromeda227799/checkstyle that referenced this issue May 27, 2023
Andromeda227799 pushed a commit to Andromeda227799/checkstyle that referenced this issue May 28, 2023
Andromeda227799 pushed a commit to Andromeda227799/checkstyle that referenced this issue May 28, 2023
Andromeda227799 pushed a commit to Andromeda227799/checkstyle that referenced this issue May 28, 2023
Andromeda227799 pushed a commit to Andromeda227799/checkstyle that referenced this issue May 28, 2023
Andromeda227799 pushed a commit to Andromeda227799/checkstyle that referenced this issue Jun 6, 2023
aayushRedHat added a commit to aayushRedHat/checkstyle that referenced this issue Dec 16, 2023
@mahfouz72
Copy link
Member

@romani @nrmancuso there is an open PR for this but no activity since jan.idk if it is abandoned.so can i take this or should we wait

@nrmancuso
Copy link
Member

@mahfouz72 you can pick it up, I closed the PR

mahfouz72 added a commit to mahfouz72/checkstyle that referenced this issue Apr 16, 2024
mahfouz72 added a commit to mahfouz72/checkstyle that referenced this issue Apr 16, 2024
mahfouz72 added a commit to mahfouz72/checkstyle that referenced this issue Apr 16, 2024
mahfouz72 added a commit to mahfouz72/checkstyle that referenced this issue Apr 16, 2024
mahfouz72 added a commit to mahfouz72/checkstyle that referenced this issue Apr 16, 2024
@checkstyle checkstyle deleted a comment from Andromeda227799 Apr 17, 2024
@checkstyle checkstyle deleted a comment from AayushSaini101 Apr 17, 2024
@checkstyle checkstyle deleted a comment from nrmancuso Apr 17, 2024
mahfouz72 added a commit to mahfouz72/checkstyle that referenced this issue Apr 19, 2024
mahfouz72 added a commit to mahfouz72/checkstyle that referenced this issue Apr 19, 2024
mahfouz72 added a commit to mahfouz72/checkstyle that referenced this issue May 8, 2024
mahfouz72 added a commit to mahfouz72/checkstyle that referenced this issue May 8, 2024
@github-actions github-actions bot added this to the 10.17.0 milestone May 12, 2024
@romani romani added the bug label May 13, 2024
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.

4 participants