From 64c4e4c6c784c8a1509bd52ee4a03e2b5a8aeaf0 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Fri, 27 Jan 2023 11:25:57 -0500 Subject: [PATCH] Treat constant tuples as constants for yoda-conditions (#2265) --- .../test/fixtures/flake8_simplify/SIM300.py | 6 +- .../flake8_simplify/rules/yoda_conditions.rs | 5 +- ...ke8_simplify__tests__SIM300_SIM300.py.snap | 73 ++++++++++++------- 3 files changed, 53 insertions(+), 31 deletions(-) diff --git a/resources/test/fixtures/flake8_simplify/SIM300.py b/resources/test/fixtures/flake8_simplify/SIM300.py index a573cc5fc2..b2d55a9a1b 100644 --- a/resources/test/fixtures/flake8_simplify/SIM300.py +++ b/resources/test/fixtures/flake8_simplify/SIM300.py @@ -1,9 +1,10 @@ # Errors "yoda" == compare # SIM300 -'yoda' == compare # SIM300 +"yoda" == compare # SIM300 42 == age # SIM300 +("a", "b") == compare # SIM300 "yoda" <= compare # SIM300 -'yoda' < compare # SIM300 +"yoda" < compare # SIM300 42 > age # SIM300 YODA == age # SIM300 YODA > age # SIM300 @@ -12,6 +13,7 @@ YODA >= age # SIM300 # OK compare == "yoda" age == 42 +compare == ("a", "b") x == y "yoda" == compare == 1 "yoda" == compare == someothervar diff --git a/src/rules/flake8_simplify/rules/yoda_conditions.rs b/src/rules/flake8_simplify/rules/yoda_conditions.rs index 3c5f7871a3..b4f5ccc485 100644 --- a/src/rules/flake8_simplify/rules/yoda_conditions.rs +++ b/src/rules/flake8_simplify/rules/yoda_conditions.rs @@ -8,9 +8,10 @@ use crate::registry::Diagnostic; use crate::violations; /// Return `true` if an [`Expr`] is a constant or a constant-like name. -fn is_constant(expr: &Expr) -> bool { +fn is_constant_like(expr: &Expr) -> bool { match &expr.node { ExprKind::Constant { .. } => true, + ExprKind::Tuple { elts, .. } => elts.iter().all(is_constant_like), ExprKind::Name { id, .. } => string::is_upper(id), _ => false, } @@ -35,7 +36,7 @@ pub fn yoda_conditions( return; } - if !is_constant(left) || is_constant(right) { + if !is_constant_like(left) || is_constant_like(right) { return; } diff --git a/src/rules/flake8_simplify/snapshots/ruff__rules__flake8_simplify__tests__SIM300_SIM300.py.snap b/src/rules/flake8_simplify/snapshots/ruff__rules__flake8_simplify__tests__SIM300_SIM300.py.snap index 3fc55c11c3..d7ad75210f 100644 --- a/src/rules/flake8_simplify/snapshots/ruff__rules__flake8_simplify__tests__SIM300_SIM300.py.snap +++ b/src/rules/flake8_simplify/snapshots/ruff__rules__flake8_simplify__tests__SIM300_SIM300.py.snap @@ -23,7 +23,7 @@ expression: diagnostics parent: ~ - kind: YodaConditions: - suggestion: "compare == 'yoda'" + suggestion: "compare == \"yoda\"" location: row: 3 column: 0 @@ -32,7 +32,7 @@ expression: diagnostics column: 17 fix: content: - - "compare == 'yoda'" + - "compare == \"yoda\"" location: row: 3 column: 0 @@ -61,116 +61,135 @@ expression: diagnostics parent: ~ - kind: YodaConditions: - suggestion: "compare >= \"yoda\"" + suggestion: "compare == (\"a\", \"b\")" location: row: 5 column: 0 end_location: row: 5 + column: 21 + fix: + content: + - "compare == (\"a\", \"b\")" + location: + row: 5 + column: 0 + end_location: + row: 5 + column: 21 + parent: ~ +- kind: + YodaConditions: + suggestion: "compare >= \"yoda\"" + location: + row: 6 + column: 0 + end_location: + row: 6 column: 17 fix: content: - "compare >= \"yoda\"" location: - row: 5 + row: 6 column: 0 end_location: - row: 5 + row: 6 column: 17 parent: ~ - kind: YodaConditions: - suggestion: "compare > 'yoda'" + suggestion: "compare > \"yoda\"" location: - row: 6 + row: 7 column: 0 end_location: - row: 6 + row: 7 column: 16 fix: content: - - "compare > 'yoda'" + - "compare > \"yoda\"" location: - row: 6 + row: 7 column: 0 end_location: - row: 6 + row: 7 column: 16 parent: ~ - kind: YodaConditions: suggestion: age < 42 location: - row: 7 + row: 8 column: 0 end_location: - row: 7 + row: 8 column: 8 fix: content: - age < 42 location: - row: 7 + row: 8 column: 0 end_location: - row: 7 + row: 8 column: 8 parent: ~ - kind: YodaConditions: suggestion: age == YODA location: - row: 8 + row: 9 column: 0 end_location: - row: 8 + row: 9 column: 11 fix: content: - age == YODA location: - row: 8 + row: 9 column: 0 end_location: - row: 8 + row: 9 column: 11 parent: ~ - kind: YodaConditions: suggestion: age < YODA location: - row: 9 + row: 10 column: 0 end_location: - row: 9 + row: 10 column: 10 fix: content: - age < YODA location: - row: 9 + row: 10 column: 0 end_location: - row: 9 + row: 10 column: 10 parent: ~ - kind: YodaConditions: suggestion: age <= YODA location: - row: 10 + row: 11 column: 0 end_location: - row: 10 + row: 11 column: 11 fix: content: - age <= YODA location: - row: 10 + row: 11 column: 0 end_location: - row: 10 + row: 11 column: 11 parent: ~