From 50187016cb93802b405fbb7daba7cb503b3d113d Mon Sep 17 00:00:00 2001 From: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Date: Wed, 27 Dec 2023 06:45:37 -0800 Subject: [PATCH] [`refurb`] Avoid false positives for `math-constant` (`FURB152`) (#9290) Fixes #9281 --- .../resources/test/fixtures/refurb/FURB152.py | 30 ++ .../src/rules/refurb/rules/math_constant.rs | 20 +- ...es__refurb__tests__FURB152_FURB152.py.snap | 272 +++++++++++++++++- 3 files changed, 311 insertions(+), 11 deletions(-) diff --git a/crates/ruff_linter/resources/test/fixtures/refurb/FURB152.py b/crates/ruff_linter/resources/test/fixtures/refurb/FURB152.py index e9339a86fcd3d..598d811d75e7c 100644 --- a/crates/ruff_linter/resources/test/fixtures/refurb/FURB152.py +++ b/crates/ruff_linter/resources/test/fixtures/refurb/FURB152.py @@ -10,6 +10,36 @@ r = 3.141 # FURB152 +r = 3.142 # FURB152 + r = 3.1415 # FURB152 +r = 3.1416 # FURB152 + +r = 3.141592 # FURB152 + +r = 3.141593 # FURB152 + +r = 3.14159265 # FURB152 + +r = 3.141592653589793238462643383279 # FURB152 + +r = 3.14159266 # OK + e = 2.7 # OK + +e = 2.718 # FURB152 + +e = 2.7182 # FURB152 + +e = 2.7183 # FURB152 + +e = 2.719 # OK + +e = 2.71824 # OK + +e = 2.71820001 # OK + +e = 2.718200000000001 # OK + +e = 2.7182000000000001 # FURB152 diff --git a/crates/ruff_linter/src/rules/refurb/rules/math_constant.rs b/crates/ruff_linter/src/rules/refurb/rules/math_constant.rs index 23ef1aa8c1d21..6ec50d7c68a12 100644 --- a/crates/ruff_linter/src/rules/refurb/rules/math_constant.rs +++ b/crates/ruff_linter/src/rules/refurb/rules/math_constant.rs @@ -83,6 +83,20 @@ fn convert_to_constant( )) } +fn matches_constant(constant: f64, value: f64) -> bool { + for point in 2..=15 { + let rounded = (constant * 10_f64.powi(point)).round() / 10_f64.powi(point); + if (rounded - value).abs() < f64::EPSILON { + return true; + } + let rounded = (constant * 10_f64.powi(point)).floor() / 10_f64.powi(point); + if (rounded - value).abs() < f64::EPSILON { + return true; + } + } + false +} + #[derive(Debug, Clone, Copy)] enum Constant { Pi, @@ -94,11 +108,11 @@ impl Constant { #[allow(clippy::approx_constant)] fn from_value(value: f64) -> Option { if (3.14..3.15).contains(&value) { - Some(Self::Pi) + matches_constant(std::f64::consts::PI, value).then_some(Self::Pi) } else if (2.71..2.72).contains(&value) { - Some(Self::E) + matches_constant(std::f64::consts::E, value).then_some(Self::E) } else if (6.28..6.29).contains(&value) { - Some(Self::Tau) + matches_constant(std::f64::consts::TAU, value).then_some(Self::Tau) } else { None } diff --git a/crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__FURB152_FURB152.py.snap b/crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__FURB152_FURB152.py.snap index 094f30df73e58..9933f51589ab9 100644 --- a/crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__FURB152_FURB152.py.snap +++ b/crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__FURB152_FURB152.py.snap @@ -77,7 +77,7 @@ FURB152.py:11:5: FURB152 [*] Replace `3.141` with `math.pi` 11 | r = 3.141 # FURB152 | ^^^^^ FURB152 12 | -13 | r = 3.1415 # FURB152 +13 | r = 3.142 # FURB152 | = help: Use `math.pi` @@ -93,17 +93,17 @@ FURB152.py:11:5: FURB152 [*] Replace `3.141` with `math.pi` 11 |-r = 3.141 # FURB152 12 |+r = math.pi # FURB152 12 13 | -13 14 | r = 3.1415 # FURB152 +13 14 | r = 3.142 # FURB152 14 15 | -FURB152.py:13:5: FURB152 [*] Replace `3.1415` with `math.pi` +FURB152.py:13:5: FURB152 [*] Replace `3.142` with `math.pi` | 11 | r = 3.141 # FURB152 12 | -13 | r = 3.1415 # FURB152 - | ^^^^^^ FURB152 +13 | r = 3.142 # FURB152 + | ^^^^^ FURB152 14 | -15 | e = 2.7 # OK +15 | r = 3.1415 # FURB152 | = help: Use `math.pi` @@ -116,9 +116,265 @@ FURB152.py:13:5: FURB152 [*] Replace `3.1415` with `math.pi` 10 11 | 11 12 | r = 3.141 # FURB152 12 13 | -13 |-r = 3.1415 # FURB152 +13 |-r = 3.142 # FURB152 14 |+r = math.pi # FURB152 14 15 | -15 16 | e = 2.7 # OK +15 16 | r = 3.1415 # FURB152 +16 17 | + +FURB152.py:15:5: FURB152 [*] Replace `3.1415` with `math.pi` + | +13 | r = 3.142 # FURB152 +14 | +15 | r = 3.1415 # FURB152 + | ^^^^^^ FURB152 +16 | +17 | r = 3.1416 # FURB152 + | + = help: Use `math.pi` + +ℹ Safe fix + 1 |+import math +1 2 | r = 3.1 # OK +2 3 | +3 4 | A = 3.14 * r ** 2 # FURB152 +-------------------------------------------------------------------------------- +12 13 | +13 14 | r = 3.142 # FURB152 +14 15 | +15 |-r = 3.1415 # FURB152 + 16 |+r = math.pi # FURB152 +16 17 | +17 18 | r = 3.1416 # FURB152 +18 19 | + +FURB152.py:17:5: FURB152 [*] Replace `3.1416` with `math.pi` + | +15 | r = 3.1415 # FURB152 +16 | +17 | r = 3.1416 # FURB152 + | ^^^^^^ FURB152 +18 | +19 | r = 3.141592 # FURB152 + | + = help: Use `math.pi` + +ℹ Safe fix + 1 |+import math +1 2 | r = 3.1 # OK +2 3 | +3 4 | A = 3.14 * r ** 2 # FURB152 +-------------------------------------------------------------------------------- +14 15 | +15 16 | r = 3.1415 # FURB152 +16 17 | +17 |-r = 3.1416 # FURB152 + 18 |+r = math.pi # FURB152 +18 19 | +19 20 | r = 3.141592 # FURB152 +20 21 | + +FURB152.py:19:5: FURB152 [*] Replace `3.141592` with `math.pi` + | +17 | r = 3.1416 # FURB152 +18 | +19 | r = 3.141592 # FURB152 + | ^^^^^^^^ FURB152 +20 | +21 | r = 3.141593 # FURB152 + | + = help: Use `math.pi` + +ℹ Safe fix + 1 |+import math +1 2 | r = 3.1 # OK +2 3 | +3 4 | A = 3.14 * r ** 2 # FURB152 +-------------------------------------------------------------------------------- +16 17 | +17 18 | r = 3.1416 # FURB152 +18 19 | +19 |-r = 3.141592 # FURB152 + 20 |+r = math.pi # FURB152 +20 21 | +21 22 | r = 3.141593 # FURB152 +22 23 | + +FURB152.py:21:5: FURB152 [*] Replace `3.141593` with `math.pi` + | +19 | r = 3.141592 # FURB152 +20 | +21 | r = 3.141593 # FURB152 + | ^^^^^^^^ FURB152 +22 | +23 | r = 3.14159265 # FURB152 + | + = help: Use `math.pi` + +ℹ Safe fix + 1 |+import math +1 2 | r = 3.1 # OK +2 3 | +3 4 | A = 3.14 * r ** 2 # FURB152 +-------------------------------------------------------------------------------- +18 19 | +19 20 | r = 3.141592 # FURB152 +20 21 | +21 |-r = 3.141593 # FURB152 + 22 |+r = math.pi # FURB152 +22 23 | +23 24 | r = 3.14159265 # FURB152 +24 25 | + +FURB152.py:23:5: FURB152 [*] Replace `3.14159265` with `math.pi` + | +21 | r = 3.141593 # FURB152 +22 | +23 | r = 3.14159265 # FURB152 + | ^^^^^^^^^^ FURB152 +24 | +25 | r = 3.141592653589793238462643383279 # FURB152 + | + = help: Use `math.pi` + +ℹ Safe fix + 1 |+import math +1 2 | r = 3.1 # OK +2 3 | +3 4 | A = 3.14 * r ** 2 # FURB152 +-------------------------------------------------------------------------------- +20 21 | +21 22 | r = 3.141593 # FURB152 +22 23 | +23 |-r = 3.14159265 # FURB152 + 24 |+r = math.pi # FURB152 +24 25 | +25 26 | r = 3.141592653589793238462643383279 # FURB152 +26 27 | + +FURB152.py:25:5: FURB152 [*] Replace `3.141592653589793238462643383279` with `math.pi` + | +23 | r = 3.14159265 # FURB152 +24 | +25 | r = 3.141592653589793238462643383279 # FURB152 + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ FURB152 +26 | +27 | r = 3.14159266 # OK + | + = help: Use `math.pi` + +ℹ Safe fix + 1 |+import math +1 2 | r = 3.1 # OK +2 3 | +3 4 | A = 3.14 * r ** 2 # FURB152 +-------------------------------------------------------------------------------- +22 23 | +23 24 | r = 3.14159265 # FURB152 +24 25 | +25 |-r = 3.141592653589793238462643383279 # FURB152 + 26 |+r = math.pi # FURB152 +26 27 | +27 28 | r = 3.14159266 # OK +28 29 | + +FURB152.py:31:5: FURB152 [*] Replace `2.718` with `math.e` + | +29 | e = 2.7 # OK +30 | +31 | e = 2.718 # FURB152 + | ^^^^^ FURB152 +32 | +33 | e = 2.7182 # FURB152 + | + = help: Use `math.e` + +ℹ Safe fix + 1 |+import math +1 2 | r = 3.1 # OK +2 3 | +3 4 | A = 3.14 * r ** 2 # FURB152 +-------------------------------------------------------------------------------- +28 29 | +29 30 | e = 2.7 # OK +30 31 | +31 |-e = 2.718 # FURB152 + 32 |+e = math.e # FURB152 +32 33 | +33 34 | e = 2.7182 # FURB152 +34 35 | + +FURB152.py:33:5: FURB152 [*] Replace `2.7182` with `math.e` + | +31 | e = 2.718 # FURB152 +32 | +33 | e = 2.7182 # FURB152 + | ^^^^^^ FURB152 +34 | +35 | e = 2.7183 # FURB152 + | + = help: Use `math.e` + +ℹ Safe fix + 1 |+import math +1 2 | r = 3.1 # OK +2 3 | +3 4 | A = 3.14 * r ** 2 # FURB152 +-------------------------------------------------------------------------------- +30 31 | +31 32 | e = 2.718 # FURB152 +32 33 | +33 |-e = 2.7182 # FURB152 + 34 |+e = math.e # FURB152 +34 35 | +35 36 | e = 2.7183 # FURB152 +36 37 | + +FURB152.py:35:5: FURB152 [*] Replace `2.7183` with `math.e` + | +33 | e = 2.7182 # FURB152 +34 | +35 | e = 2.7183 # FURB152 + | ^^^^^^ FURB152 +36 | +37 | e = 2.719 # OK + | + = help: Use `math.e` + +ℹ Safe fix + 1 |+import math +1 2 | r = 3.1 # OK +2 3 | +3 4 | A = 3.14 * r ** 2 # FURB152 +-------------------------------------------------------------------------------- +32 33 | +33 34 | e = 2.7182 # FURB152 +34 35 | +35 |-e = 2.7183 # FURB152 + 36 |+e = math.e # FURB152 +36 37 | +37 38 | e = 2.719 # OK +38 39 | + +FURB152.py:45:5: FURB152 [*] Replace `2.7182000000000001` with `math.e` + | +43 | e = 2.718200000000001 # OK +44 | +45 | e = 2.7182000000000001 # FURB152 + | ^^^^^^^^^^^^^^^^^^ FURB152 + | + = help: Use `math.e` + +ℹ Safe fix + 1 |+import math +1 2 | r = 3.1 # OK +2 3 | +3 4 | A = 3.14 * r ** 2 # FURB152 +-------------------------------------------------------------------------------- +42 43 | +43 44 | e = 2.718200000000001 # OK +44 45 | +45 |-e = 2.7182000000000001 # FURB152 + 46 |+e = math.e # FURB152