[`flake8-simplify`] - Fix syntax error in autofix (`SIM114`) (#9704)

## Summary

A fix for
https://github.com/astral-sh/ruff/issues/8402#issuecomment-1916215124

Improves the code, as well. :)

## Test Plan

`cargo test`
This commit is contained in:
Steve C 2024-01-30 11:36:44 -05:00 committed by GitHub
parent 3c7fea769c
commit 214563261d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 37 additions and 12 deletions

View File

@ -143,3 +143,8 @@ elif c: # but not on the second branch
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

View File

@ -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.

View File

@ -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

View File

@ -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