-
Notifications
You must be signed in to change notification settings - Fork 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
82 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,24 @@ | ||
class MyException(Exception): | ||
pass | ||
|
||
|
||
class MainFunctionFailed(Exception): | ||
pass | ||
|
||
|
||
def process(): | ||
raise MyException | ||
|
||
|
||
def bad(): | ||
try: | ||
process() | ||
except MyException: | ||
raise MainFunctionFailed() | ||
|
||
|
||
def good(): | ||
try: | ||
process() | ||
except MyException as ex: | ||
raise MainFunctionFailed() from ex |
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 |
---|---|---|
|
@@ -1751,6 +1751,9 @@ | |
"TRY0", | ||
"TRY00", | ||
"TRY004", | ||
"TRY2", | ||
"TRY20", | ||
"TRY200", | ||
"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 reraise_no_cause::{reraise_no_cause, ReraiseNoCause}; | ||
pub use try_consider_else::{try_consider_else, TryConsiderElse}; | ||
|
||
mod prefer_type_error; | ||
mod reraise_no_cause; | ||
mod try_consider_else; |
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,32 @@ | ||
use ruff_macros::derive_message_formats; | ||
use rustpython_ast::{Stmt, StmtKind}; | ||
|
||
use crate::ast::types::Range; | ||
use crate::checkers::ast::Checker; | ||
use crate::define_violation; | ||
use crate::registry::Diagnostic; | ||
use crate::violation::Violation; | ||
|
||
define_violation!( | ||
pub struct ReraiseNoCause; | ||
); | ||
impl Violation for ReraiseNoCause { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Use `raise from` to specify exception cause") | ||
} | ||
} | ||
|
||
/// TRY200 | ||
pub fn reraise_no_cause(checker: &mut Checker, body: &[Stmt]) { | ||
for stmt in body.iter() { | ||
if let StmtKind::Raise { cause, .. } = &stmt.node { | ||
if cause.is_none() { | ||
checker.diagnostics.push(Diagnostic::new( | ||
ReraiseNoCause, | ||
Range::from_located(stmt), | ||
)); | ||
} | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...es/tryceratops/snapshots/ruff__rules__tryceratops__tests__reraise-no-cause_TRY200.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,15 @@ | ||
--- | ||
source: src/rules/tryceratops/mod.rs | ||
expression: diagnostics | ||
--- | ||
- kind: | ||
ReraiseNoCause: ~ | ||
location: | ||
row: 17 | ||
column: 8 | ||
end_location: | ||
row: 17 | ||
column: 34 | ||
fix: ~ | ||
parent: ~ | ||
|