-
Notifications
You must be signed in to change notification settings - Fork 883
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
crates/ruff/resources/test/fixtures/pylint/binary_op_exception.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 @@ | ||
try: | ||
1 / 0 | ||
except ZeroDivisionError or ValueError as e: # [binary-op-exception] | ||
pass | ||
|
||
try: | ||
raise ValueError | ||
except ZeroDivisionError and ValueError as e: # [binary-op-exception] | ||
print(e) | ||
|
||
try: | ||
pass | ||
except (ValueError, Exception, IOError): | ||
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,81 @@ | ||
use rustpython_parser::ast::{Excepthandler, ExcepthandlerKind, ExprKind}; | ||
|
||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::types::Range; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
#[derive(Debug, PartialEq, Eq, Copy, Clone)] | ||
enum Boolop { | ||
And, | ||
Or, | ||
} | ||
|
||
impl From<&rustpython_parser::ast::Boolop> for Boolop { | ||
fn from(op: &rustpython_parser::ast::Boolop) -> Self { | ||
match op { | ||
rustpython_parser::ast::Boolop::And => Boolop::And, | ||
rustpython_parser::ast::Boolop::Or => Boolop::Or, | ||
} | ||
} | ||
} | ||
|
||
/// ## What it does | ||
/// Checks for `except` clauses that attempt to catch multiple | ||
/// exceptions with a binary operation (`and` or `or`). | ||
/// | ||
/// ## Why is this bad? | ||
/// A binary operation will not catch multiple exceptions. Instead, the binary | ||
/// operation will be evaluated first, and the result of _that_ operation will | ||
/// be caught (for an `or` operation, this is typically the first exception in | ||
/// the list). This is almost never the desired behavior. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// try: | ||
/// pass | ||
/// except A or B: | ||
/// pass | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// try: | ||
/// pass | ||
/// except (A ,B): | ||
/// pass | ||
/// ``` | ||
#[violation] | ||
pub struct BinaryOpException { | ||
op: Boolop, | ||
} | ||
|
||
impl Violation for BinaryOpException { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
let BinaryOpException { op } = self; | ||
match op { | ||
Boolop::And => format!("Exception to catch is the result of a binary `and` operation"), | ||
Boolop::Or => format!("Exception to catch is the result of a binary `or` operation"), | ||
} | ||
} | ||
} | ||
|
||
/// PLW0711 | ||
pub fn binary_op_exception(checker: &mut Checker, excepthandler: &Excepthandler) { | ||
let ExcepthandlerKind::ExceptHandler { type_, .. } = &excepthandler.node; | ||
|
||
let Some(type_) = type_ else { | ||
return; | ||
}; | ||
|
||
let ExprKind::BoolOp { op, .. } = &type_.node else { | ||
return; | ||
}; | ||
|
||
checker.diagnostics.push(Diagnostic::new( | ||
BinaryOpException { op: op.into() }, | ||
Range::from(type_), | ||
)); | ||
} |
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
31 changes: 31 additions & 0 deletions
31
...rc/rules/pylint/snapshots/ruff__rules__pylint__tests__PLW0711_binary_op_exception.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,31 @@ | ||
--- | ||
source: crates/ruff/src/rules/pylint/mod.rs | ||
expression: diagnostics | ||
--- | ||
- kind: | ||
name: BinaryOpException | ||
body: "Exception to catch is the result of a binary `or` operation" | ||
suggestion: ~ | ||
fixable: false | ||
location: | ||
row: 3 | ||
column: 7 | ||
end_location: | ||
row: 3 | ||
column: 38 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
name: BinaryOpException | ||
body: "Exception to catch is the result of a binary `and` operation" | ||
suggestion: ~ | ||
fixable: false | ||
location: | ||
row: 8 | ||
column: 7 | ||
end_location: | ||
row: 8 | ||
column: 39 | ||
fix: ~ | ||
parent: ~ | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.