-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pylint] Implement PLR0916 (
too-many-boolean-expressions
) (#7975)
## Summary Add [R0916](https://pylint.readthedocs.io/en/latest/user_guide/messages/refactor/too-many-boolean-expressions.html), no autofix available. See: #970 ## Test Plan `cargo test` and manually.
- Loading branch information
1 parent
5da0f91
commit 21ea290
Showing
11 changed files
with
398 additions
and
0 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
crates/ruff_linter/resources/test/fixtures/pylint/too_many_boolean_expressions.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,54 @@ | ||
if a: | ||
... | ||
elif (a and b): | ||
... | ||
elif (a and b) and c: | ||
... | ||
elif (a and b) and c and d: | ||
... | ||
elif (a and b) and c and d and e: | ||
... | ||
elif (a and b) and c and d and e and f: | ||
... | ||
elif (a and b) and c and d and e and f and g: | ||
... | ||
elif (a and b) and c and d and e and f and g and h: | ||
... | ||
elif (a and b) and c and d and e and f and g and h and i: | ||
... | ||
elif (a and b) and c and d and e and f and g and h and i and j: | ||
... | ||
elif (a and b) and c and d and e and f and g and h and i and j and k: | ||
... | ||
elif (a and b) and c and d and e and f and g and h and i and j and k and l: | ||
... | ||
elif (a and b) and c and d and e and f and g and h and i and j and k and l and m: | ||
... | ||
elif (a and b) and c and d and e and f and g and h and i and j and k and l and m and n: | ||
... | ||
elif (a and b) and c and d and e and f and g and h and i and j and k and l and m and n and o: | ||
... | ||
elif (a and b) and c and d and e and f and g and h and i and j and k and l and m and n and o and p: | ||
... | ||
elif (a and b) and c and d and e and f and g and h and i and j and k and l and m and n and o and p and q: | ||
... | ||
elif (a and b) and c and d and e and f and g and h and i and j and k and l and m and n and o and p and q and r: | ||
... | ||
elif (a and b) and c and d and e and f and g and h and i and j and k and l and m and n and o and p and q and r and s: | ||
... | ||
elif (a and b) and c and d and e and f and g and h and i and j and k and l and m and n and o and p and q and r and s and t: | ||
... | ||
elif (a and b) and c and d and e and f and g and h and i and j and k and l and m and n and o and p and q and r and s and t and u: | ||
... | ||
elif (a and b) and c and d and e and f and g and h and i and j and k and l and m and n and o and p and q and r and s and t and u and v: | ||
... | ||
elif (a and b) and c and d and e and f and g and h and i and j and k and l and m and n and o and p and q and r and s and t and u and v and w: | ||
... | ||
elif (a and b) and c and d and e and f and g and h and i and j and k and l and m and n and o and p and q and r and s and t and u and v and w and x: | ||
... | ||
elif (a and b) and c and d and e and f and g and h and i and j and k and l and m and n and o and p and q and r and s and t and u and v and w and x and y: | ||
... | ||
elif (a and b) and c and d and e and f and g and h and i and j and k and l and m and n and o and p and q and r and s and t and u and v and w and x and y and z: | ||
... | ||
else: | ||
... |
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
89 changes: 89 additions & 0 deletions
89
crates/ruff_linter/src/rules/pylint/rules/too_many_boolean_expressions.rs
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,89 @@ | ||
use ast::{Expr, StmtIf}; | ||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast as ast; | ||
use ruff_text_size::Ranged; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for too many Boolean expressions in an `if` statement. | ||
/// | ||
/// By default, this rule allows up to 5 expressions. This can be configured | ||
/// using the [`pylint.max-bool-expr`] option. | ||
/// | ||
/// ## Why is this bad? | ||
/// `if` statements with many Boolean expressions are harder to understand | ||
/// and maintain. Consider assigning the result of the Boolean expression, | ||
/// or any of its sub-expressions, to a variable. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// if a and b and c and d and e and f and g and h: | ||
/// ... | ||
/// ``` | ||
/// | ||
/// ## Options | ||
/// - `pylint.max-bool-expr` | ||
#[violation] | ||
pub struct TooManyBooleanExpressions { | ||
expressions: usize, | ||
max_expressions: usize, | ||
} | ||
|
||
impl Violation for TooManyBooleanExpressions { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
let TooManyBooleanExpressions { | ||
expressions, | ||
max_expressions, | ||
} = self; | ||
format!("Too many Boolean expressions ({expressions} > {max_expressions})") | ||
} | ||
} | ||
|
||
/// PLR0916 | ||
pub(crate) fn too_many_boolean_expressions(checker: &mut Checker, stmt: &StmtIf) { | ||
if let Some(bool_op) = stmt.test.as_bool_op_expr() { | ||
let expressions = count_bools(bool_op); | ||
if expressions > checker.settings.pylint.max_bool_expr { | ||
checker.diagnostics.push(Diagnostic::new( | ||
TooManyBooleanExpressions { | ||
expressions, | ||
max_expressions: checker.settings.pylint.max_bool_expr, | ||
}, | ||
bool_op.range(), | ||
)); | ||
} | ||
} | ||
|
||
for elif in &stmt.elif_else_clauses { | ||
if let Some(bool_op) = elif.test.as_ref().and_then(Expr::as_bool_op_expr) { | ||
let expressions = count_bools(bool_op); | ||
if expressions > checker.settings.pylint.max_bool_expr { | ||
checker.diagnostics.push(Diagnostic::new( | ||
TooManyBooleanExpressions { | ||
expressions, | ||
max_expressions: checker.settings.pylint.max_bool_expr, | ||
}, | ||
bool_op.range(), | ||
)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// Count the number of Boolean expressions in a `bool_op` expression. | ||
fn count_bools(bool_op: &ast::ExprBoolOp) -> usize { | ||
bool_op | ||
.values | ||
.iter() | ||
.map(|expr| { | ||
if let Expr::BoolOp(bool_op) = expr { | ||
count_bools(bool_op) | ||
} else { | ||
1 | ||
} | ||
}) | ||
.sum::<usize>() | ||
} |
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
Oops, something went wrong.