Skip to content

Commit

Permalink
Redirect TRY200 to B904 (#9755)
Browse files Browse the repository at this point in the history
Follow-up to #9754 and #9689. Alternative to #9714.

Marks `TRY200` as removed and redirects to `B904` instead of marking as
deprecated and suggesting `B904` instead.
  • Loading branch information
zanieb committed Feb 1, 2024
1 parent 003a360 commit f6812ee
Show file tree
Hide file tree
Showing 7 changed files with 5 additions and 93 deletions.
40 changes: 0 additions & 40 deletions crates/ruff_linter/resources/test/fixtures/tryceratops/TRY200.py

This file was deleted.

4 changes: 0 additions & 4 deletions crates/ruff_linter/src/checkers/ast/analyze/except_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_linter/src/codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
1 change: 1 addition & 0 deletions crates/ruff_linter/src/rule_redirects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ static REDIRECTS: Lazy<HashMap<&'static str, &'static str>> = Lazy::new(|| {
("T004", "FIX004"),
("RUF011", "B035"),
("TCH006", "TCH010"),
("TRY200", "B904"),
// Test redirect by exact code
#[cfg(feature = "test-rules")]
("RUF940", "RUF950"),
Expand Down
1 change: 0 additions & 1 deletion crates/ruff_linter/src/rules/tryceratops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))]
Expand Down
30 changes: 3 additions & 27 deletions crates/ruff_linter/src/rules/tryceratops/rules/reraise_no_cause.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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));
}
}
}
}

This file was deleted.

0 comments on commit f6812ee

Please sign in to comment.