diff --git a/crates/ruff_linter/resources/test/fixtures/refurb/FURB152.py b/crates/ruff_linter/resources/test/fixtures/refurb/FURB152.py index 5e1bfbb16640d..e9339a86fcd3d 100644 --- a/crates/ruff_linter/resources/test/fixtures/refurb/FURB152.py +++ b/crates/ruff_linter/resources/test/fixtures/refurb/FURB152.py @@ -5,3 +5,11 @@ C = 6.28 * r # FURB152 e = 2.71 # FURB152 + +r = 3.15 # OK + +r = 3.141 # FURB152 + +r = 3.1415 # FURB152 + +e = 2.7 # OK 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 6b590275a38ba..23ef1aa8c1d21 100644 --- a/crates/ruff_linter/src/rules/refurb/rules/math_constant.rs +++ b/crates/ruff_linter/src/rules/refurb/rules/math_constant.rs @@ -53,23 +53,17 @@ pub(crate) fn math_constant(checker: &mut Checker, literal: &ast::ExprNumberLite let Number::Float(value) = literal.value else { return; }; - for (real_value, constant) in [ - (std::f64::consts::PI, "pi"), - (std::f64::consts::E, "e"), - (std::f64::consts::TAU, "tau"), - ] { - if (value - real_value).abs() < 1e-2 { - let mut diagnostic = Diagnostic::new( - MathConstant { - literal: checker.locator().slice(literal).into(), - constant, - }, - literal.range(), - ); - diagnostic.try_set_fix(|| convert_to_constant(literal, constant, checker)); - checker.diagnostics.push(diagnostic); - return; - } + + if let Some(constant) = Constant::from_value(value) { + let mut diagnostic = Diagnostic::new( + MathConstant { + literal: checker.locator().slice(literal).into(), + constant: constant.name(), + }, + literal.range(), + ); + diagnostic.try_set_fix(|| convert_to_constant(literal, constant.name(), checker)); + checker.diagnostics.push(diagnostic); } } @@ -88,3 +82,33 @@ fn convert_to_constant( [edit], )) } + +#[derive(Debug, Clone, Copy)] +enum Constant { + Pi, + E, + Tau, +} + +impl Constant { + #[allow(clippy::approx_constant)] + fn from_value(value: f64) -> Option { + if (3.14..3.15).contains(&value) { + Some(Self::Pi) + } else if (2.71..2.72).contains(&value) { + Some(Self::E) + } else if (6.28..6.29).contains(&value) { + Some(Self::Tau) + } else { + None + } + } + + fn name(self) -> &'static str { + match self { + Constant::Pi => "pi", + Constant::E => "e", + Constant::Tau => "tau", + } + } +} 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 aa97aead864cb..094f30df73e58 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 @@ -43,6 +43,7 @@ FURB152.py:5:5: FURB152 [*] Replace `6.28` with `math.tau` 6 |+C = math.tau * r # FURB152 6 7 | 7 8 | e = 2.71 # FURB152 +8 9 | FURB152.py:7:5: FURB152 [*] Replace `2.71` with `math.e` | @@ -50,6 +51,8 @@ FURB152.py:7:5: FURB152 [*] Replace `2.71` with `math.e` 6 | 7 | e = 2.71 # FURB152 | ^^^^ FURB152 +8 | +9 | r = 3.15 # OK | = help: Use `math.e` @@ -63,5 +66,59 @@ FURB152.py:7:5: FURB152 [*] Replace `2.71` with `math.e` 6 7 | 7 |-e = 2.71 # FURB152 8 |+e = math.e # FURB152 +8 9 | +9 10 | r = 3.15 # OK +10 11 | + +FURB152.py:11:5: FURB152 [*] Replace `3.141` with `math.pi` + | + 9 | r = 3.15 # OK +10 | +11 | r = 3.141 # FURB152 + | ^^^^^ FURB152 +12 | +13 | r = 3.1415 # 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 +-------------------------------------------------------------------------------- +8 9 | +9 10 | r = 3.15 # OK +10 11 | +11 |-r = 3.141 # FURB152 + 12 |+r = math.pi # FURB152 +12 13 | +13 14 | r = 3.1415 # FURB152 +14 15 | + +FURB152.py:13:5: FURB152 [*] Replace `3.1415` with `math.pi` + | +11 | r = 3.141 # FURB152 +12 | +13 | r = 3.1415 # FURB152 + | ^^^^^^ FURB152 +14 | +15 | e = 2.7 # 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 +-------------------------------------------------------------------------------- +10 11 | +11 12 | r = 3.141 # FURB152 +12 13 | +13 |-r = 3.1415 # FURB152 + 14 |+r = math.pi # FURB152 +14 15 | +15 16 | e = 2.7 # OK