Skip to content

Commit

Permalink
[flake8-simplify] - Fix syntax error in autofix (SIM114) (#9704)
Browse files Browse the repository at this point in the history
## Summary

A fix for
#8402 (comment)

Improves the code, as well. :)

## Test Plan

`cargo test`
  • Loading branch information
diceroll123 authored Jan 30, 2024
1 parent 3c7fea7 commit 2145632
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,8 @@ def func():

if a: b # here's a comment
elif c: b


if(x > 200): pass
elif(100 < x and x < 200 and 300 < y and y < 800):
pass
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,9 @@ fn merge_branches(
return Err(anyhow::anyhow!("Expected colon after test"));
};

let mut following_branch_tokenizer =
SimpleTokenizer::starts_at(following_branch.test.end(), locator.contents());

// Identify the colon (`:`) at the end of the following branch's test.
let Some(following_branch_colon) =
following_branch_tokenizer.find(|token| token.kind == SimpleTokenKind::Colon)
else {
return Err(anyhow::anyhow!("Expected colon after test"));
};

let main_edit = Edit::deletion(
locator.full_line_end(current_branch_colon.end()),
locator.full_line_end(following_branch_colon.end()),
locator.full_line_end(current_branch.end()),
locator.full_line_end(following_branch.end()),
);

// If the test isn't parenthesized, consider parenthesizing it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,13 @@ SIM114.py:144:1: SIM114 Combine `if` branches using logical `or` operator
|
= help: Combine `if` branches

SIM114.py:148:1: SIM114 Combine `if` branches using logical `or` operator
|
148 | / if(x > 200): pass
149 | | elif(100 < x and x < 200 and 300 < y and y < 800):
150 | | pass
| |________^ SIM114
|
= help: Combine `if` branches


Original file line number Diff line number Diff line change
Expand Up @@ -442,5 +442,26 @@ SIM114.py:144:1: SIM114 [*] Combine `if` branches using logical `or` operator
144 |-if a: b # here's a comment
145 |-elif c: b
144 |+if a or c: b # here's a comment
146 145 |
147 146 |
148 147 | if(x > 200): pass

SIM114.py:148:1: SIM114 [*] Combine `if` branches using logical `or` operator
|
148 | / if(x > 200): pass
149 | | elif(100 < x and x < 200 and 300 < y and y < 800):
150 | | pass
| |________^ SIM114
|
= help: Combine `if` branches

Safe fix
145 145 | elif c: b
146 146 |
147 147 |
148 |-if(x > 200): pass
149 |-elif(100 < x and x < 200 and 300 < y and y < 800):
150 |- pass
148 |+if(x > 200) or (100 < x and x < 200 and 300 < y and y < 800): pass


0 comments on commit 2145632

Please sign in to comment.