mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 11:59:35 +00:00
Avoid reraise-no-cause for explicit reraises (#2174)
This commit is contained in:
parent
8e1fac620e
commit
d5dff11d4b
5 changed files with 94 additions and 159 deletions
37
resources/test/fixtures/tryceratops/TRY200.py
vendored
37
resources/test/fixtures/tryceratops/TRY200.py
vendored
|
@ -1,27 +1,32 @@
|
||||||
|
"""
|
||||||
|
Violation:
|
||||||
|
Reraise without using 'from'
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
class MyException(Exception):
|
class MyException(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class MainFunctionFailed(Exception):
|
def func():
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def process():
|
|
||||||
raise MyException
|
|
||||||
|
|
||||||
|
|
||||||
def bad():
|
|
||||||
try:
|
try:
|
||||||
process()
|
a = 1
|
||||||
except MyException:
|
except Exception:
|
||||||
raise MainFunctionFailed()
|
raise MyException()
|
||||||
|
|
||||||
|
|
||||||
|
def func():
|
||||||
|
try:
|
||||||
|
a = 1
|
||||||
|
except Exception:
|
||||||
if True:
|
if True:
|
||||||
raise MainFunctionFailed()
|
raise MyException()
|
||||||
|
|
||||||
|
|
||||||
def good():
|
def good():
|
||||||
try:
|
try:
|
||||||
process()
|
a = 1
|
||||||
except MyException as ex:
|
except MyException as e:
|
||||||
raise MainFunctionFailed() from ex
|
raise e # This is verbose violation, shouldn't trigger no cause
|
||||||
|
except Exception:
|
||||||
|
raise # Just re-raising don't need 'from'
|
||||||
|
|
|
@ -3,7 +3,9 @@ source: src/rules/flake8_bugbear/mod.rs
|
||||||
expression: diagnostics
|
expression: diagnostics
|
||||||
---
|
---
|
||||||
- kind:
|
- kind:
|
||||||
UnusedLoopControlVariable: i
|
UnusedLoopControlVariable:
|
||||||
|
name: i
|
||||||
|
safe: true
|
||||||
location:
|
location:
|
||||||
row: 6
|
row: 6
|
||||||
column: 4
|
column: 4
|
||||||
|
@ -20,7 +22,9 @@ expression: diagnostics
|
||||||
column: 5
|
column: 5
|
||||||
parent: ~
|
parent: ~
|
||||||
- kind:
|
- kind:
|
||||||
UnusedLoopControlVariable: k
|
UnusedLoopControlVariable:
|
||||||
|
name: k
|
||||||
|
safe: true
|
||||||
location:
|
location:
|
||||||
row: 18
|
row: 18
|
||||||
column: 12
|
column: 12
|
||||||
|
@ -37,7 +41,9 @@ expression: diagnostics
|
||||||
column: 13
|
column: 13
|
||||||
parent: ~
|
parent: ~
|
||||||
- kind:
|
- kind:
|
||||||
UnusedLoopControlVariable: i
|
UnusedLoopControlVariable:
|
||||||
|
name: i
|
||||||
|
safe: true
|
||||||
location:
|
location:
|
||||||
row: 30
|
row: 30
|
||||||
column: 4
|
column: 4
|
||||||
|
@ -54,7 +60,9 @@ expression: diagnostics
|
||||||
column: 5
|
column: 5
|
||||||
parent: ~
|
parent: ~
|
||||||
- kind:
|
- kind:
|
||||||
UnusedLoopControlVariable: k
|
UnusedLoopControlVariable:
|
||||||
|
name: k
|
||||||
|
safe: true
|
||||||
location:
|
location:
|
||||||
row: 30
|
row: 30
|
||||||
column: 12
|
column: 12
|
||||||
|
@ -70,4 +78,52 @@ expression: diagnostics
|
||||||
row: 30
|
row: 30
|
||||||
column: 13
|
column: 13
|
||||||
parent: ~
|
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: ~
|
||||||
|
|
||||||
|
|
|
@ -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: ~
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use ruff_macros::derive_message_formats;
|
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::types::Range;
|
||||||
use crate::ast::visitor::{self, Visitor};
|
use crate::ast::visitor::{self, Visitor};
|
||||||
|
@ -46,8 +46,12 @@ pub fn reraise_no_cause(checker: &mut Checker, body: &[Stmt]) {
|
||||||
};
|
};
|
||||||
|
|
||||||
for stmt in raises {
|
for stmt in raises {
|
||||||
if let StmtKind::Raise { cause, .. } = &stmt.node {
|
if let StmtKind::Raise { exc, cause, .. } = &stmt.node {
|
||||||
if cause.is_none() {
|
if exc
|
||||||
|
.as_ref()
|
||||||
|
.map_or(false, |expr| matches!(expr.node, ExprKind::Call { .. }))
|
||||||
|
&& cause.is_none()
|
||||||
|
{
|
||||||
checker
|
checker
|
||||||
.diagnostics
|
.diagnostics
|
||||||
.push(Diagnostic::new(ReraiseNoCause, Range::from_located(stmt)));
|
.push(Diagnostic::new(ReraiseNoCause, Range::from_located(stmt)));
|
||||||
|
|
|
@ -5,21 +5,21 @@ expression: diagnostics
|
||||||
- kind:
|
- kind:
|
||||||
ReraiseNoCause: ~
|
ReraiseNoCause: ~
|
||||||
location:
|
location:
|
||||||
row: 17
|
row: 15
|
||||||
column: 8
|
column: 8
|
||||||
end_location:
|
end_location:
|
||||||
row: 17
|
row: 15
|
||||||
column: 34
|
column: 27
|
||||||
fix: ~
|
fix: ~
|
||||||
parent: ~
|
parent: ~
|
||||||
- kind:
|
- kind:
|
||||||
ReraiseNoCause: ~
|
ReraiseNoCause: ~
|
||||||
location:
|
location:
|
||||||
row: 20
|
row: 23
|
||||||
column: 12
|
column: 12
|
||||||
end_location:
|
end_location:
|
||||||
row: 20
|
row: 23
|
||||||
column: 38
|
column: 31
|
||||||
fix: ~
|
fix: ~
|
||||||
parent: ~
|
parent: ~
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue