Skip to content

Commit 84cead4

Browse files
committed
Enhance BLE001: fix docs; light speed-up; improve readability
1 parent 351121c commit 84cead4

File tree

3 files changed

+70
-52
lines changed

3 files changed

+70
-52
lines changed

crates/ruff_linter/resources/test/fixtures/flake8_blind_except/BLE.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@
154154
except Exception as e:
155155
raise ValueError from e
156156

157+
try:
158+
...
159+
except Exception as e:
160+
raise e from ValueError("hello")
161+
157162

158163
try:
159164
pass
@@ -245,3 +250,9 @@
245250
pass
246251
except (Exception, ValueError) as e:
247252
raise e
253+
254+
# `from None` cause
255+
try:
256+
pass
257+
except BaseException as e:
258+
raise e from None

crates/ruff_linter/src/rules/flake8_blind_except/rules/blind_except.rs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::checkers::ast::Checker;
1111

1212
/// ## What it does
1313
/// Checks for `except` clauses that catch all exceptions. This includes
14-
/// bare `except`, `except BaseException` and `except Exception`.
14+
/// `except BaseException` and `except Exception`.
1515
///
1616
///
1717
/// ## Why is this bad?
@@ -149,24 +149,28 @@ impl<'a> ReraiseVisitor<'a> {
149149

150150
impl<'a> StatementVisitor<'a> for ReraiseVisitor<'a> {
151151
fn visit_stmt(&mut self, stmt: &'a Stmt) {
152+
if self.seen {
153+
return;
154+
}
152155
match stmt {
153156
Stmt::Raise(ast::StmtRaise { exc, cause, .. }) => {
154-
if let Some(cause) = cause {
155-
if let Expr::Name(ast::ExprName { id, .. }) = cause.as_ref() {
156-
if self.name.is_some_and(|name| id == name) {
157-
self.seen = true;
158-
}
157+
// except Exception [as <name>]:
158+
// raise [<exc> [from <cause>]]
159+
let reraised = match (self.name, exc.as_deref(), cause.as_deref()) {
160+
// `raise`
161+
(_, None, None) => true,
162+
// `raise SomeExc from <name>`
163+
(Some(name), _, Some(Expr::Name(ast::ExprName { id, .. }))) if name == id => {
164+
true
159165
}
160-
} else {
161-
if let Some(exc) = exc {
162-
if let Expr::Name(ast::ExprName { id, .. }) = exc.as_ref() {
163-
if self.name.is_some_and(|name| id == name) {
164-
self.seen = true;
165-
}
166-
}
167-
} else {
168-
self.seen = true;
166+
// `raise <name>` and `raise <name> from SomeCause`
167+
(Some(name), Some(Expr::Name(ast::ExprName { id, .. })), _) if name == id => {
168+
true
169169
}
170+
_ => false,
171+
};
172+
if reraised {
173+
self.seen = true;
170174
}
171175
}
172176
Stmt::Try(_) | Stmt::FunctionDef(_) | Stmt::ClassDef(_) => {}
@@ -200,6 +204,9 @@ impl<'a> LogExceptionVisitor<'a> {
200204

201205
impl<'a> StatementVisitor<'a> for LogExceptionVisitor<'a> {
202206
fn visit_stmt(&mut self, stmt: &'a Stmt) {
207+
if self.seen {
208+
return;
209+
}
203210
match stmt {
204211
Stmt::Expr(ast::StmtExpr { value, .. }) => {
205212
if let Expr::Call(ast::ExprCall {

crates/ruff_linter/src/rules/flake8_blind_except/snapshots/ruff_linter__rules__flake8_blind_except__tests__BLE001_BLE.py.snap

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -148,92 +148,92 @@ BLE.py:131:8: BLE001 Do not catch blind exception: `Exception`
148148
132 | critical("...", exc_info=None)
149149
|
150150

151-
BLE.py:169:9: BLE001 Do not catch blind exception: `Exception`
152-
|
153-
167 | try:
154-
168 | pass
155-
169 | except (Exception,):
156-
| ^^^^^^^^^ BLE001
157-
170 | pass
158-
|
159-
160151
BLE.py:174:9: BLE001 Do not catch blind exception: `Exception`
161152
|
162153
172 | try:
163154
173 | pass
164-
174 | except (Exception, ValueError):
155+
174 | except (Exception,):
165156
| ^^^^^^^^^ BLE001
166157
175 | pass
167158
|
168159

169-
BLE.py:179:21: BLE001 Do not catch blind exception: `Exception`
160+
BLE.py:179:9: BLE001 Do not catch blind exception: `Exception`
170161
|
171162
177 | try:
172163
178 | pass
173-
179 | except (ValueError, Exception):
174-
| ^^^^^^^^^ BLE001
164+
179 | except (Exception, ValueError):
165+
| ^^^^^^^^^ BLE001
175166
180 | pass
176167
|
177168

178169
BLE.py:184:21: BLE001 Do not catch blind exception: `Exception`
179170
|
180171
182 | try:
181172
183 | pass
182-
184 | except (ValueError, Exception) as e:
173+
184 | except (ValueError, Exception):
183174
| ^^^^^^^^^ BLE001
184-
185 | print(e)
175+
185 | pass
185176
|
186177

187-
BLE.py:189:9: BLE001 Do not catch blind exception: `BaseException`
178+
BLE.py:189:21: BLE001 Do not catch blind exception: `Exception`
188179
|
189180
187 | try:
190181
188 | pass
191-
189 | except (BaseException, TypeError):
192-
| ^^^^^^^^^^^^^ BLE001
193-
190 | pass
182+
189 | except (ValueError, Exception) as e:
183+
| ^^^^^^^^^ BLE001
184+
190 | print(e)
194185
|
195186

196-
BLE.py:194:20: BLE001 Do not catch blind exception: `BaseException`
187+
BLE.py:194:9: BLE001 Do not catch blind exception: `BaseException`
197188
|
198189
192 | try:
199190
193 | pass
200-
194 | except (TypeError, BaseException):
201-
| ^^^^^^^^^^^^^ BLE001
191+
194 | except (BaseException, TypeError):
192+
| ^^^^^^^^^^^^^ BLE001
202193
195 | pass
203194
|
204195

205-
BLE.py:199:9: BLE001 Do not catch blind exception: `Exception`
196+
BLE.py:199:20: BLE001 Do not catch blind exception: `BaseException`
206197
|
207198
197 | try:
208199
198 | pass
209-
199 | except (Exception, BaseException):
210-
| ^^^^^^^^^ BLE001
200+
199 | except (TypeError, BaseException):
201+
| ^^^^^^^^^^^^^ BLE001
211202
200 | pass
212203
|
213204

214-
BLE.py:204:9: BLE001 Do not catch blind exception: `BaseException`
205+
BLE.py:204:9: BLE001 Do not catch blind exception: `Exception`
215206
|
216207
202 | try:
217208
203 | pass
218-
204 | except (BaseException, Exception):
219-
| ^^^^^^^^^^^^^ BLE001
209+
204 | except (Exception, BaseException):
210+
| ^^^^^^^^^ BLE001
220211
205 | pass
221212
|
222213

223-
BLE.py:210:10: BLE001 Do not catch blind exception: `Exception`
214+
BLE.py:209:9: BLE001 Do not catch blind exception: `BaseException`
224215
|
225-
208 | try:
226-
209 | pass
227-
210 | except ((Exception, ValueError), TypeError):
228-
| ^^^^^^^^^ BLE001
229-
211 | pass
216+
207 | try:
217+
208 | pass
218+
209 | except (BaseException, Exception):
219+
| ^^^^^^^^^^^^^ BLE001
220+
210 | pass
230221
|
231222

232-
BLE.py:215:22: BLE001 Do not catch blind exception: `BaseException`
223+
BLE.py:215:10: BLE001 Do not catch blind exception: `Exception`
233224
|
234225
213 | try:
235226
214 | pass
236-
215 | except (ValueError, (BaseException, TypeError)):
237-
| ^^^^^^^^^^^^^ BLE001
227+
215 | except ((Exception, ValueError), TypeError):
228+
| ^^^^^^^^^ BLE001
238229
216 | pass
239230
|
231+
232+
BLE.py:220:22: BLE001 Do not catch blind exception: `BaseException`
233+
|
234+
218 | try:
235+
219 | pass
236+
220 | except (ValueError, (BaseException, TypeError)):
237+
| ^^^^^^^^^^^^^ BLE001
238+
221 | pass
239+
|

0 commit comments

Comments
 (0)