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

Avoid flagging same-condition cases in SIM103 #2404

Merged
merged 1 commit into from
Jan 31, 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
16 changes: 16 additions & 0 deletions resources/test/fixtures/flake8_simplify/SIM103.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions src/rules/flake8_simplify/rules/ast_if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ pub fn nested_if_statements(
checker.diagnostics.push(diagnostic);
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Bool {
True,
False,
Expand Down Expand Up @@ -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 },
Expand Down