Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement flake8-simplify SIM105 rule
- Loading branch information
Showing
10 changed files
with
119 additions
and
1 deletion.
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,24 @@ | ||
def foo(): | ||
pass | ||
|
||
try: | ||
foo() | ||
except ValueError: # SIM105 | ||
pass | ||
|
||
try: | ||
foo() | ||
except (ValueError, OSError): # SIM105 | ||
pass | ||
|
||
try: | ||
foo() | ||
except: # SIM105 | ||
pass | ||
|
||
try: | ||
foo() | ||
except ValueError: | ||
print('foo') | ||
except OSError: | ||
pass |
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 |
---|---|---|
|
@@ -895,6 +895,8 @@ | |
"S107", | ||
"SIM", | ||
"SIM1", | ||
"SIM10", | ||
"SIM105", | ||
"SIM11", | ||
"SIM118", | ||
"SIM2", | ||
|
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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
pub use bool_ops::{and_false, or_true}; | ||
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 key_in_dict; | ||
mod use_contextlib_suppress; | ||
mod yoda_conditions; |
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,34 @@ | ||
use rustpython_ast::{Excepthandler, ExcepthandlerKind, Stmt}; | ||
|
||
use crate::ast::helpers; | ||
use crate::ast::types::Range; | ||
use crate::checkers::ast::Checker; | ||
use crate::registry::{Check, CheckKind}; | ||
|
||
/// SIM105 | ||
pub fn use_contextlib_suppress(checker: &mut Checker, try_: &Stmt, handlers: &[Excepthandler]) { | ||
if handlers.len() != 1 { | ||
return; | ||
} | ||
let handler = &handlers[0]; | ||
let ExcepthandlerKind::ExceptHandler { body, .. } = &handler.node; | ||
if body.len() == 1 { | ||
let node = &body[0].node; | ||
if matches!(node, rustpython_ast::StmtKind::Pass) { | ||
let handler_names: Vec<_> = helpers::extract_handler_names(handlers) | ||
.into_iter() | ||
.flatten() | ||
.collect(); | ||
let exception = if handler_names.is_empty() { | ||
"Exception".to_string() | ||
} else { | ||
handler_names.join(", ") | ||
}; | ||
let check = Check::new( | ||
CheckKind::UseContextlibSuppress(exception), | ||
Range::from_located(try_), | ||
); | ||
checker.add_check(check); | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/flake8_simplify/snapshots/ruff__flake8_simplify__tests__SIM105_SIM105.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,35 @@ | ||
--- | ||
source: src/flake8_simplify/mod.rs | ||
expression: checks | ||
--- | ||
- kind: | ||
UseContextlibSuppress: ValueError | ||
location: | ||
row: 4 | ||
column: 0 | ||
end_location: | ||
row: 7 | ||
column: 8 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
UseContextlibSuppress: "ValueError, OSError" | ||
location: | ||
row: 9 | ||
column: 0 | ||
end_location: | ||
row: 12 | ||
column: 8 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
UseContextlibSuppress: Exception | ||
location: | ||
row: 14 | ||
column: 0 | ||
end_location: | ||
row: 17 | ||
column: 8 | ||
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