-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Parenthesize composite if condition before inverting in invert-if assist #6894
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Looking at this, I wonder if this ideally should be de-morganed?
if x != 3 && x != 4 && x != 5?Uh oh!
There was an error while loading. Please reload this page.
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.
I think whether de-morganing is desired depends heavily on the situation. One could argue that, when most (more than half?) of the parts of a composite condition are negative (
if x != 3 && x != 4 && y == 5), it would be better to de-morgan when inverting (if x== 3 || x == 4 && y != 5). The situation in this test might also benefit from de-morganing, as it makes it clearer that this is a "x not in [3, 4, 5]" case. There might be other cases where de-morganing might make the code worse though. And in the end it also depends on the user's preferences.I think it would be good to support it, but I'm not sure about what the best defaults are. I think adding parens might be less surprising to the user and automatic de-morganing could be opt-in through a config option, while manual de-morganing can be achieved through a separate code action (called "distribute
!over operands" or something like that). Not sure if that conforms to being the "least surprising" and "enable all features for better discovery" principles though.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.
Hm, yeah, agree with the reasoning that distributing
!should be a separate action. We actually have it, but it it is a bit naive:Uh oh!
There was an error while loading. Please reload this page.
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.
In the case you cite, because you invoke the assist on the
&&operator, the current behaviour is actually what I would expect to happen. It would be nice if, when invoked on the prefixed!, the!could be distributed over the entire parenthesized expression. Maybe the assist could even trigger on theifkeyword, to allow applying De Morgan's law on the entire condition without the need for parentheses or losing the current behaviour.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.
Another thing that would be nice to have is if applying De Morgan's law twice would result in the initial expression:
Currently:
if x == 4 || y == 5 {}->if !(x != 3 && y != 4) {}->if !(!(x == 3 || y ==4)) {}That simplification is something we would probably want to do in the invert-if assist as well.