[flake8-simplify] Further simplify to binary in preview for if-else-block-instead-of-if-exp (SIM108)#12796
Merged
charliermarsh merged 13 commits intoastral-sh:mainfrom Aug 10, 2024
Merged
Conversation
Collaborator
Author
|
The edge cases in question are pretty rare - not sure if there is a better way to organize the code (for performance and readability) to take advantage of that. |
Contributor
|
| code | total | + violation | - violation | + fix | - fix |
|---|---|---|---|---|---|
| SIM108 | 14 | 9 | 5 | 0 | 0 |
Member
|
Sweet, thanks! |
charliermarsh
approved these changes
Aug 10, 2024
Member
charliermarsh
left a comment
There was a problem hiding this comment.
This looks good to me. I just added a check to ensure there's no effect (like a function call) in the test, since it no longer gets repeated.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
In most cases we should suggest a ternary operator, but there are three edge cases where a binary operator is more appropriate.
Given an if-else block of the form
This PR updates the check for SIM108 to the following:
test == body_valueand preview enabled, suggest to replace withtarget_var = test or else_valuetest == not body_valueand preview enabled, suggest to replace withtarget_var = body_value and else_valuenot test == body_valueand preview enabled, suggest to replace withtarget_var = body_value and else_valuetarget_var = body_value if test else else_valueCloses #12189.