Skip to content

Commit ddc1985

Browse files
committed
[flake8-bandit] Added Rule S110 (try/except/pass)
ref: astral-sh#1646
1 parent adb5c5b commit ddc1985

File tree

9 files changed

+159
-0
lines changed

9 files changed

+159
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
try:
2+
pass
3+
except Exception:
4+
pass
5+
6+
try:
7+
pass
8+
except:
9+
pass
10+
11+
try:
12+
pass
13+
except ValueError:
14+
pass

src/checkers/ast.rs

+9
Original file line numberDiff line numberDiff line change
@@ -3420,6 +3420,15 @@ where
34203420
body,
34213421
);
34223422
}
3423+
if self.settings.rules.enabled(&Rule::TryExceptPass) {
3424+
flake8_bandit::rules::try_except_pass(
3425+
self,
3426+
type_.as_deref(),
3427+
name.as_deref(),
3428+
body,
3429+
self.settings.flake8_bandit.check_typed_exception,
3430+
);
3431+
}
34233432
if self.settings.rules.enabled(&Rule::ReraiseNoCause) {
34243433
tryceratops::rules::reraise_no_cause(self, body);
34253434
}

src/registry.rs

+1
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ ruff_macros::define_rule_mapping!(
331331
S106 => violations::HardcodedPasswordFuncArg,
332332
S107 => violations::HardcodedPasswordDefault,
333333
S108 => violations::HardcodedTempFile,
334+
S110 => rules::flake8_bandit::rules::TryExceptPass,
334335
S113 => violations::RequestWithoutTimeout,
335336
S324 => violations::HashlibInsecureHashFunction,
336337
S501 => violations::RequestWithNoCertValidation,

src/rules/flake8_bandit/mod.rs

+17
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ mod tests {
3131
#[test_case(Rule::SnmpWeakCryptography, Path::new("S509.py"); "S509")]
3232
#[test_case(Rule::LoggingConfigInsecureListen, Path::new("S612.py"); "S612")]
3333
#[test_case(Rule::Jinja2AutoescapeFalse, Path::new("S701.py"); "S701")]
34+
#[test_case(Rule::TryExceptPass, Path::new("S110.py"); "S110")]
3435
fn rules(rule_code: Rule, path: &Path) -> Result<()> {
3536
let snapshot = format!("{}_{}", rule_code.code(), path.to_string_lossy());
3637
let diagnostics = test_path(
@@ -55,11 +56,27 @@ mod tests {
5556
"/dev/shm".to_string(),
5657
"/foo".to_string(),
5758
],
59+
check_typed_exception: false,
5860
},
5961
..Settings::for_rule(Rule::HardcodedTempFile)
6062
},
6163
)?;
6264
assert_yaml_snapshot!("S108_extend", diagnostics);
6365
Ok(())
6466
}
67+
68+
#[test]
69+
fn check_typed_exception() -> Result<()> {
70+
let mut settings: super::settings::Settings = Default::default();
71+
settings.check_typed_exception = true;
72+
let diagnostics = test_path(
73+
Path::new("./resources/test/fixtures/flake8_bandit/S110.py"),
74+
&Settings {
75+
flake8_bandit: settings,
76+
..Settings::for_rule(Rule::TryExceptPass)
77+
},
78+
)?;
79+
assert_yaml_snapshot!("S110_typed", diagnostics);
80+
Ok(())
81+
}
6582
}

src/rules/flake8_bandit/rules/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub use request_with_no_cert_validation::request_with_no_cert_validation;
1717
pub use request_without_timeout::request_without_timeout;
1818
pub use snmp_insecure_version::snmp_insecure_version;
1919
pub use snmp_weak_cryptography::snmp_weak_cryptography;
20+
pub use try_except_pass::{try_except_pass, TryExceptPass};
2021
pub use unsafe_yaml_load::unsafe_yaml_load;
2122

