-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(B029): Add B029 from flake8-bugbear (#3068)
- Loading branch information
1 parent
f72ed25
commit b657468
Showing
9 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
crates/ruff/resources/test/fixtures/flake8_bugbear/B029.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
""" | ||
Should emit: | ||
B029 - on lines 8 and 13 | ||
""" | ||
|
||
try: | ||
pass | ||
except (): | ||
pass | ||
|
||
try: | ||
pass | ||
except () as e: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
crates/ruff/src/rules/flake8_bugbear/rules/except_with_empty_tuple.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use ruff_macros::{define_violation, derive_message_formats}; | ||
use rustpython_parser::ast::Excepthandler; | ||
|
||
use crate::ast::types::Range; | ||
use crate::checkers::ast::Checker; | ||
use crate::registry::Diagnostic; | ||
use crate::violation::Violation; | ||
|
||
use rustpython_parser::ast::{ExcepthandlerKind, ExprKind}; | ||
|
||
define_violation!( | ||
pub struct ExceptWithEmptyTuple; | ||
); | ||
impl Violation for ExceptWithEmptyTuple { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Using except (): with an empty tuple does not handle/catch anything. Add exceptions to handle.") | ||
} | ||
} | ||
|
||
/// B029 | ||
pub fn except_with_empty_tuple(checker: &mut Checker, excepthandler: &Excepthandler) { | ||
let ExcepthandlerKind::ExceptHandler { type_, .. } = &excepthandler.node; | ||
if type_.is_none() { | ||
return; | ||
} | ||
let ExprKind::Tuple { elts, .. } = &type_.as_ref().unwrap().node else { | ||
return; | ||
}; | ||
if elts.is_empty() { | ||
checker.diagnostics.push(Diagnostic::new( | ||
ExceptWithEmptyTuple, | ||
Range::from_located(excepthandler), | ||
)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
.../src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B029_B029.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_bugbear/mod.rs | ||
expression: diagnostics | ||
--- | ||
- kind: | ||
ExceptWithEmptyTuple: ~ | ||
location: | ||
row: 8 | ||
column: 0 | ||
end_location: | ||
row: 9 | ||
column: 8 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
ExceptWithEmptyTuple: ~ | ||
location: | ||
row: 13 | ||
column: 0 | ||
end_location: | ||
row: 14 | ||
column: 8 | ||
fix: ~ | ||
parent: ~ | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.