Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement nested with detection (SIM117) (#1651)
- Loading branch information
1 parent
2f71bdf
commit fb8024a
Showing
9 changed files
with
88 additions
and
17 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,13 @@ | ||
with A() as a: # SIM117 | ||
with B() as b: | ||
print("hello") | ||
|
||
with A() as a: | ||
a() | ||
with B() as b: | ||
print("hello") | ||
|
||
with A() as a: | ||
with B() as b: | ||
print("hello") | ||
a() |
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
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,21 @@ | ||
use rustpython_ast::{Stmt, StmtKind}; | ||
|
||
use crate::ast::types::Range; | ||
use crate::checkers::ast::Checker; | ||
use crate::registry::{Check, CheckKind}; | ||
|
||
/// SIM117 | ||
pub fn multiple_with_statements(checker: &mut Checker, stmt: &Stmt) { | ||
let StmtKind::With { body, .. } = &stmt.node else { | ||
return; | ||
}; | ||
if body.len() != 1 { | ||
return; | ||
} | ||
if matches!(body[0].node, StmtKind::With { .. }) { | ||
checker.add_check(Check::new( | ||
CheckKind::MultipleWithStatements, | ||
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,11 +1,12 @@ | ||
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 ast_with::multiple_with_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 ast_bool_op; | ||
mod ast_if; | ||
mod ast_with; | ||
mod key_in_dict; | ||
mod use_contextlib_suppress; | ||
mod yoda_conditions; |
14 changes: 14 additions & 0 deletions
14
src/flake8_simplify/snapshots/ruff__flake8_simplify__tests__SIM117_SIM117.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,14 @@ | ||
--- | ||
source: src/flake8_simplify/mod.rs | ||
expression: checks | ||
--- | ||
- kind: MultipleWithStatements | ||
location: | ||
row: 1 | ||
column: 0 | ||
end_location: | ||
row: 3 | ||
column: 22 | ||
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