diff --git a/crates/ruff_linter/resources/test/fixtures/tryceratops/TRY200.py b/crates/ruff_linter/resources/test/fixtures/tryceratops/TRY200.py deleted file mode 100644 index 4b00c3cef0686..0000000000000 --- a/crates/ruff_linter/resources/test/fixtures/tryceratops/TRY200.py +++ /dev/null @@ -1,40 +0,0 @@ -""" -Violation: -Reraise without using 'from' -""" - - -class MyException(Exception): - pass - - -def func(): - try: - a = 1 - except Exception: - raise MyException() - - -def func(): - try: - a = 1 - except Exception: - if True: - raise MyException() - - -def good(): - try: - a = 1 - except MyException as e: - raise e # This is verbose violation, shouldn't trigger no cause - except Exception: - raise # Just re-raising don't need 'from' - - -def good(): - try: - from mod import f - except ImportError: - def f(): - raise MyException() # Raising within a new scope is fine diff --git a/crates/ruff_linter/src/checkers/ast/analyze/except_handler.rs b/crates/ruff_linter/src/checkers/ast/analyze/except_handler.rs index d278f4c8f016b..2e39f0f965f3f 100644 --- a/crates/ruff_linter/src/checkers/ast/analyze/except_handler.rs +++ b/crates/ruff_linter/src/checkers/ast/analyze/except_handler.rs @@ -5,7 +5,6 @@ use crate::checkers::ast::Checker; use crate::registry::Rule; use crate::rules::{ flake8_bandit, flake8_blind_except, flake8_bugbear, flake8_builtins, pycodestyle, pylint, - tryceratops, }; /// Run lint rules over an [`ExceptHandler`] syntax node. @@ -66,9 +65,6 @@ pub(crate) fn except_handler(except_handler: &ExceptHandler, checker: &mut Check if checker.enabled(Rule::ExceptWithNonExceptionClasses) { flake8_bugbear::rules::except_with_non_exception_classes(checker, except_handler); } - if checker.enabled(Rule::ReraiseNoCause) { - tryceratops::rules::reraise_no_cause(checker, body); - } if checker.enabled(Rule::BinaryOpException) { pylint::rules::binary_op_exception(checker, except_handler); } diff --git a/crates/ruff_linter/src/codes.rs b/crates/ruff_linter/src/codes.rs index f318e6e7a3ed0..19da7d8aef40d 100644 --- a/crates/ruff_linter/src/codes.rs +++ b/crates/ruff_linter/src/codes.rs @@ -847,7 +847,7 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<(RuleGroup, Rule)> { (Tryceratops, "002") => (RuleGroup::Stable, rules::tryceratops::rules::RaiseVanillaClass), (Tryceratops, "003") => (RuleGroup::Stable, rules::tryceratops::rules::RaiseVanillaArgs), (Tryceratops, "004") => (RuleGroup::Stable, rules::tryceratops::rules::TypeCheckWithoutTypeError), - (Tryceratops, "200") => (RuleGroup::Deprecated, rules::tryceratops::rules::ReraiseNoCause), + (Tryceratops, "200") => (RuleGroup::Removed, rules::tryceratops::rules::ReraiseNoCause), (Tryceratops, "201") => (RuleGroup::Stable, rules::tryceratops::rules::VerboseRaise), (Tryceratops, "300") => (RuleGroup::Stable, rules::tryceratops::rules::TryConsiderElse), (Tryceratops, "301") => (RuleGroup::Stable, rules::tryceratops::rules::RaiseWithinTry), diff --git a/crates/ruff_linter/src/rule_redirects.rs b/crates/ruff_linter/src/rule_redirects.rs index 87330b9ae3fc2..8cd7cb401dcf9 100644 --- a/crates/ruff_linter/src/rule_redirects.rs +++ b/crates/ruff_linter/src/rule_redirects.rs @@ -100,6 +100,7 @@ static REDIRECTS: Lazy> = Lazy::new(|| { ("T004", "FIX004"), ("RUF011", "B035"), ("TCH006", "TCH010"), + ("TRY200", "B904"), // Test redirect by exact code #[cfg(feature = "test-rules")] ("RUF940", "RUF950"), diff --git a/crates/ruff_linter/src/rules/tryceratops/mod.rs b/crates/ruff_linter/src/rules/tryceratops/mod.rs index c8fd7f3ae0dae..21eaa92962639 100644 --- a/crates/ruff_linter/src/rules/tryceratops/mod.rs +++ b/crates/ruff_linter/src/rules/tryceratops/mod.rs @@ -18,7 +18,6 @@ mod tests { #[test_case(Rule::RaiseVanillaClass, Path::new("TRY002.py"))] #[test_case(Rule::RaiseVanillaArgs, Path::new("TRY003.py"))] #[test_case(Rule::TypeCheckWithoutTypeError, Path::new("TRY004.py"))] - #[test_case(Rule::ReraiseNoCause, Path::new("TRY200.py"))] #[test_case(Rule::VerboseRaise, Path::new("TRY201.py"))] #[test_case(Rule::TryConsiderElse, Path::new("TRY300.py"))] #[test_case(Rule::RaiseWithinTry, Path::new("TRY301.py"))] diff --git a/crates/ruff_linter/src/rules/tryceratops/rules/reraise_no_cause.rs b/crates/ruff_linter/src/rules/tryceratops/rules/reraise_no_cause.rs index 19317109888a5..0de0ae4249d57 100644 --- a/crates/ruff_linter/src/rules/tryceratops/rules/reraise_no_cause.rs +++ b/crates/ruff_linter/src/rules/tryceratops/rules/reraise_no_cause.rs @@ -1,13 +1,7 @@ -use ruff_python_ast::{Expr, Stmt}; - -use ruff_diagnostics::{Diagnostic, Violation}; +use ruff_diagnostics::Violation; use ruff_macros::{derive_message_formats, violation}; -use ruff_python_ast::helpers::RaiseStatementVisitor; -use ruff_python_ast::statement_visitor::StatementVisitor; - -use crate::checkers::ast::Checker; -/// ## Deprecation +/// ## Removed /// This rule is identical to [B904] which should be used instead. /// /// ## What it does @@ -44,28 +38,10 @@ use crate::checkers::ast::Checker; #[violation] pub struct ReraiseNoCause; +/// TRY200 impl Violation for ReraiseNoCause { #[derive_message_formats] fn message(&self) -> String { format!("Use `raise from` to specify exception cause") } } - -/// TRY200 -pub(crate) fn reraise_no_cause(checker: &mut Checker, body: &[Stmt]) { - let raises = { - let mut visitor = RaiseStatementVisitor::default(); - visitor.visit_body(body); - visitor.raises - }; - - for (range, exc, cause) in raises { - if cause.is_none() { - if exc.is_some_and(Expr::is_call_expr) { - checker - .diagnostics - .push(Diagnostic::new(ReraiseNoCause, range)); - } - } - } -} diff --git a/crates/ruff_linter/src/rules/tryceratops/snapshots/ruff_linter__rules__tryceratops__tests__reraise-no-cause_TRY200.py.snap b/crates/ruff_linter/src/rules/tryceratops/snapshots/ruff_linter__rules__tryceratops__tests__reraise-no-cause_TRY200.py.snap deleted file mode 100644 index dd34fcc5528e3..0000000000000 --- a/crates/ruff_linter/src/rules/tryceratops/snapshots/ruff_linter__rules__tryceratops__tests__reraise-no-cause_TRY200.py.snap +++ /dev/null @@ -1,20 +0,0 @@ ---- -source: crates/ruff_linter/src/rules/tryceratops/mod.rs ---- -TRY200.py:15:9: TRY200 Use `raise from` to specify exception cause - | -13 | a = 1 -14 | except Exception: -15 | raise MyException() - | ^^^^^^^^^^^^^^^^^^^ TRY200 - | - -TRY200.py:23:13: TRY200 Use `raise from` to specify exception cause - | -21 | except Exception: -22 | if True: -23 | raise MyException() - | ^^^^^^^^^^^^^^^^^^^ TRY200 - | - -