From 15d4774b6b3807a8cd2bffecfbc12d09e79bafb1 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Tue, 31 Jan 2023 12:45:51 -0500 Subject: [PATCH] Avoid flagging same-condition cases in SIM103 (#2404) --- .../test/fixtures/flake8_simplify/SIM103.py | 16 ++++++++++++++++ src/rules/flake8_simplify/rules/ast_if.rs | 7 +++++++ 2 files changed, 23 insertions(+) diff --git a/resources/test/fixtures/flake8_simplify/SIM103.py b/resources/test/fixtures/flake8_simplify/SIM103.py index 40128b5c66..c3101611d9 100644 --- a/resources/test/fixtures/flake8_simplify/SIM103.py +++ b/resources/test/fixtures/flake8_simplify/SIM103.py @@ -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 diff --git a/src/rules/flake8_simplify/rules/ast_if.rs b/src/rules/flake8_simplify/rules/ast_if.rs index 9af92a9ecc..1c9c7a5403 100644 --- a/src/rules/flake8_simplify/rules/ast_if.rs +++ b/src/rules/flake8_simplify/rules/ast_if.rs @@ -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 },