From bf090897195ad2c3c30fffea6212fe3824656b55 Mon Sep 17 00:00:00 2001 From: Steve C Date: Sat, 20 Jan 2024 18:59:30 -0500 Subject: [PATCH] tweaks --- .../pylint/rules/useless_else_on_loop.rs | 26 ++++----- ...view__PLW0120_useless_else_on_loop.py.snap | 58 +++++++++---------- 2 files changed, 39 insertions(+), 45 deletions(-) diff --git a/crates/ruff_linter/src/rules/pylint/rules/useless_else_on_loop.rs b/crates/ruff_linter/src/rules/pylint/rules/useless_else_on_loop.rs index 46312e8cc2dc76..a65a3f7b0ef2f3 100644 --- a/crates/ruff_linter/src/rules/pylint/rules/useless_else_on_loop.rs +++ b/crates/ruff_linter/src/rules/pylint/rules/useless_else_on_loop.rs @@ -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()), @@ -141,21 +145,17 @@ 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, - )); + )) } } diff --git a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__preview__PLW0120_useless_else_on_loop.py.snap b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__preview__PLW0120_useless_else_on_loop.py.snap index c6d839243d4be6..0633d6a68908ab 100644 --- a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__preview__PLW0120_useless_else_on_loop.py.snap +++ b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__preview__PLW0120_useless_else_on_loop.py.snap @@ -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 | @@ -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 | @@ -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 | @@ -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 | @@ -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 | @@ -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 |