feat(B029): Add B029 from flake8-bugbear (#3068)

This commit is contained in:
Carlos Gonçalves 2023-02-20 20:57:13 +00:00 committed by GitHub
parent f72ed255e5
commit b657468346
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 84 additions and 0 deletions

View file

@ -0,0 +1,14 @@
"""
Should emit:
B029 - on lines 8 and 13
"""
try:
pass
except ():
pass
try:
pass
except () as e:
pass

View file

@ -3708,6 +3708,9 @@ where
self.settings.flake8_bandit.check_typed_exception,
);
}
if self.settings.rules.enabled(&Rule::ExceptWithEmptyTuple) {
flake8_bugbear::rules::except_with_empty_tuple(self, excepthandler);
}
if self.settings.rules.enabled(&Rule::ReraiseNoCause) {
tryceratops::rules::reraise_no_cause(self, body);
}

View file

@ -179,6 +179,7 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<Rule> {
(Flake8Bugbear, "025") => Rule::DuplicateTryBlockException,
(Flake8Bugbear, "026") => Rule::StarArgUnpackingAfterKeywordArg,
(Flake8Bugbear, "027") => Rule::EmptyMethodWithoutAbstractDecorator,
(Flake8Bugbear, "029") => Rule::ExceptWithEmptyTuple,
(Flake8Bugbear, "904") => Rule::RaiseWithoutFromInsideExcept,
(Flake8Bugbear, "905") => Rule::ZipWithoutExplicitStrict,

View file

@ -182,6 +182,7 @@ ruff_macros::register_rules!(
rules::flake8_bugbear::rules::EmptyMethodWithoutAbstractDecorator,
rules::flake8_bugbear::rules::RaiseWithoutFromInsideExcept,
rules::flake8_bugbear::rules::ZipWithoutExplicitStrict,
rules::flake8_bugbear::rules::ExceptWithEmptyTuple,
// flake8-blind-except
rules::flake8_blind_except::rules::BlindExcept,
// flake8-comprehensions

View file

@ -41,6 +41,7 @@ mod tests {
#[test_case(Rule::StarArgUnpackingAfterKeywordArg, Path::new("B026.py"); "B026")]
#[test_case(Rule::EmptyMethodWithoutAbstractDecorator, Path::new("B027.py"); "B027")]
#[test_case(Rule::EmptyMethodWithoutAbstractDecorator, Path::new("B027.pyi"); "B027_pyi")]
#[test_case(Rule::ExceptWithEmptyTuple, Path::new("B029.py"); "B029")]
#[test_case(Rule::RaiseWithoutFromInsideExcept, Path::new("B904.py"); "B904")]
#[test_case(Rule::ZipWithoutExplicitStrict, Path::new("B905.py"); "B905")]
fn rules(rule_code: Rule, path: &Path) -> Result<()> {

View file

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

View file

@ -10,6 +10,7 @@ pub use cannot_raise_literal::{cannot_raise_literal, CannotRaiseLiteral};
pub use duplicate_exceptions::{
duplicate_exceptions, DuplicateHandlerException, DuplicateTryBlockException,
};
pub use except_with_empty_tuple::{except_with_empty_tuple, ExceptWithEmptyTuple};
pub use f_string_docstring::{f_string_docstring, FStringDocstring};
pub use function_call_argument_default::{
function_call_argument_default, FunctionCallArgumentDefault,
@ -47,6 +48,7 @@ mod assignment_to_os_environ;
mod cached_instance_method;
mod cannot_raise_literal;
mod duplicate_exceptions;
mod except_with_empty_tuple;
mod f_string_docstring;
mod function_call_argument_default;
mod function_uses_loop_variable;

View file

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