forked from astral-sh/ruff
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[
flake8-bandit
] Added Rule S110 (try/except/pass)
ref: astral-sh#1646
- Loading branch information
Showing
9 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
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 @@ | ||
try: | ||
pass | ||
except Exception: | ||
pass | ||
|
||
try: | ||
pass | ||
except: | ||
pass | ||
|
||
try: | ||
pass | ||
except ValueError: | ||
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
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,45 @@ | ||
use ruff_macros::derive_message_formats; | ||
use rustpython_ast::{Expr, ExprKind, Located, Stmt, StmtKind}; | ||
|
||
use crate::ast::types::Range; | ||
use crate::checkers::ast::Checker; | ||
use crate::define_violation; | ||
use crate::registry::Diagnostic; | ||
use crate::violation::Violation; | ||
|
||
define_violation!( | ||
pub struct TryExceptPass; | ||
); | ||
impl Violation for TryExceptPass { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Try, Except, Pass detected.") | ||
} | ||
} | ||
|
||
/// S110 | ||
pub fn try_except_pass( | ||
checker: &mut Checker, | ||
type_: Option<&Expr>, | ||
_name: Option<&str>, | ||
body: &[Stmt], | ||
check_typed_exception: bool, | ||
) { | ||
if body.len() == 1 | ||
&& body[0].node == StmtKind::Pass | ||
&& (check_typed_exception | ||
|| match &type_ { | ||
Some(Located { | ||
node: ExprKind::Name { id, .. }, | ||
.. | ||
}) => id == "Exception", | ||
None => true, | ||
_ => false, | ||
}) | ||
{ | ||
checker.diagnostics.push(Diagnostic::new( | ||
TryExceptPass, | ||
Range::from_located(&body[0]), | ||
)); | ||
} | ||
} |
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_bandit/snapshots/ruff__rules__flake8_bandit__tests__S110_S110.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: src/rules/flake8_bandit/mod.rs | ||
expression: diagnostics | ||
--- | ||
- kind: | ||
TryExceptPass: ~ | ||
location: | ||
row: 4 | ||
column: 4 | ||
end_location: | ||
row: 4 | ||
column: 8 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
TryExceptPass: ~ | ||
location: | ||
row: 9 | ||
column: 4 | ||
end_location: | ||
row: 9 | ||
column: 8 | ||
fix: ~ | ||
parent: ~ | ||
|
35 changes: 35 additions & 0 deletions
35
src/rules/flake8_bandit/snapshots/ruff__rules__flake8_bandit__tests__S110_typed.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,35 @@ | ||
--- | ||
source: src/rules/flake8_bandit/mod.rs | ||
expression: diagnostics | ||
--- | ||
- kind: | ||
TryExceptPass: ~ | ||
location: | ||
row: 4 | ||
column: 4 | ||
end_location: | ||
row: 4 | ||
column: 8 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
TryExceptPass: ~ | ||
location: | ||
row: 9 | ||
column: 4 | ||
end_location: | ||
row: 9 | ||
column: 8 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
TryExceptPass: ~ | ||
location: | ||
row: 14 | ||
column: 4 | ||
end_location: | ||
row: 14 | ||
column: 8 | ||
fix: ~ | ||
parent: ~ | ||
|