Skip to content

Commit

Permalink
tweaks + snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
diceroll123 committed Jan 20, 2024
1 parent b312212 commit 6cb0b69
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ def not_ok1():
pass


def not_ok_with_comments():
if 1:
pass
else: # else comment
if 2:
pass
else:
pass # final pass comment


# Regression test for https://github.com/apache/airflow/blob/f1e1cdcc3b2826e68ba133f350300b5065bbca33/airflow/models/dag.py#L1737
def not_ok2():
if True:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,13 @@ pub(crate) fn collapsible_else_if(checker: &mut Checker, stmt: &Stmt) {

if checker.settings.preview.is_enabled() {
let inner_if_line_start = checker.locator().line_start(first.start());
let inner_if_line_end = checker.locator().line_end(first.end());
let inner_if_full_line_end = checker.locator().full_line_end(first.end());

let desired_indentation = indentation(checker.locator(), else_clause).unwrap_or("");

let indented = adjust_indentation(
TextRange::new(inner_if_line_start, first.end()),
TextRange::new(inner_if_line_start, inner_if_line_end),
desired_indentation,
checker.locator(),
checker.stylist(),
Expand All @@ -105,7 +107,7 @@ pub(crate) fn collapsible_else_if(checker: &mut Checker, stmt: &Stmt) {
.unwrap();

diagnostic.set_fix(Fix::applicable_edits(
Edit::deletion(inner_if_line_start, first.end()),
Edit::deletion(inner_if_line_start, inner_if_full_line_end),
[Edit::range_replacement(
fixed_indented,
TextRange::new(else_clause.start(), else_colon.end()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ collapsible_else_if.py:37:5: PLR5501 Use `elif` instead of `else` then `if`, to
| |________^ PLR5501
39 | pass
|
= help: Use `elif` instead of `else` then `if`, to reduce indentation

collapsible_else_if.py:45:5: PLR5501 Use `elif` instead of `else` then `if`, to reduce indentation
|
Expand All @@ -23,17 +24,32 @@ collapsible_else_if.py:45:5: PLR5501 Use `elif` instead of `else` then `if`, to
47 | pass
48 | else:
|
= help: Use `elif` instead of `else` then `if`, to reduce indentation

collapsible_else_if.py:58:5: PLR5501 Use `elif` instead of `else` then `if`, to reduce indentation
collapsible_else_if.py:55:5: PLR5501 Use `elif` instead of `else` then `if`, to reduce indentation
|
56 | elif True:
57 | print(2)
58 | else:
53 | if 1:
54 | pass
55 | else: # else comment
| _____^
59 | | if True:
56 | | if 2:
| |________^ PLR5501
60 | print(3)
61 | else:
57 | pass
58 | else:
|
= help: Use `elif` instead of `else` then `if`, to reduce indentation

collapsible_else_if.py:68:5: PLR5501 Use `elif` instead of `else` then `if`, to reduce indentation
|
66 | elif True:
67 | print(2)
68 | else:
| _____^
69 | | if True:
| |________^ PLR5501
70 | print(3)
71 | else:
|
= help: Use `elif` instead of `else` then `if`, to reduce indentation


Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
---
source: crates/ruff_linter/src/rules/pylint/mod.rs
---
collapsible_else_if.py:37:5: PLR5501 [*] Use `elif` instead of `else` then `if`, to reduce indentation
|
35 | if 1:
36 | pass
37 | else:
| _____^
38 | | if 2:
| |________^ PLR5501
39 | pass
|
= help: Use `elif` instead of `else` then `if`, to reduce indentation

Safe fix
34 34 | def not_ok0():
35 35 | if 1:
36 36 | pass
37 |- else:
38 |- if 2:
39 |- pass
37 |+ elif 2:
38 |+ pass
40 39 |
41 40 |
42 41 | def not_ok1():

collapsible_else_if.py:45:5: PLR5501 [*] Use `elif` instead of `else` then `if`, to reduce indentation
|
43 | if 1:
44 | pass
45 | else:
| _____^
46 | | if 2:
| |________^ PLR5501
47 | pass
48 | else:
|
= help: Use `elif` instead of `else` then `if`, to reduce indentation

Safe fix
42 42 | def not_ok1():
43 43 | if 1:
44 44 | pass
45 |+ elif 2:
46 |+ pass
45 47 | else:
46 |- if 2:
47 |- pass
48 |- else:
49 |- pass
48 |+ pass
50 49 |
51 50 |
52 51 | def not_ok_with_comments():

collapsible_else_if.py:55:5: PLR5501 [*] Use `elif` instead of `else` then `if`, to reduce indentation
|
53 | if 1:
54 | pass
55 | else: # else comment
| _____^
56 | | if 2:
| |________^ PLR5501
57 | pass
58 | else:
|
= help: Use `elif` instead of `else` then `if`, to reduce indentation

Safe fix
52 52 | def not_ok_with_comments():
53 53 | if 1:
54 54 | pass
55 |- else: # else comment
56 |- if 2:
57 |- pass
58 |- else:
59 |- pass # final pass comment
55 |+ elif 2:
56 |+ pass
57 |+ else:
58 |+ pass # final pass comment # else comment
60 59 |
61 60 |
62 61 | # Regression test for https://github.com/apache/airflow/blob/f1e1cdcc3b2826e68ba133f350300b5065bbca33/airflow/models/dag.py#L1737

collapsible_else_if.py:68:5: PLR5501 [*] Use `elif` instead of `else` then `if`, to reduce indentation
|
66 | elif True:
67 | print(2)
68 | else:
| _____^
69 | | if True:
| |________^ PLR5501
70 | print(3)
71 | else:
|
= help: Use `elif` instead of `else` then `if`, to reduce indentation

Safe fix
65 65 | print(1)
66 66 | elif True:
67 67 | print(2)
68 |+ elif True:
69 |+ print(3)
68 70 | else:
69 |- if True:
70 |- print(3)
71 |- else:
72 |- print(4)
71 |+ print(4)
73 72 |


0 comments on commit 6cb0b69

Please sign in to comment.