Include with statements in complexity calculation (#3771)

This commit is contained in:
Charlie Marsh 2023-03-28 11:20:22 -04:00 committed by GitHub
parent bfecf684ce
commit 81de3a16bc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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);
@ -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);