mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-19 01:51:30 +00:00
Avoid flagging typed exceptions in tuples (#2728)
This commit is contained in:
parent
6f58717ba4
commit
812b227334
5 changed files with 61 additions and 15 deletions
|
@ -8,7 +8,22 @@ try:
|
|||
except:
|
||||
continue
|
||||
|
||||
try:
|
||||
pass
|
||||
except (Exception,):
|
||||
continue
|
||||
|
||||
try:
|
||||
pass
|
||||
except (Exception, ValueError):
|
||||
continue
|
||||
|
||||
try:
|
||||
pass
|
||||
except ValueError:
|
||||
continue
|
||||
|
||||
try:
|
||||
pass
|
||||
except (ValueError,):
|
||||
continue
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
use rustpython_parser::ast::{Constant, Expr, ExprKind};
|
||||
|
||||
use crate::checkers::ast::Checker;
|
||||
|
||||
const PASSWORD_NAMES: [&str; 7] = [
|
||||
"password", "pass", "passwd", "pwd", "secret", "token", "secrete",
|
||||
];
|
||||
|
@ -20,3 +22,21 @@ pub fn matches_password_name(string: &str) -> bool {
|
|||
.iter()
|
||||
.any(|name| string.to_lowercase().contains(name))
|
||||
}
|
||||
|
||||
pub fn is_untyped_exception(type_: Option<&Expr>, checker: &Checker) -> bool {
|
||||
type_.map_or(true, |type_| {
|
||||
if let ExprKind::Tuple { elts, .. } = &type_.node {
|
||||
elts.iter().any(|type_| {
|
||||
checker.resolve_call_path(type_).map_or(false, |call_path| {
|
||||
call_path.as_slice() == ["", "Exception"]
|
||||
|| call_path.as_slice() == ["", "BaseException"]
|
||||
})
|
||||
})
|
||||
} else {
|
||||
checker.resolve_call_path(type_).map_or(false, |call_path| {
|
||||
call_path.as_slice() == ["", "Exception"]
|
||||
|| call_path.as_slice() == ["", "BaseException"]
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
use ruff_macros::{define_violation, derive_message_formats};
|
||||
use rustpython_parser::ast::{Expr, Stmt, StmtKind};
|
||||
|
||||
use ruff_macros::{define_violation, derive_message_formats};
|
||||
|
||||
use crate::ast::types::Range;
|
||||
use crate::checkers::ast::Checker;
|
||||
use crate::registry::Diagnostic;
|
||||
use crate::rules::flake8_bandit::helpers::is_untyped_exception;
|
||||
use crate::violation::Violation;
|
||||
|
||||
define_violation!(
|
||||
|
@ -26,13 +28,7 @@ pub fn try_except_continue(
|
|||
) {
|
||||
if body.len() == 1
|
||||
&& body[0].node == StmtKind::Continue
|
||||
&& (check_typed_exception
|
||||
|| type_.map_or(true, |type_| {
|
||||
checker.resolve_call_path(type_).map_or(true, |call_path| {
|
||||
call_path.as_slice() == ["", "Exception"]
|
||||
|| call_path.as_slice() == ["", "BaseException"]
|
||||
})
|
||||
}))
|
||||
&& (check_typed_exception || is_untyped_exception(type_, checker))
|
||||
{
|
||||
checker.diagnostics.push(Diagnostic::new(
|
||||
TryExceptContinue,
|
||||
|
|
|
@ -4,6 +4,7 @@ use rustpython_parser::ast::{Expr, Stmt, StmtKind};
|
|||
use crate::ast::types::Range;
|
||||
use crate::checkers::ast::Checker;
|
||||
use crate::registry::Diagnostic;
|
||||
use crate::rules::flake8_bandit::helpers::is_untyped_exception;
|
||||
use crate::violation::Violation;
|
||||
|
||||
define_violation!(
|
||||
|
@ -26,13 +27,7 @@ pub fn try_except_pass(
|
|||
) {
|
||||
if body.len() == 1
|
||||
&& body[0].node == StmtKind::Pass
|
||||
&& (check_typed_exception
|
||||
|| type_.map_or(true, |type_| {
|
||||
checker.resolve_call_path(type_).map_or(true, |call_path| {
|
||||
call_path.as_slice() == ["", "Exception"]
|
||||
|| call_path.as_slice() == ["", "BaseException"]
|
||||
})
|
||||
}))
|
||||
&& (check_typed_exception || is_untyped_exception(type_, checker))
|
||||
{
|
||||
checker.diagnostics.push(Diagnostic::new(
|
||||
TryExceptPass,
|
||||
|
|
|
@ -22,4 +22,24 @@ expression: diagnostics
|
|||
column: 12
|
||||
fix: ~
|
||||
parent: ~
|
||||
- kind:
|
||||
TryExceptContinue: ~
|
||||
location:
|
||||
row: 14
|
||||
column: 4
|
||||
end_location:
|
||||
row: 14
|
||||
column: 12
|
||||
fix: ~
|
||||
parent: ~
|
||||
- kind:
|
||||
TryExceptContinue: ~
|
||||
location:
|
||||
row: 19
|
||||
column: 4
|
||||
end_location:
|
||||
row: 19
|
||||
column: 12
|
||||
fix: ~
|
||||
parent: ~
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue