Skip to content

Commit

Permalink
add PLR0916
Browse files Browse the repository at this point in the history
  • Loading branch information
diceroll123 committed Oct 16, 2023
1 parent aa6846c commit 69820d2
Show file tree
Hide file tree
Showing 11 changed files with 383 additions and 0 deletions.
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:
...
3 changes: 3 additions & 0 deletions crates/ruff_linter/src/checkers/ast/analyze/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1067,6 +1067,9 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
if checker.enabled(Rule::CheckAndRemoveFromSet) {
refurb::rules::check_and_remove_from_set(checker, if_);
}
if checker.enabled(Rule::TooManyBooleanExpressions) {
pylint::rules::too_many_boolean_expressions(checker, if_);
}
if checker.source_type.is_stub() {
if checker.any_enabled(&[
Rule::UnrecognizedVersionInfoCheck,
Expand Down
1 change: 1 addition & 0 deletions crates/ruff_linter/src/codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<(RuleGroup, Rule)> {
(Pylint, "R0912") => (RuleGroup::Unspecified, rules::pylint::rules::TooManyBranches),
(Pylint, "R0913") => (RuleGroup::Unspecified, rules::pylint::rules::TooManyArguments),
(Pylint, "R0915") => (RuleGroup::Unspecified, rules::pylint::rules::TooManyStatements),
(Pylint, "R0916") => (RuleGroup::Unspecified, rules::pylint::rules::TooManyBooleanExpressions),
(Pylint, "R1701") => (RuleGroup::Unspecified, rules::pylint::rules::RepeatedIsinstanceCalls),
(Pylint, "R1711") => (RuleGroup::Unspecified, rules::pylint::rules::UselessReturn),
(Pylint, "R1714") => (RuleGroup::Unspecified, rules::pylint::rules::RepeatedEqualityComparison),
Expand Down
16 changes: 16 additions & 0 deletions crates/ruff_linter/src/rules/pylint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,22 @@ mod tests {
Ok(())
}

#[test]
fn max_boolean_expressions() -> Result<()> {
let diagnostics = test_path(
Path::new("pylint/too_many_boolean_expressions.py"),
&LinterSettings {
pylint: pylint::settings::Settings {
max_bools: 5,
..pylint::settings::Settings::default()
},
..LinterSettings::for_rule(Rule::TooManyBooleanExpressions)
},
)?;
assert_messages!(diagnostics);
Ok(())
}

#[test]
fn max_statements() -> Result<()> {
let diagnostics = test_path(
Expand Down
2 changes: 2 additions & 0 deletions crates/ruff_linter/src/rules/pylint/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub(crate) use subprocess_popen_preexec_fn::*;
pub(crate) use subprocess_run_without_check::*;
pub(crate) use sys_exit_alias::*;
pub(crate) use too_many_arguments::*;
pub(crate) use too_many_boolean_expressions::*;
pub(crate) use too_many_branches::*;
pub(crate) use too_many_public_methods::*;
pub(crate) use too_many_return_statements::*;
Expand Down Expand Up @@ -103,6 +104,7 @@ mod subprocess_popen_preexec_fn;
mod subprocess_run_without_check;
mod sys_exit_alias;
mod too_many_arguments;
mod too_many_boolean_expressions;
mod too_many_branches;
mod too_many_public_methods;
mod too_many_return_statements;
Expand Down
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(),
));
}
}
}
}
2 changes: 2 additions & 0 deletions crates/ruff_linter/src/rules/pylint/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub struct Settings {
pub allow_magic_value_types: Vec<ConstantType>,
pub max_args: usize,
pub max_returns: usize,
pub max_bools: usize,
pub max_branches: usize,
pub max_statements: usize,
pub max_public_methods: usize,
Expand All @@ -51,6 +52,7 @@ impl Default for Settings {
allow_magic_value_types: vec![ConstantType::Str, ConstantType::Bytes],
max_args: 5,
max_returns: 6,
max_bools: 5,
max_branches: 12,
max_statements: 50,
max_public_methods: 20,
Expand Down
Loading

0 comments on commit 69820d2

Please sign in to comment.