Skip to content

Commit

Permalink
Include with statements in complexity calculation (#3771)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh authored Mar 28, 2023
1 parent bfecf68 commit 81de3a1
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions crates/ruff/src/rules/mccabe/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ fn get_complexity_number(stmts: &[Stmt]) -> usize {
complexity += get_complexity_number(body);
complexity += get_complexity_number(orelse);
}
StmtKind::With { body, .. } | StmtKind::AsyncWith { body, .. } => {
complexity += get_complexity_number(body);
}
StmtKind::While { body, orelse, .. } => {
complexity += 1;
complexity += get_complexity_number(body);
Expand Down Expand Up @@ -403,6 +406,19 @@ def process_detect_lines():
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(())
}

#[test]
fn with() -> Result<()> {
let source = r#"
def with_lock():
with lock:
if foo:
print('bar')
"#;
let stmts = parser::parse_program(source, "<filename>")?;
assert_eq!(get_complexity_number(&stmts), 2);
Expand Down

0 comments on commit 81de3a1

Please sign in to comment.