Avoid flagging same-condition cases in SIM103 (#2404)

This commit is contained in:
Charlie Marsh 2023-01-31 12:45:51 -05:00 committed by GitHub
parent 293c7e00d5
commit 15d4774b6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 0 deletions

View File

@ -50,3 +50,19 @@ def f():
return False
else:
return True
def f():
# OK
if a:
return False
else:
return False
def f():
# OK
if a:
return True
else:
return True

View File

@ -128,6 +128,7 @@ pub fn nested_if_statements(
checker.diagnostics.push(diagnostic);
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Bool {
True,
False,
@ -167,6 +168,12 @@ pub fn return_bool_condition_directly(checker: &mut Checker, stmt: &Stmt) {
let (Some(if_return), Some(else_return)) = (is_one_line_return_bool(body), is_one_line_return_bool(orelse)) else {
return;
};
// If the branches have the same condition, abort (although the code could be simplified).
if if_return == else_return {
return;
}
let condition = unparse_expr(test, checker.stylist);
let mut diagnostic = Diagnostic::new(
violations::ReturnBoolConditionDirectly { cond: condition },