-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
168 additions
and
0 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,54 @@ | ||
""" | ||
Violation: | ||
Raising an exception using its assigned name is verbose and unrequired | ||
""" | ||
import logging | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class MyException(Exception): | ||
pass | ||
|
||
|
||
def bad(): | ||
try: | ||
process() | ||
except MyException as e: | ||
logger.exception("process failed") | ||
raise e | ||
|
||
|
||
def good(): | ||
try: | ||
process() | ||
except MyException: | ||
logger.exception("process failed") | ||
raise | ||
|
||
def still_good(): | ||
try: | ||
process() | ||
except MyException as e: | ||
print(e) | ||
raise | ||
|
||
|
||
def bad_that_needs_recursion(): | ||
try: | ||
process() | ||
except MyException as e: | ||
logger.exception("process failed") | ||
if True: | ||
raise e | ||
|
||
|
||
def bad_that_needs_recursion_2(): | ||
try: | ||
process() | ||
except MyException as e: | ||
logger.exception("process failed") | ||
if True: | ||
def foo(): | ||
raise e |
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 |
---|---|---|
|
@@ -1782,6 +1782,9 @@ | |
"TRY0", | ||
"TRY00", | ||
"TRY004", | ||
"TRY2", | ||
"TRY20", | ||
"TRY201", | ||
"TRY3", | ||
"TRY30", | ||
"TRY300", | ||
|
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
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,5 +1,7 @@ | ||
pub use prefer_type_error::{prefer_type_error, PreferTypeError}; | ||
pub use try_consider_else::{try_consider_else, TryConsiderElse}; | ||
pub use verbose_raise::{verbose_raise, VerboseRaise}; | ||
|
||
mod prefer_type_error; | ||
mod try_consider_else; | ||
mod verbose_raise; |
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,68 @@ | ||
use ruff_macros::derive_message_formats; | ||
use rustpython_ast::{Excepthandler, ExcepthandlerKind, Expr, ExprKind, Stmt, StmtKind}; | ||
|
||
use crate::ast::types::Range; | ||
use crate::ast::visitor; | ||
use crate::ast::visitor::Visitor; | ||
use crate::checkers::ast::Checker; | ||
use crate::define_violation; | ||
use crate::registry::Diagnostic; | ||
use crate::violation::Violation; | ||
|
||
define_violation!( | ||
pub struct VerboseRaise; | ||
); | ||
impl Violation for VerboseRaise { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Use `raise` without specifying exception name") | ||
} | ||
} | ||
|
||
#[derive(Default)] | ||
struct RaiseStatementVisitor<'a> { | ||
raises: Vec<Option<&'a Expr>>, | ||
} | ||
|
||
impl<'a, 'b> Visitor<'b> for RaiseStatementVisitor<'a> | ||
where | ||
'b: 'a, | ||
{ | ||
fn visit_stmt(&mut self, stmt: &'b Stmt) { | ||
match &stmt.node { | ||
StmtKind::Raise { exc, .. } => self.raises.push(exc.as_ref().map(|expr| &**expr)), | ||
_ => visitor::walk_stmt(self, stmt), | ||
} | ||
} | ||
} | ||
|
||
/// TRY201 | ||
pub fn verbose_raise(checker: &mut Checker, handlers: &[Excepthandler]) { | ||
for handler in handlers { | ||
// If the handler assigned a name to the exception... | ||
if let ExcepthandlerKind::ExceptHandler { | ||
name: Some(exception_name), | ||
body, | ||
.. | ||
} = &handler.node | ||
{ | ||
let raises = { | ||
let mut visitor = RaiseStatementVisitor::default(); | ||
for stmt in body { | ||
visitor.visit_stmt(stmt); | ||
} | ||
visitor.raises | ||
}; | ||
for expr in raises.into_iter().flatten() { | ||
// ...and the raised object is bound to the same name... | ||
if let ExprKind::Name { id, .. } = &expr.node { | ||
if id == exception_name { | ||
checker | ||
.diagnostics | ||
.push(Diagnostic::new(VerboseRaise, Range::from_located(expr))); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...rules/tryceratops/snapshots/ruff__rules__tryceratops__tests__verbose-raise_TRY201.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/rules/tryceratops/mod.rs | ||
expression: diagnostics | ||
--- | ||
- kind: | ||
VerboseRaise: ~ | ||
location: | ||
row: 20 | ||
column: 14 | ||
end_location: | ||
row: 20 | ||
column: 15 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
VerboseRaise: ~ | ||
location: | ||
row: 44 | ||
column: 18 | ||
end_location: | ||
row: 44 | ||
column: 19 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
VerboseRaise: ~ | ||
location: | ||
row: 54 | ||
column: 22 | ||
end_location: | ||
row: 54 | ||
column: 23 | ||
fix: ~ | ||
parent: ~ | ||
|