Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refine complexity rules for try-except-else-finally #3519

Merged
merged 1 commit into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 35 additions & 7 deletions crates/ruff/src/rules/mccabe/rules.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rustpython_parser::ast::{ExcepthandlerKind, ExprKind, Stmt, StmtKind};
use rustpython_parser::ast::{ExcepthandlerKind, Stmt, StmtKind};

use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
Expand Down Expand Up @@ -74,13 +74,10 @@ fn get_complexity_number(stmts: &[Stmt]) -> usize {
complexity += get_complexity_number(body);
complexity += get_complexity_number(orelse);
}
StmtKind::While { test, body, orelse } => {
StmtKind::While { body, orelse, .. } => {
complexity += 1;
complexity += get_complexity_number(body);
complexity += get_complexity_number(orelse);
if let ExprKind::BoolOp { .. } = &test.node {
complexity += 1;
}
}
StmtKind::Match { cases, .. } => {
complexity += 1;
Expand All @@ -100,8 +97,10 @@ fn get_complexity_number(stmts: &[Stmt]) -> usize {
orelse,
finalbody,
} => {
complexity += 1;
complexity += get_complexity_number(body);
if !orelse.is_empty() {
complexity += 1;
}
complexity += get_complexity_number(orelse);
complexity += get_complexity_number(finalbody);
for handler in handlers {
Expand Down Expand Up @@ -307,7 +306,7 @@ def nested_try_finally():
print(3)
"#;
let stmts = parser::parse_program(source, "<filename>")?;
assert_eq!(get_complexity_number(&stmts), 3);
assert_eq!(get_complexity_number(&stmts), 1);
Ok(())
}

Expand Down Expand Up @@ -374,4 +373,33 @@ class Class:
assert_eq!(get_complexity_number(&stmts), 9);
Ok(())
}

#[test]
fn finally() -> Result<()> {
let source = r#"
def process_detect_lines():
try:
pass
finally:
pass
"#;
let stmts = parser::parse_program(source, "<filename>")?;
assert_eq!(get_complexity_number(&stmts), 1);
Ok(())
}

#[test]
fn if_in_finally() -> Result<()> {
let source = r#"
def process_detect_lines():
try:
pass
finally:
if res:
errors.append(f"Non-zero exit code {res}")
"#;
let stmts = parser::parse_program(source, "<filename>")?;
assert_eq!(get_complexity_number(&stmts), 2);
Ok(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ expression: diagnostics
parent: ~
- kind:
name: ComplexStructure
body: "`nested_try_finally` is too complex (3)"
body: "`nested_try_finally` is too complex (1)"
suggestion: ~
fixable: false
location:
Expand Down