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
Conversation
Github, generate report |
cce1a3b
to
83700c0
Compare
413d0be
to
447f762
Compare
Github, generate report |
report is good |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question
src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/InnerAssignmentCheck.java
Show resolved
Hide resolved
There was a problem hiding this 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:
.../checkstyle/checks/coding/innerassignment/InputInnerAssignmentSwitchAndSwitchExpression.java
Outdated
Show resolved
Hide resolved
447f762
to
ab49920
Compare
Github, generate site |
ab49920
to
32b2013
Compare
@mahfouz72 please generate a regression report against openjdk21 |
Github, generate report |
Report generation failed on phase "make_report", |
Github, generate report |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok to merge
There was a problem hiding this 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.
src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/InnerAssignmentCheckTest.java
Outdated
Show resolved
Hide resolved
int x = 0; | ||
x = switch (mode) { | ||
case 2 -> { | ||
yield x = 2; // violation |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
checkstyle/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/InnerAssignmentCheck.java
Line 94 in 405db25
{TokenTypes.EXPR, TokenTypes.SLIST}, |
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
.../checkstyle/checks/coding/innerassignment/InputInnerAssignmentSwitchAndSwitchExpression.java
Show resolved
Hide resolved
...es-noncompilable/com/puppycrawl/tools/checkstyle/checks/coding/innerassignment/Example2.java
Outdated
Show resolved
Hide resolved
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. |
32b2013
to
ac4bb63
Compare
ac4bb63
to
405db25
Compare
Yes this is only to generate another report I generated two all projects: #14804 (comment) |
I don't see anything wrong. Can you please point to an example that you think violates this wording
|
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. |
I think I need some clarification then on what is a "top level statement" is. I tried google. The first thing said 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). |
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. |
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. |
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. |
Resolves #13086 :
Diff Regression config: https://gist.githubusercontent.com/mahfouz72/70e0fb4b76668949fd9e0fd3744deddd/raw/0b035184445fee1009e49543af7b1daa6b52f79f/innerass.xml
Diff Regression projects: https://gist.githubusercontent.com/mahfouz72/297d7353d2d0bcec178e1b098109cd9e/raw/546dcdbdff363110fa265ea80d70e36f079c6ee8/projects-to-test-on.properties