diff --git a/resources/test/fixtures/tryceratops/TRY200.py b/resources/test/fixtures/tryceratops/TRY200.py index 55d366e3bd..825b97ee51 100644 --- a/resources/test/fixtures/tryceratops/TRY200.py +++ b/resources/test/fixtures/tryceratops/TRY200.py @@ -1,27 +1,32 @@ +""" +Violation: +Reraise without using 'from' +""" + + class MyException(Exception): pass -class MainFunctionFailed(Exception): - pass - - -def process(): - raise MyException - - -def bad(): +def func(): try: - process() - except MyException: - raise MainFunctionFailed() + a = 1 + except Exception: + raise MyException() + +def func(): + try: + a = 1 + except Exception: if True: - raise MainFunctionFailed() + raise MyException() def good(): try: - process() - except MyException as ex: - raise MainFunctionFailed() from ex + 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' diff --git a/src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B007_B007.py.snap b/src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B007_B007.py.snap index f441bb8b23..cc2ee892bc 100644 --- a/src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B007_B007.py.snap +++ b/src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B007_B007.py.snap @@ -3,7 +3,9 @@ source: src/rules/flake8_bugbear/mod.rs expression: diagnostics --- - kind: - UnusedLoopControlVariable: i + UnusedLoopControlVariable: + name: i + safe: true location: row: 6 column: 4 @@ -20,7 +22,9 @@ expression: diagnostics column: 5 parent: ~ - kind: - UnusedLoopControlVariable: k + UnusedLoopControlVariable: + name: k + safe: true location: row: 18 column: 12 @@ -37,7 +41,9 @@ expression: diagnostics column: 13 parent: ~ - kind: - UnusedLoopControlVariable: i + UnusedLoopControlVariable: + name: i + safe: true location: row: 30 column: 4 @@ -54,7 +60,9 @@ expression: diagnostics column: 5 parent: ~ - kind: - UnusedLoopControlVariable: k + UnusedLoopControlVariable: + name: k + safe: true location: row: 30 column: 12 @@ -70,4 +78,52 @@ expression: diagnostics row: 30 column: 13 parent: ~ +- kind: + UnusedLoopControlVariable: + name: bar + safe: false + location: + row: 34 + column: 9 + end_location: + row: 34 + column: 12 + fix: ~ + parent: ~ +- kind: + UnusedLoopControlVariable: + name: bar + safe: false + location: + row: 38 + column: 9 + end_location: + row: 38 + column: 12 + fix: ~ + parent: ~ +- kind: + UnusedLoopControlVariable: + name: bar + safe: false + location: + row: 42 + column: 9 + end_location: + row: 42 + column: 12 + fix: ~ + parent: ~ +- kind: + UnusedLoopControlVariable: + name: bar + safe: false + location: + row: 46 + column: 9 + end_location: + row: 46 + column: 12 + fix: ~ + parent: ~ diff --git a/src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B007_B007.py.snap.new b/src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B007_B007.py.snap.new deleted file mode 100644 index 1fee3a28b4..0000000000 --- a/src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B007_B007.py.snap.new +++ /dev/null @@ -1,130 +0,0 @@ ---- -source: src/rules/flake8_bugbear/mod.rs -assertion_line: 52 -expression: diagnostics ---- -- kind: - UnusedLoopControlVariable: - name: i - safe: true - location: - row: 6 - column: 4 - end_location: - row: 6 - column: 5 - fix: - content: _i - location: - row: 6 - column: 4 - end_location: - row: 6 - column: 5 - parent: ~ -- kind: - UnusedLoopControlVariable: - name: k - safe: true - location: - row: 18 - column: 12 - end_location: - row: 18 - column: 13 - fix: - content: _k - location: - row: 18 - column: 12 - end_location: - row: 18 - column: 13 - parent: ~ -- kind: - UnusedLoopControlVariable: - name: i - safe: true - location: - row: 30 - column: 4 - end_location: - row: 30 - column: 5 - fix: - content: _i - location: - row: 30 - column: 4 - end_location: - row: 30 - column: 5 - parent: ~ -- kind: - UnusedLoopControlVariable: - name: k - safe: true - location: - row: 30 - column: 12 - end_location: - row: 30 - column: 13 - fix: - content: _k - location: - row: 30 - column: 12 - end_location: - row: 30 - column: 13 - parent: ~ -- kind: - UnusedLoopControlVariable: - name: bar - safe: false - location: - row: 34 - column: 9 - end_location: - row: 34 - column: 12 - fix: ~ - parent: ~ -- kind: - UnusedLoopControlVariable: - name: bar - safe: false - location: - row: 38 - column: 9 - end_location: - row: 38 - column: 12 - fix: ~ - parent: ~ -- kind: - UnusedLoopControlVariable: - name: bar - safe: false - location: - row: 42 - column: 9 - end_location: - row: 42 - column: 12 - fix: ~ - parent: ~ -- kind: - UnusedLoopControlVariable: - name: bar - safe: false - location: - row: 46 - column: 9 - end_location: - row: 46 - column: 12 - fix: ~ - parent: ~ - diff --git a/src/rules/tryceratops/rules/reraise_no_cause.rs b/src/rules/tryceratops/rules/reraise_no_cause.rs index d435f9a224..178642f0eb 100644 --- a/src/rules/tryceratops/rules/reraise_no_cause.rs +++ b/src/rules/tryceratops/rules/reraise_no_cause.rs @@ -1,5 +1,5 @@ use ruff_macros::derive_message_formats; -use rustpython_ast::{Stmt, StmtKind}; +use rustpython_ast::{ExprKind, Stmt, StmtKind}; use crate::ast::types::Range; use crate::ast::visitor::{self, Visitor}; @@ -46,8 +46,12 @@ pub fn reraise_no_cause(checker: &mut Checker, body: &[Stmt]) { }; for stmt in raises { - if let StmtKind::Raise { cause, .. } = &stmt.node { - if cause.is_none() { + if let StmtKind::Raise { exc, cause, .. } = &stmt.node { + if exc + .as_ref() + .map_or(false, |expr| matches!(expr.node, ExprKind::Call { .. })) + && cause.is_none() + { checker .diagnostics .push(Diagnostic::new(ReraiseNoCause, Range::from_located(stmt))); diff --git a/src/rules/tryceratops/snapshots/ruff__rules__tryceratops__tests__reraise-no-cause_TRY200.py.snap b/src/rules/tryceratops/snapshots/ruff__rules__tryceratops__tests__reraise-no-cause_TRY200.py.snap index 3ae3d4e3be..9a452955c6 100644 --- a/src/rules/tryceratops/snapshots/ruff__rules__tryceratops__tests__reraise-no-cause_TRY200.py.snap +++ b/src/rules/tryceratops/snapshots/ruff__rules__tryceratops__tests__reraise-no-cause_TRY200.py.snap @@ -5,21 +5,21 @@ expression: diagnostics - kind: ReraiseNoCause: ~ location: - row: 17 + row: 15 column: 8 end_location: - row: 17 - column: 34 + row: 15 + column: 27 fix: ~ parent: ~ - kind: ReraiseNoCause: ~ location: - row: 20 + row: 23 column: 12 end_location: - row: 20 - column: 38 + row: 23 + column: 31 fix: ~ parent: ~