Skip to content

Commit

Permalink
Refactor, add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Dec 19, 2024
1 parent 82fa0ae commit 1838141
Show file tree
Hide file tree
Showing 13 changed files with 358 additions and 181 deletions.
15 changes: 13 additions & 2 deletions crates/ruff_linter/resources/test/fixtures/ruff/RUF047_for.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,29 @@
# `else`
else:
pass
...


for _and in so:
does()
# this
else:
...
pass


for of in course():
this()
else:
... # too


for of in course():
this()
else:
...
# too

for of in course():
this()
else:
...
# this comment does not belong to the else
34 changes: 27 additions & 7 deletions crates/ruff_linter/resources/test/fixtures/ruff/RUF047_if.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,41 @@

if this_second_comment():
belongs() # to
# `else`
# `else`
else:
pass
...


if and_so():
does()
# this
if this_second_comment():
belongs() # to
# `else`
else:
...
pass


if of_course():
this()
else:
... # too

if of_course():
this()
else:
...
# comment

if of_course():
this()
else:
...
# this comment doesn't belong to the if

if of_course: this()
else: ...

if of_course:
this() # comment
else: ...

if of_course:
this() # comment
else: ... # trailing
31 changes: 29 additions & 2 deletions crates/ruff_linter/resources/test/fixtures/ruff/RUF047_try.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
# `else`
else:
pass
...


try:
Expand All @@ -33,7 +32,6 @@
# this
else:
...
pass


try:
Expand All @@ -42,3 +40,32 @@
this()
else:
... # too

try:
of_course()
except:
this()
else:
...
# This comment belongs to else
finally:
pass

try:
of_course()
except:
this()
else:
...
# This comment belongs to finally
finally:
pass


try:
of_course()
except:
this()
else:
...
# This comment belongs to the statement coming after the else
15 changes: 13 additions & 2 deletions crates/ruff_linter/resources/test/fixtures/ruff/RUF047_while.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,29 @@
# `else`
else:
pass
...


while and_so:
does()
# this
else:
...
pass


while of_course():
this()
else:
... # too

while of_course():
this()
else:
...
# this comment belongs to the else

while of_course():
this()
else:
...
# this comment belongs to the statement coming after the else

24 changes: 13 additions & 11 deletions crates/ruff_linter/src/checkers/ast/analyze/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1239,7 +1239,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
ruff::rules::if_key_in_dict_del(checker, if_);
}
if checker.enabled(Rule::NeedlessElse) {
ruff::rules::needless_else(checker, stmt);
ruff::rules::needless_else(checker, if_.into());
}
}
Stmt::Assert(
Expand Down Expand Up @@ -1348,7 +1348,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
flake8_async::rules::async_busy_wait(checker, while_stmt);
}
if checker.enabled(Rule::NeedlessElse) {
ruff::rules::needless_else(checker, stmt);
ruff::rules::needless_else(checker, while_stmt.into());
}
}
Stmt::For(
Expand Down Expand Up @@ -1437,16 +1437,18 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
}
}
if checker.enabled(Rule::NeedlessElse) {
ruff::rules::needless_else(checker, stmt);
ruff::rules::needless_else(checker, for_stmt.into());
}
}
Stmt::Try(ast::StmtTry {
body,
handlers,
orelse,
finalbody,
..
}) => {
Stmt::Try(
try_stmt @ ast::StmtTry {
body,
handlers,
orelse,
finalbody,
..
},
) => {
if checker.enabled(Rule::TooManyNestedBlocks) {
pylint::rules::too_many_nested_blocks(checker, stmt);
}
Expand Down Expand Up @@ -1514,7 +1516,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
tryceratops::rules::error_instead_of_exception(checker, handlers);
}
if checker.enabled(Rule::NeedlessElse) {
ruff::rules::needless_else(checker, stmt);
ruff::rules::needless_else(checker, try_stmt.into());
}
}
Stmt::Assign(assign @ ast::StmtAssign { targets, value, .. }) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ pub(crate) fn useless_else_on_loop(
return;
}

let else_range =
identifier::else_loop(stmt, checker.locator().contents()).expect("else clause");
let else_range = identifier::else_(stmt, checker.locator().contents()).expect("else clause");

let mut diagnostic = Diagnostic::new(UselessElseOnLoop, else_range);
diagnostic.try_set_fix(|| {
Expand Down
Loading

0 comments on commit 1838141

Please sign in to comment.