From 8fba97f72fc64fa0fbf818db8a571dc20f394676 Mon Sep 17 00:00:00 2001 From: Auguste Lalande Date: Mon, 12 Feb 2024 20:21:06 -0500 Subject: [PATCH] `PLR2004`: Accept 0.0 and 1.0 as common magic values (#9964) ## Summary Accept 0.0 and 1.0 as common magic values. This is in line with the pylint behaviour, and I think makes sense conceptually. ## Test Plan Test cases were added to `crates/ruff_linter/resources/test/fixtures/pylint/magic_value_comparison.py` --- .../fixtures/pylint/magic_value_comparison.py | 15 ++++ .../pylint/rules/magic_value_comparison.rs | 4 +- ...ts__PLR2004_magic_value_comparison.py.snap | 68 ++++++++++++------- ...ylint__tests__allow_magic_value_types.snap | 44 ++++++++---- 4 files changed, 92 insertions(+), 39 deletions(-) diff --git a/crates/ruff_linter/resources/test/fixtures/pylint/magic_value_comparison.py b/crates/ruff_linter/resources/test/fixtures/pylint/magic_value_comparison.py index fd5e550a54dff..76139346b435d 100644 --- a/crates/ruff_linter/resources/test/fixtures/pylint/magic_value_comparison.py +++ b/crates/ruff_linter/resources/test/fixtures/pylint/magic_value_comparison.py @@ -35,6 +35,15 @@ if argc != 1: # correct pass +if argc != -1.0: # correct + pass + +if argc != 0.0: # correct + pass + +if argc != 1.0: # correct + pass + if argc != 2: # [magic-value-comparison] pass @@ -44,6 +53,12 @@ if argc != +2: # [magic-value-comparison] pass +if argc != -2.0: # [magic-value-comparison] + pass + +if argc != +2.0: # [magic-value-comparison] + pass + if __name__ == "__main__": # correct pass diff --git a/crates/ruff_linter/src/rules/pylint/rules/magic_value_comparison.rs b/crates/ruff_linter/src/rules/pylint/rules/magic_value_comparison.rs index e37147ac3d9ef..5e2463df4def1 100644 --- a/crates/ruff_linter/src/rules/pylint/rules/magic_value_comparison.rs +++ b/crates/ruff_linter/src/rules/pylint/rules/magic_value_comparison.rs @@ -86,8 +86,10 @@ fn is_magic_value(literal_expr: LiteralExpressionRef, allowed_types: &[ConstantT !matches!(value.to_str(), "" | "__main__") } LiteralExpressionRef::NumberLiteral(ast::ExprNumberLiteral { value, .. }) => match value { + #[allow(clippy::float_cmp)] + ast::Number::Float(value) => !(*value == 0.0 || *value == 1.0), ast::Number::Int(value) => !matches!(*value, Int::ZERO | Int::ONE), - _ => true, + ast::Number::Complex { .. } => true, }, LiteralExpressionRef::BytesLiteral(_) => true, } diff --git a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLR2004_magic_value_comparison.py.snap b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLR2004_magic_value_comparison.py.snap index a8701ef854602..2f292d9d9a950 100644 --- a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLR2004_magic_value_comparison.py.snap +++ b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLR2004_magic_value_comparison.py.snap @@ -10,49 +10,67 @@ magic_value_comparison.py:5:4: PLR2004 Magic value used in comparison, consider 6 | pass | -magic_value_comparison.py:38:12: PLR2004 Magic value used in comparison, consider replacing `2` with a constant variable +magic_value_comparison.py:47:12: PLR2004 Magic value used in comparison, consider replacing `2` with a constant variable | -36 | pass -37 | -38 | if argc != 2: # [magic-value-comparison] +45 | pass +46 | +47 | if argc != 2: # [magic-value-comparison] | ^ PLR2004 -39 | pass +48 | pass | -magic_value_comparison.py:41:12: PLR2004 Magic value used in comparison, consider replacing `-2` with a constant variable +magic_value_comparison.py:50:12: PLR2004 Magic value used in comparison, consider replacing `-2` with a constant variable | -39 | pass -40 | -41 | if argc != -2: # [magic-value-comparison] +48 | pass +49 | +50 | if argc != -2: # [magic-value-comparison] | ^^ PLR2004 -42 | pass +51 | pass | -magic_value_comparison.py:44:12: PLR2004 Magic value used in comparison, consider replacing `+2` with a constant variable +magic_value_comparison.py:53:12: PLR2004 Magic value used in comparison, consider replacing `+2` with a constant variable | -42 | pass -43 | -44 | if argc != +2: # [magic-value-comparison] +51 | pass +52 | +53 | if argc != +2: # [magic-value-comparison] | ^^ PLR2004 -45 | pass +54 | pass + | + +magic_value_comparison.py:56:12: PLR2004 Magic value used in comparison, consider replacing `-2.0` with a constant variable + | +54 | pass +55 | +56 | if argc != -2.0: # [magic-value-comparison] + | ^^^^ PLR2004 +57 | pass + | + +magic_value_comparison.py:59:12: PLR2004 Magic value used in comparison, consider replacing `+2.0` with a constant variable + | +57 | pass +58 | +59 | if argc != +2.0: # [magic-value-comparison] + | ^^^^ PLR2004 +60 | pass | -magic_value_comparison.py:65:21: PLR2004 Magic value used in comparison, consider replacing `3.141592653589793238` with a constant variable +magic_value_comparison.py:80:21: PLR2004 Magic value used in comparison, consider replacing `3.141592653589793238` with a constant variable | -63 | pi_estimation = 3.14 -64 | -65 | if pi_estimation == 3.141592653589793238: # [magic-value-comparison] +78 | pi_estimation = 3.14 +79 | +80 | if pi_estimation == 3.141592653589793238: # [magic-value-comparison] | ^^^^^^^^^^^^^^^^^^^^ PLR2004 -66 | pass +81 | pass | -magic_value_comparison.py:71:21: PLR2004 Magic value used in comparison, consider replacing `0x3` with a constant variable +magic_value_comparison.py:86:21: PLR2004 Magic value used in comparison, consider replacing `0x3` with a constant variable | -69 | pass -70 | -71 | if pi_estimation == 0x3: # [magic-value-comparison] +84 | pass +85 | +86 | if pi_estimation == 0x3: # [magic-value-comparison] | ^^^ PLR2004 -72 | pass +87 | pass | diff --git a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__allow_magic_value_types.snap b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__allow_magic_value_types.snap index 366263fabb914..31a4c8eda0b9b 100644 --- a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__allow_magic_value_types.snap +++ b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__allow_magic_value_types.snap @@ -1,31 +1,49 @@ --- source: crates/ruff_linter/src/rules/pylint/mod.rs --- -magic_value_comparison.py:59:22: PLR2004 Magic value used in comparison, consider replacing `"Hunter2"` with a constant variable +magic_value_comparison.py:56:12: PLR2004 Magic value used in comparison, consider replacing `-2.0` with a constant variable + | +54 | pass +55 | +56 | if argc != -2.0: # [magic-value-comparison] + | ^^^^ PLR2004 +57 | pass + | + +magic_value_comparison.py:59:12: PLR2004 Magic value used in comparison, consider replacing `+2.0` with a constant variable | 57 | pass 58 | -59 | if input_password == "Hunter2": # correct - | ^^^^^^^^^ PLR2004 +59 | if argc != +2.0: # [magic-value-comparison] + | ^^^^ PLR2004 60 | pass | -magic_value_comparison.py:65:21: PLR2004 Magic value used in comparison, consider replacing `3.141592653589793238` with a constant variable +magic_value_comparison.py:74:22: PLR2004 Magic value used in comparison, consider replacing `"Hunter2"` with a constant variable + | +72 | pass +73 | +74 | if input_password == "Hunter2": # correct + | ^^^^^^^^^ PLR2004 +75 | pass + | + +magic_value_comparison.py:80:21: PLR2004 Magic value used in comparison, consider replacing `3.141592653589793238` with a constant variable | -63 | pi_estimation = 3.14 -64 | -65 | if pi_estimation == 3.141592653589793238: # [magic-value-comparison] +78 | pi_estimation = 3.14 +79 | +80 | if pi_estimation == 3.141592653589793238: # [magic-value-comparison] | ^^^^^^^^^^^^^^^^^^^^ PLR2004 -66 | pass +81 | pass | -magic_value_comparison.py:77:18: PLR2004 Magic value used in comparison, consider replacing `b"something"` with a constant variable +magic_value_comparison.py:92:18: PLR2004 Magic value used in comparison, consider replacing `b"something"` with a constant variable | -75 | user_input = b"Hello, There!" -76 | -77 | if user_input == b"something": # correct +90 | user_input = b"Hello, There!" +91 | +92 | if user_input == b"something": # correct | ^^^^^^^^^^^^ PLR2004 -78 | pass +93 | pass |