Skip to content

Commit

Permalink
tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
diceroll123 committed Jan 21, 2024
1 parent f40fc2c commit bfba477
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 44 deletions.
24 changes: 12 additions & 12 deletions crates/ruff_linter/src/rules/pylint/rules/useless_else_on_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ pub(crate) fn useless_else_on_loop(
} else {
let desired_indentation = indentation(checker.locator(), stmt).unwrap_or("");
let else_line_range = checker.locator().full_line_range(else_range.start());
let else_colon =
SimpleTokenizer::starts_at(else_range.start(), checker.locator().contents())
.find(|token| token.kind == SimpleTokenKind::Colon)
.unwrap();

let indented = adjust_indentation(
TextRange::new(else_line_range.end(), end.end()),
Expand All @@ -141,19 +145,15 @@ pub(crate) fn useless_else_on_loop(
)
.unwrap();

// we'll either delete the whole "else" line, or preserve the comment if there is one
let else_deletion_range = if let Some(comment_token) =
SimpleTokenizer::starts_at(else_range.start(), checker.locator().contents())
.find(|token| token.kind == SimpleTokenKind::Comment)
{
TextRange::new(else_range.start(), comment_token.start())
} else {
else_line_range
};

diagnostic.set_fix(Fix::applicable_edits(
Edit::range_replacement(indented, TextRange::new(else_line_range.end(), end.end())),
[Edit::range_deletion(else_deletion_range)],
Edit::deletion(
else_line_range.end(),
checker.locator().full_line_end(end.end()),
),
[Edit::range_replacement(
indented,
TextRange::new(else_line_range.start(), else_colon.end()),
)],
Applicability::Safe,
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ useless_else_on_loop.py:9:5: PLW0120 [*] `else` clause on loop without a `break`
8 8 | return i
9 |- else: # [useless-else-on-loop]
10 |- print("math is broken")
9 |+ # [useless-else-on-loop]
10 |+ print("math is broken")
11 11 | return None
12 12 |
13 13 |
9 |+ print("math is broken") # [useless-else-on-loop]
11 10 | return None
12 11 |
13 12 |

useless_else_on_loop.py:18:5: PLW0120 [*] `else` clause on loop without a `break` statement; remove the `else` and de-indent all the code inside it
|
Expand All @@ -41,11 +40,10 @@ useless_else_on_loop.py:18:5: PLW0120 [*] `else` clause on loop without a `break
17 17 | return 1
18 |- else: # [useless-else-on-loop]
19 |- print("math is broken")
18 |+ # [useless-else-on-loop]
19 |+ print("math is broken")
20 20 | return None
21 21 |
22 22 |
18 |+ print("math is broken") # [useless-else-on-loop]
20 19 | return None
21 20 |
22 21 |

useless_else_on_loop.py:30:1: PLW0120 [*] `else` clause on loop without a `break` statement; remove the `else` and de-indent all the code inside it
|
Expand All @@ -63,11 +61,10 @@ useless_else_on_loop.py:30:1: PLW0120 [*] `else` clause on loop without a `break
29 29 |
30 |-else: # [useless-else-on-loop]
31 |- print("or else!")
30 |+# [useless-else-on-loop]
31 |+print("or else!")
32 32 |
33 33 |
34 34 | while True:
30 |+print("or else!") # [useless-else-on-loop]
32 31 |
33 32 |
34 33 | while True:

useless_else_on_loop.py:37:1: PLW0120 [*] `else` clause on loop without a `break` statement; remove the `else` and de-indent all the code inside it
|
Expand All @@ -85,11 +82,10 @@ useless_else_on_loop.py:37:1: PLW0120 [*] `else` clause on loop without a `break
36 36 | break
37 |-else: # [useless-else-on-loop]
38 |- print("or else!")
37 |+# [useless-else-on-loop]
38 |+print("or else!")
39 39 |
40 40 | for j in range(10):
41 41 | pass
37 |+print("or else!") # [useless-else-on-loop]
39 38 |
40 39 | for j in range(10):
41 40 | pass

useless_else_on_loop.py:42:1: PLW0120 [*] `else` clause on loop without a `break` statement; remove the `else` and de-indent all the code inside it
|
Expand All @@ -110,13 +106,12 @@ useless_else_on_loop.py:42:1: PLW0120 [*] `else` clause on loop without a `break
43 |- print("fat chance")
44 |- for j in range(10):
45 |- break
42 |+# [useless-else-on-loop]
43 |+print("fat chance")
44 |+for j in range(10):
45 |+ break
46 46 |
47 47 |
48 48 | def test_return_for2():
42 |+print("fat chance")
43 |+for j in range(10):
44 |+ break # [useless-else-on-loop]
46 45 |
47 46 |
48 47 | def test_return_for2():

useless_else_on_loop.py:88:5: PLW0120 [*] `else` clause on loop without a `break` statement; remove the `else` and de-indent all the code inside it
|
Expand All @@ -135,11 +130,10 @@ useless_else_on_loop.py:88:5: PLW0120 [*] `else` clause on loop without a `break
87 87 | print("all right")
88 |- else: # [useless-else-on-loop]
89 |- return True
88 |+ # [useless-else-on-loop]
89 |+ return True
90 90 | return False
91 91 |
92 92 |
88 |+ return True # [useless-else-on-loop]
90 89 | return False
91 90 |
92 91 |

useless_else_on_loop.py:98:9: PLW0120 [*] `else` clause on loop without a `break` statement; remove the `else` and de-indent all the code inside it
|
Expand Down

0 comments on commit bfba477

Please sign in to comment.