Avoid reraise-no-cause for explicit reraises (#2174)

This commit is contained in:
Charlie Marsh 2023-01-25 15:51:24 -05:00 committed by GitHub
parent 8e1fac620e
commit d5dff11d4b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 94 additions and 159 deletions

View file

@ -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'

View file

@ -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: ~

View file

@ -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: ~

View file

@ -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)));

View file

@ -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: ~