-
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
127 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,29 @@ | ||
class MyException(Exception): | ||
pass | ||
|
||
|
||
def bad(): | ||
try: | ||
a = process() | ||
if not a: | ||
raise MyException(a) | ||
|
||
raise MyException(a) | ||
|
||
try: | ||
b = process() | ||
if not b: | ||
raise MyException(b) | ||
except Exception: | ||
logger.exception("something failed") | ||
except Exception: | ||
logger.exception("something failed") | ||
|
||
|
||
def good(): | ||
try: | ||
a = process() # This throws the exception now | ||
except MyException: | ||
logger.exception("a failed") | ||
except Exception: | ||
logger.exception("something failed") |
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 |
---|---|---|
|
@@ -1818,6 +1818,7 @@ | |
"TRY3", | ||
"TRY30", | ||
"TRY300", | ||
"TRY301", | ||
"TYP", | ||
"TYP0", | ||
"TYP00", | ||
|
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,9 +1,11 @@ | ||
pub use prefer_type_error::{prefer_type_error, PreferTypeError}; | ||
pub use raise_within_try::{raise_within_try, RaiseWithinTry}; | ||
pub use reraise_no_cause::{reraise_no_cause, ReraiseNoCause}; | ||
pub use try_consider_else::{try_consider_else, TryConsiderElse}; | ||
pub use verbose_raise::{verbose_raise, VerboseRaise}; | ||
|
||
mod prefer_type_error; | ||
mod raise_within_try; | ||
mod reraise_no_cause; | ||
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,54 @@ | ||
use ruff_macros::derive_message_formats; | ||
use rustpython_ast::{Stmt, StmtKind}; | ||
|
||
use crate::ast::types::Range; | ||
use crate::ast::visitor::{self, Visitor}; | ||
use crate::checkers::ast::Checker; | ||
use crate::define_violation; | ||
use crate::registry::Diagnostic; | ||
use crate::violation::Violation; | ||
|
||
define_violation!( | ||
pub struct RaiseWithinTry; | ||
); | ||
impl Violation for RaiseWithinTry { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Abstract `raise` to an inner function") | ||
} | ||
} | ||
|
||
#[derive(Default)] | ||
struct RaiseStatementVisitor<'a> { | ||
raises: Vec<&'a Stmt>, | ||
} | ||
|
||
impl<'a, 'b> Visitor<'b> for RaiseStatementVisitor<'a> | ||
where | ||
'b: 'a, | ||
{ | ||
fn visit_stmt(&mut self, stmt: &'b Stmt) { | ||
match stmt.node { | ||
StmtKind::Raise { .. } => self.raises.push(stmt), | ||
StmtKind::Try { .. } => (), | ||
_ => visitor::walk_stmt(self, stmt), | ||
} | ||
} | ||
} | ||
|
||
/// TRY301 | ||
pub fn raise_within_try(checker: &mut Checker, body: &[Stmt]) { | ||
let raises = { | ||
let mut visitor = RaiseStatementVisitor::default(); | ||
for stmt in body { | ||
visitor.visit_stmt(stmt); | ||
} | ||
visitor.raises | ||
}; | ||
|
||
for stmt in raises { | ||
checker | ||
.diagnostics | ||
.push(Diagnostic::new(RaiseWithinTry, Range::from_located(stmt))); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...es/tryceratops/snapshots/ruff__rules__tryceratops__tests__raise-within-try_TRY301.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: | ||
RaiseWithinTry: ~ | ||
location: | ||
row: 9 | ||
column: 12 | ||
end_location: | ||
row: 9 | ||
column: 32 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
RaiseWithinTry: ~ | ||
location: | ||
row: 11 | ||
column: 8 | ||
end_location: | ||
row: 11 | ||
column: 28 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
RaiseWithinTry: ~ | ||
location: | ||
row: 16 | ||
column: 16 | ||
end_location: | ||
row: 16 | ||
column: 36 | ||
fix: ~ | ||
parent: ~ | ||
|