Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement nested-if detection (#1649)
- Loading branch information
1 parent
9041423
commit 30d6688
Showing
10 changed files
with
136 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
if a: # SIM102 | ||
if b: | ||
c | ||
|
||
|
||
if a: | ||
pass | ||
elif b: # SIM102 | ||
if c: | ||
d | ||
|
||
if a: | ||
if b: | ||
c | ||
else: | ||
d | ||
|
||
if __name__ == "__main__": | ||
if foo(): | ||
... | ||
|
||
if a: | ||
d | ||
if b: | ||
c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use rustpython_ast::{Constant, Expr, ExprKind, Stmt, StmtKind}; | ||
|
||
use crate::ast::types::Range; | ||
use crate::checkers::ast::Checker; | ||
use crate::registry::{Check, CheckKind}; | ||
|
||
fn is_main_check(expr: &Expr) -> bool { | ||
if let ExprKind::Compare { | ||
left, comparators, .. | ||
} = &expr.node | ||
{ | ||
if let ExprKind::Name { id, .. } = &left.node { | ||
if id == "__name__" { | ||
if comparators.len() == 1 { | ||
if let ExprKind::Constant { | ||
value: Constant::Str(value), | ||
.. | ||
} = &comparators[0].node | ||
{ | ||
if value == "__main__" { | ||
return true; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
false | ||
} | ||
|
||
/// SIM102 | ||
pub fn nested_if_statements(checker: &mut Checker, stmt: &Stmt) { | ||
let StmtKind::If { test, body, orelse } = &stmt.node else { | ||
return; | ||
}; | ||
|
||
// if a: <--- | ||
// if b: <--- | ||
// c | ||
let is_nested_if = { | ||
if orelse.is_empty() && body.len() == 1 { | ||
if let StmtKind::If { orelse, .. } = &body[0].node { | ||
orelse.is_empty() | ||
} else { | ||
false | ||
} | ||
} else { | ||
false | ||
} | ||
}; | ||
|
||
if !is_nested_if { | ||
return; | ||
}; | ||
|
||
if is_main_check(test) { | ||
return; | ||
} | ||
|
||
checker.add_check(Check::new( | ||
CheckKind::NestedIfStatements, | ||
Range::from_located(stmt), | ||
)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
pub use bool_ops::{a_and_not_a, a_or_not_a, and_false, or_true}; | ||
pub use ast_bool_op::{a_and_not_a, a_or_not_a, and_false, or_true}; | ||
pub use ast_if::nested_if_statements; | ||
pub use key_in_dict::{key_in_dict_compare, key_in_dict_for}; | ||
pub use use_contextlib_suppress::use_contextlib_suppress; | ||
pub use yoda_conditions::yoda_conditions; | ||
|
||
mod bool_ops; | ||
mod ast_bool_op; | ||
mod ast_if; | ||
mod key_in_dict; | ||
mod use_contextlib_suppress; | ||
mod yoda_conditions; |
23 changes: 23 additions & 0 deletions
23
src/flake8_simplify/snapshots/ruff__flake8_simplify__tests__SIM102_SIM102.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
source: src/flake8_simplify/mod.rs | ||
expression: checks | ||
--- | ||
- kind: NestedIfStatements | ||
location: | ||
row: 1 | ||
column: 0 | ||
end_location: | ||
row: 3 | ||
column: 9 | ||
fix: ~ | ||
parent: ~ | ||
- kind: NestedIfStatements | ||
location: | ||
row: 8 | ||
column: 0 | ||
end_location: | ||
row: 10 | ||
column: 9 | ||
fix: ~ | ||
parent: ~ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters