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

Issue #13086: fix InnerAssignmentCheck In SwitchRuleAndExpression #14804

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

@mahfouz72
Copy link
Contributor Author

Github, generate report

@mahfouz72 mahfouz72 marked this pull request as draft April 16, 2024 22:22
@mahfouz72 mahfouz72 force-pushed the fix-innerassignment branch 2 times, most recently from 413d0be to 447f762 Compare April 16, 2024 22:41
@mahfouz72 mahfouz72 marked this pull request as ready for review April 16, 2024 23:03
@mahfouz72
Copy link
Contributor Author

Github, generate report

@mahfouz72
Copy link
Contributor Author

report is good

Copy link
Member

@romani romani left a comment

Choose a reason for hiding this comment

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

Question

@@ -101,6 +101,7 @@ public class InnerAssignmentCheck
TokenTypes.RESOURCE_SPECIFICATION,
},
{TokenTypes.EXPR, TokenTypes.LAMBDA},
{TokenTypes.EXPR, TokenTypes.SWITCH_RULE, TokenTypes.LITERAL_SWITCH, TokenTypes.SLIST},
Copy link
Member

Choose a reason for hiding this comment

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

Why we need TokenTypes.SLIST ? And TokenTypes.LITERAL_SWITCH

Copy link
Contributor Author

@mahfouz72 mahfouz72 Apr 17, 2024

Choose a reason for hiding this comment

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

To make sure that we are in normal Switch context not Switch Expression. if it is {TokenTypes.EXPR, TokenTypes.SWITCH_RULE} only then we will have no violation if the assignment is in switch expression (one line syntax without braces)
ex

    public void test2(int mode) {
        int x = 0;
        x = switch (mode) {
            case 2 -> {
                yield x = 2; 
            }
            case 1 -> x = 1;  // this will be ok but should be violation 
            default -> throw new UnsupportedOperationException();
        };
    }

Switch Expression AST:

        |--EXPR -> EXPR [8:10]
        |       |   `--ASSIGN -> = [8:10]   <<---
        |       |       |--IDENT -> x [8:8]
        |       |       `--LITERAL_SWITCH -> switch [8:12]   <<---
        |       |           |--LPAREN -> ( [8:19]
        |       |           |--EXPR -> EXPR [8:20]
        |       |           |   `--IDENT -> mode [8:20]
        |       |           |--RPAREN -> ) [8:24]
        |       |           |--LCURLY -> { [8:26]
        |       |           |--SWITCH_RULE -> SWITCH_RULE [9:12]   <<---
        |       |           |   |--LITERAL_CASE -> case [9:12]
        |       |           |   |   `--EXPR -> EXPR [9:17]
        |       |           |   |       `--NUM_INT -> 1 [9:17]
        |       |           |   |--LAMBDA -> -> [9:19]
        |       |           |   |--EXPR -> EXPR [9:24]         <<---
        |       |           |   |   `--ASSIGN -> = [9:24]
        |       |           |   |       |--IDENT -> x [9:22]
        |       |           |   |       `--NUM_INT -> 1 [9:26]
        |       |           |   `--SEMI -> ; [9:27]
        |       |           `--RCURLY -> } [10:8]

switch rule AST

        `--SLIST -> { [6:33]          <<---
        |       |--LITERAL_SWITCH -> switch [7:8]   <<---
        |       |   |--LPAREN -> ( [7:15]
        |       |   |--EXPR -> EXPR [7:16]
        |       |   |   `--IDENT -> mode [7:16]
        |       |   |--RPAREN -> ) [7:20]
        |       |   |--LCURLY -> { [7:22]
        |       |   |--SWITCH_RULE -> SWITCH_RULE [8:12]          <<---
        |       |   |   |--LITERAL_CASE -> case [8:12]
        |       |   |   |   `--EXPR -> EXPR [8:17]
        |       |   |   |       `--NUM_INT -> 1 [8:17]
        |       |   |   |--LAMBDA -> -> [8:19]
        |       |   |   |--EXPR -> EXPR [8:24]       <<---
        |       |   |   |   `--ASSIGN -> = [8:24]
        |       |   |   |       |--IDENT -> x [8:22]
        |       |   |   |       `--NUM_INT -> 1 [8:26]
        |       |   |   `--SEMI -> ; [8:27]
        |       |   `--RCURLY -> } [9:8]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@romani ping

Copy link
Member

@nrmancuso nrmancuso left a comment

Choose a reason for hiding this comment

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

Please review documentation to see if we need to update there as well.

Item:

@mahfouz72
Copy link
Contributor Author

Github, generate site

@nrmancuso
Copy link
Member

@mahfouz72 please generate a regression report against openjdk21

@nrmancuso nrmancuso assigned romani and unassigned nrmancuso Apr 19, 2024
@mahfouz72
Copy link
Contributor Author

Github, generate report

Copy link
Contributor

Report generation failed on phase "make_report",
step "Generate report".
Link: https://github.com/checkstyle/checkstyle/actions/runs/8756731451

@mahfouz72
Copy link
Contributor Author

Github, generate report

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.

InnerAssignmentCheck failed for one line code in Java 14 switch expression
3 participants