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

Merged
merged 1 commit into from May 12, 2024

Conversation

@mahfouz72
Copy link
Member 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
Member Author

Github, generate report

@mahfouz72
Copy link
Member 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

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
Member 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
Member 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
Member Author

Github, generate report

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.

Ok to merge

@romani romani requested a review from rnveach May 7, 2024 20:36
@romani romani assigned rnveach and unassigned romani May 7, 2024
Copy link
Member

@rnveach rnveach left a comment

Choose a reason for hiding this comment

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

There is not really any descriptions update in this PR.
https://checkstyle.org/checks/coding/innerassignment.html#Description

Except for the loop idioms, all assignments should occur in their own top-level statement to increase readability.

Is this wording technically not correct anymore? I wouldn't really consider cases to be top level statements.

int x = 0;
x = switch (mode) {
case 2 -> {
yield x = 2; // violation
Copy link
Member

@rnveach rnveach May 8, 2024

Choose a reason for hiding this comment

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

So why does this create violations but something like test 2 doesn't? Is this because it is basically nested inside an assignment already?

Is there somewhere (official?) that shows what inner assignments are in this case?

What if we had a case like (also add it):

System.out.println(switch (mode) {
            case 2 -> {
                yield x = 2;

This wouldn't be an violation?

Copy link
Member Author

Choose a reason for hiding this comment

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

So why does this create violations but something like test 2 doesn't

because this is an assignment inside expressions ( switch expressions ), while test2 is an assignment inside normal switch statements

Is there somewhere (official?)

I didn't find any but what I know from our docs is that inner assignments are the assignments inside expressions. So basically the assignments inside switch expressions x = switch(){....} should be avoided

What if we had a case like (also add it):

Yes, violation and added

Copy link
Member

Choose a reason for hiding this comment

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

Is there somewhere (official?) that shows what inner assignments are in this case?

Doubt it. A better way to describe what this check does is "place violations (mostly) anywhere that assigned values are used directly". Ideally, we should only do one thing at a time; assign a value, or use it, not both.

Copy link
Member

Choose a reason for hiding this comment

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

yield x = 2;

Just to confirm, since I am not use to this syntax, is this the same as x = 2; yield x;?
If we had y = 2 before this line would that be a violation too?

@nrmancuso @romani Are we ok that test8 is a violation for this check?

Copy link
Member Author

@mahfouz72 mahfouz72 May 8, 2024

Choose a reason for hiding this comment

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

Just to confirm, since I am not use to this syntax, is this the same as x = 2; yield x;?

Yes I think So

If we had y = 2 before this line would that be a violation too?

this will have no violation due to


I am not sure if this is the correct behavior. but I assume it is valid because we can assign a value inside the case block but also without using this assigned value at the same time, so we are not violating this

we should only do one thing at a time; assign a value, or use it, not both.

example

System.out.println(switch (x) {
            case 1 -> x = 1;   // violation
            case 2 -> {
                  y = 2;   // assign only but not used
                 yield x = 2;      // violation
            }

this is not the case when we have something like

System.out.println(switch (x) {
            case 1 -> x = 1;   // violation
            case 2 -> {
                 yield x = 2;      // violation
            }

here in both cases, we assigned a value and and used it directly in the print

Copy link
Member

@nrmancuso nrmancuso May 9, 2024

Choose a reason for hiding this comment

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

@nrmancuso @romani Are we ok that test8 is a violation for this check?

Yes, assigned values are used directly in all three cases. I encourage everyone involved here to practice using Java 21 language features to understand how they work.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I am ok with test8 to be violation .
Old switch structure always did side effect to outside variables.
Switch like expression, that return single result, should not do side effects (changing outside of switch variables).

I reviewed one more time this PR, I am still ok to merge.

@rnveach
Copy link
Member

rnveach commented May 8, 2024

Diff Regression projects: https://gist.githubusercontent.com/mahfouz72/297d7353d2d0bcec178e1b098109cd9e/raw/546dcdbdff363110fa265ea80d70e36f079c6ee8/projects-to-test-on.properties

We normally provide regression on all projects. Even if no others have this form of code, it will show this doesn't impact older code. Please update the file.

@mahfouz72
Copy link
Member Author

We normally provide regression on all projects. Even if no others have this form of code, it will show this doesn't impact older code. Please update the file.

Yes this is only to generate another report I generated two

all projects: #14804 (comment)
java 21: #14804 (comment)

@mahfouz72
Copy link
Member Author

Is this wording technically not correct anymore?

I don't see anything wrong. Can you please point to an example that you think violates this wording

x = switch(..){ case 1 -> x = 3} (assignment is not in its own top level (nested inside another assignment) so violation
switch(..){ case 1 -> x = 3} ( I guess this is considered as a top-level so no violation )

@nrmancuso
Copy link
Member

There is not really any descriptions update in this PR. https://checkstyle.org/checks/coding/innerassignment.html#Description

Except for the loop idioms, all assignments should occur in their own top-level statement to increase readability.

Is this wording technically not correct anymore? I wouldn't really consider cases to be top level statements.

I think this still holds for switch expressions; the only assignment happening should be to a variable declared outside(top level) of the switch expression, from a value yielded by the switch expression.

@rnveach
Copy link
Member

rnveach commented May 8, 2024

I think I need some clarification then on what is a "top level statement" is.

I tried google. The first thing said Top-level statements allows you to write executable code directly at the root of a file, eliminating the need for wrapping your code in a class or method. . However that seems to be a C# link. Then I got a reddit for Java help where someone replied to the poster to clarify this exact statement and it was never answered. Is there someplace official that has the exact definition?

I always considered this check to mean something like "the statement must only contain the assignment itself", but then the whole switch is considered a single statement (unless I have the Java grammar wrong).

@romani
Copy link
Member

romani commented May 10, 2024

but then the whole switch is considered a single statement (unless I have the Java grammar wrong).

It is probably very confusing to consider switch expressions as statement. It better to think of this like inlined function in such cases.

Defect that we have is that we report violation on switch that statement like.
For now users are ok with our decision that assignments in expression like structures should be violation, so let's keep it as is, we are ok to reconsider this is user come up with good reasons later on.

@rnveach
Copy link
Member

rnveach commented May 12, 2024

I am not calling into question what we print out as violations. My whole comments was the documentation and is it clear enough on what we say this check does.

@romani
Copy link
Member

romani commented May 12, 2024

https://checkstyle.org/checks/coding/innerassignment.html#Description

I don't know how to improve our doc, we have have not very strict target of validation, and nice exception structure that is ignored.
This should not block us to merge this PR. One day in future some contributor will share good wording.

@romani romani closed this May 12, 2024
@romani romani reopened this May 12, 2024
@rnveach rnveach merged commit e7facb0 into checkstyle:master May 12, 2024
159 of 160 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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