-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(B029): Add B029 from flake8-bugbear #3068
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
24eec97
feat(B029): init add ExceptWithEmptyTuple
carlosmiei 3a2f049
add codes
carlosmiei 101ee9e
basic implementation
carlosmiei 4f249f0
change order
carlosmiei ff2cce7
rule logic
carlosmiei c0bbb55
add test
carlosmiei 7a6173a
remove comment
carlosmiei 7dae7f2
add scheme
carlosmiei e8f6c41
update generated files
carlosmiei 47fbbc6
Merge branch 'main' of github.com:charliermarsh/ruff into b029
carlosmiei fc57139
update test
carlosmiei 330e6e1
restore
carlosmiei File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you have
insta
installed? (cargo install cargo-insta
) You should be able to runcargo test --lib
, thencargo insta review
to review and check in the generated snapshot.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@charliermarsh yeah it's weird I did run
cargo test --all
andcargo insta review
but the file was not updated 🤔There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(result from running
test --all
andcargo insta review
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, you need to add the test to
crates/ruff/src/rules/flake8_bugbear/mod.rs
(hopefully the pattern is clear once you open up that file).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@charliermarsh nvm 🤦 I accidentally deleted it upon resolving the merge conflict here
will restore it asap
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Happens to the best of us :D