Skip to content

Commit

Permalink
Fix SIM102
Browse files Browse the repository at this point in the history
  • Loading branch information
harupy committed Jul 25, 2023
1 parent e7f228f commit d0ee7be
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
9 changes: 9 additions & 0 deletions crates/ruff/resources/test/fixtures/flake8_simplify/SIM102.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,12 @@
if True:
if a:
pass


# SIM102
def f():
if a:
pass
elif b:
if c:
d
2 changes: 1 addition & 1 deletion crates/ruff/src/rules/flake8_simplify/rules/fix_if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub(crate) fn fix_nested_if_statements(

// If this is an `elif`, we have to remove the `elif` keyword for now. (We'll
// restore the `el` later on.)
let is_elif = contents.starts_with("elif");
let is_elif = contents.trim_start().starts_with("elif");
let module_text = if is_elif {
Cow::Owned(contents.replacen("elif", "if", 1))
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,4 +332,26 @@ SIM102.py:132:5: SIM102 [*] Use a single `if` statement instead of nested `if` s
136 135 | print("bar")
137 136 |

SIM102.py:165:5: SIM102 [*] Use a single `if` statement instead of nested `if` statements
|
163 | if a:
164 | pass
165 | elif b:
| _____^
166 | | if c:
| |_____________^ SIM102
167 | d
|
= help: Combine `if` statements using `and`

Suggested fix
162 162 | def f():
163 163 | if a:
164 164 | pass
165 |- elif b:
166 |- if c:
167 |- d
165 |+ elif b and c:
166 |+ d


0 comments on commit d0ee7be

Please sign in to comment.