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 SIM107 (#1650)
- Loading branch information
Showing
10 changed files
with
106 additions
and
10 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,22 @@ | ||
# Bad: This example returns 3! | ||
def foo(): | ||
try: | ||
1 / 0 | ||
return "1" | ||
except: | ||
return "2" | ||
finally: | ||
return "3" | ||
|
||
|
||
# Good | ||
def foo(): | ||
return_value = None | ||
try: | ||
1 / 0 | ||
return_value = "1" | ||
except: | ||
return_value = "2" | ||
finally: | ||
return_value = "3" # you might also want to check if "except" happened | ||
return return_value |
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 |
---|---|---|
|
@@ -932,6 +932,7 @@ | |
"SIM10", | ||
"SIM102", | ||
"SIM105", | ||
"SIM107", | ||
"SIM11", | ||
"SIM117", | ||
"SIM118", | ||
|
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
34 changes: 34 additions & 0 deletions
34
src/flake8_simplify/plugins/return_in_try_except_finally.rs
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, StmtKind}; | ||
|
||
use crate::ast::types::Range; | ||
use crate::checkers::ast::Checker; | ||
use crate::registry::{Check, CheckKind}; | ||
|
||
fn find_return(stmts: &[Stmt]) -> Option<&Stmt> { | ||
stmts | ||
.iter() | ||
.find(|stmt| matches!(stmt.node, StmtKind::Return { .. })) | ||
} | ||
|
||
/// SIM107 | ||
pub fn return_in_try_except_finally( | ||
checker: &mut Checker, | ||
body: &[Stmt], | ||
handlers: &[Excepthandler], | ||
finalbody: &[Stmt], | ||
) { | ||
let try_has_return = find_return(body).is_some(); | ||
let except_has_return = handlers.iter().any(|handler| { | ||
let ExcepthandlerKind::ExceptHandler { body, .. } = &handler.node; | ||
find_return(body).is_some() | ||
}); | ||
|
||
if let Some(finally_return) = find_return(finalbody) { | ||
if try_has_return || except_has_return { | ||
checker.add_check(Check::new( | ||
CheckKind::ReturnInTryExceptFinally, | ||
Range::from_located(finally_return), | ||
)); | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/flake8_simplify/snapshots/ruff__flake8_simplify__tests__SIM107_SIM107.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: ReturnInTryExceptFinally | ||
location: | ||
row: 9 | ||
column: 8 | ||
end_location: | ||
row: 9 | ||
column: 18 | ||
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