-
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.
- Loading branch information
1 parent
aa6846c
commit 69820d2
Showing
11 changed files
with
383 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
84 changes: 84 additions & 0 deletions
84
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,84 @@ | ||
use ast::{Expr, StmtIf}; | ||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::{self 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. | ||
/// | ||
/// ## Why is this bad? | ||
/// Too many boolean expressions in an `if` statement can make the code | ||
/// harder to understand. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// if a and b and c and d and e and f and g and h: | ||
/// ... | ||
/// ``` | ||
/// | ||
/// ## Options | ||
/// - `pylint.max-bools` | ||
#[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})") | ||
} | ||
} | ||
|
||
fn count_bools(expr: &Expr) -> usize { | ||
match expr { | ||
Expr::BoolOp(ast::ExprBoolOp { op, values, .. }) => match op { | ||
ast::BoolOp::And | ast::BoolOp::Or => { | ||
(values.len() - 1) + values.iter().map(count_bools).sum::<usize>() | ||
} | ||
}, | ||
Expr::Compare(ast::ExprCompare { | ||
left, comparators, .. | ||
}) => count_bools(left) + comparators.iter().map(count_bools).sum::<usize>(), | ||
_ => 0, | ||
} | ||
} | ||
|
||
/// PLR0916 | ||
pub(crate) fn too_many_boolean_expressions(checker: &mut Checker, stmt: &StmtIf) { | ||
let test_bool_count = count_bools(stmt.test.as_ref()); | ||
|
||
if test_bool_count > checker.settings.pylint.max_bools { | ||
checker.diagnostics.push(Diagnostic::new( | ||
TooManyBooleanExpressions { | ||
expressions: test_bool_count, | ||
max_expressions: checker.settings.pylint.max_bools, | ||
}, | ||
stmt.test.as_ref().range(), | ||
)); | ||
} | ||
|
||
for elif in &stmt.elif_else_clauses { | ||
if let Some(test) = elif.test.as_ref() { | ||
let elif_bool_count = count_bools(test); | ||
|
||
if elif_bool_count > checker.settings.pylint.max_bools { | ||
checker.diagnostics.push(Diagnostic::new( | ||
TooManyBooleanExpressions { | ||
expressions: elif_bool_count, | ||
max_expressions: checker.settings.pylint.max_bools, | ||
}, | ||
test.range(), | ||
)); | ||
} | ||
} | ||
} | ||
} |
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.