2223
mod assert_used;
@@ -34,4 +35,5 @@ mod request_with_no_cert_validation;
3435
mod request_without_timeout;
3536
mod snmp_insecure_version;
3637
mod snmp_weak_cryptography;
38+
mod try_except_pass;
3739
mod unsafe_yaml_load;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use ruff_macros::derive_message_formats;
2+
use rustpython_ast::{Expr, ExprKind, Located, Stmt, StmtKind};
3+
4+
use crate::ast::types::Range;
5+
use crate::checkers::ast::Checker;
6+
use crate::define_violation;
7+
use crate::registry::Diagnostic;
8+
use crate::violation::Violation;
9+
10+
define_violation!(
11+
pub struct TryExceptPass;
12+
);
13+
impl Violation for TryExceptPass {
14+
#[derive_message_formats]
15+
fn message(&self) -> String {
16+
format!("Try, Except, Pass detected.")
17+
}
18+
}
19+
20+
/// S110
21+
pub fn try_except_pass(
22+
checker: &mut Checker,
23+
type_: Option<&Expr>,
24+
_name: Option<&str>,
25+
body: &[Stmt],
26+
check_typed_exception: bool,
27+
) {
28+
if body.len() == 1
29+
&& body[0].node == StmtKind::Pass
30+
&& (check_typed_exception
31+
|| match &type_ {
32+
Some(Located {
33+
node: ExprKind::Name { id, .. },
34+
..
35+
}) => id == "Exception",
36+
None => true,
37+
_ => false,
38+
})
39+
{
40+
checker.diagnostics.push(Diagnostic::new(
41+
TryExceptPass,
42+
Range::from_located(&body[0]),
43+
));
44+
}
45+
}

src/rules/flake8_bandit/settings.rs

+11
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,19 @@ pub struct Options {
3434
/// A list of directories to consider temporary, in addition to those
3535
/// specified by `hardcoded-tmp-directory`.
3636
pub hardcoded_tmp_directory_extend: Option<Vec<String>>,
37+
#[option(
38+
default = "false",
39+
value_type = "bool",
40+
example = "check-typed-exception = true"
41+
)]
42+
/// A list of directories to consider temporary.
43+
pub check_typed_exception: Option<bool>,
3744
}
3845

3946
#[derive(Debug, Hash)]
4047
pub struct Settings {
4148
pub hardcoded_tmp_directory: Vec<String>,
49+
pub check_typed_exception: bool,
4250
}
4351

4452
impl From<Options> for Settings {
@@ -55,6 +63,7 @@ impl From<Options> for Settings {
5563
.into_iter(),
5664
)
5765
.collect(),
66+
check_typed_exception: options.check_typed_exception.unwrap_or(false),
5867
}
5968
}
6069
}
@@ -64,6 +73,7 @@ impl From<Settings> for Options {
6473
Self {
6574
hardcoded_tmp_directory: Some(settings.hardcoded_tmp_directory),
6675
hardcoded_tmp_directory_extend: None,
76+
check_typed_exception: Some(settings.check_typed_exception),
6777
}
6878
}
6979
}
@@ -72,6 +82,7 @@ impl Default for Settings {
7282
fn default() -> Self {
7383
Self {
7484
hardcoded_tmp_directory: default_tmp_dirs(),
85+
check_typed_exception: false,
7586
}
7687
}
7788
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
source: src/rules/flake8_bandit/mod.rs
3+
expression: diagnostics
4+
---
5+
- kind:
6+
TryExceptPass: ~
7+
location:
8+
row: 4
9+
column: 4
10+
end_location:
11+
row: 4
12+
column: 8
13+
fix: ~
14+
parent: ~
15+
- kind:
16+
TryExceptPass: ~
17+
location:
18+
row: 9
19+
column: 4
20+
end_location:
21+
row: 9
22+
column: 8
23+
fix: ~
24+
parent: ~
25+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
source: src/rules/flake8_bandit/mod.rs
3+
expression: diagnostics
4+
---
5+
- kind:
6+
TryExceptPass: ~
7+
location:
8+
row: 4
9+
column: 4
10+
end_location:
11+
row: 4
12+
column: 8
13+
fix: ~
14+
parent: ~
15+
- kind:
16+
TryExceptPass: ~
17+
location:
18+
row: 9
19+
column: 4
20+
end_location:
21+
row: 9
22+
column: 8
23+
fix: ~
24+
parent: ~
25+
- kind:
26+
TryExceptPass: ~
27+
location:
28+
row: 14
29+
column: 4
30+
end_location:
31+
row: 14
32+
column: 8
33+
fix: ~
34+
parent: ~
35+

0 commit comments

Comments
 (0)