diff --git a/crates/ruff_linter/resources/test/fixtures/refurb/FURB152.py b/crates/ruff_linter/resources/test/fixtures/refurb/FURB152.py index 598d811d75e7c..adbc62114287d 100644 --- a/crates/ruff_linter/resources/test/fixtures/refurb/FURB152.py +++ b/crates/ruff_linter/resources/test/fixtures/refurb/FURB152.py @@ -43,3 +43,10 @@ e = 2.718200000000001 # OK e = 2.7182000000000001 # FURB152 + +# Full-precision literals are still reported, with the same unsafe fix. +r = 3.141592653589793 # FURB152 + +e = 2.718281828459045 # FURB152 + +t = 6.283185307179586 # 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 35b8478825df4..78d99fc0242ba 100644 --- a/crates/ruff_linter/src/rules/refurb/rules/math_constant.rs +++ b/crates/ruff_linter/src/rules/refurb/rules/math_constant.rs @@ -26,10 +26,14 @@ use crate::{Edit, Fix, FixAvailability, Violation}; /// A = math.pi * r**2 /// ``` /// +/// ## Fix safety +/// This rule's fix is marked as unsafe, as replacing a literal with the constant +/// can change the result of a calculation. +/// /// ## References /// - [Python documentation: `math` constants](https://docs.python.org/3/library/math.html#constants) #[derive(ViolationMetadata)] -#[violation_metadata(preview_since = "v0.1.6", category = Category::Correctness)] +#[violation_metadata(preview_since = "v0.1.6", category = Category::Suspicious)] pub(crate) struct MathConstant { literal: String, constant: &'static str, @@ -78,7 +82,7 @@ fn convert_to_constant( literal.start(), checker.semantic(), )?; - Ok(Fix::safe_edits( + Ok(Fix::unsafe_edits( Edit::range_replacement(binding, literal.range()), [edit], )) diff --git a/crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__math-constant_FURB152.py.snap b/crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__math-constant_FURB152.py.snap index e8285f3b8929c..2506daa1308f3 100644 --- a/crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__math-constant_FURB152.py.snap +++ b/crates/ruff_linter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__math-constant_FURB152.py.snap @@ -20,6 +20,7 @@ help: Use `math.pi` 4 + A = math.pi * r ** 2 # FURB152 5 | | +note: This is an unsafe fix and may change runtime behavior FURB152 [*] Replace `6.28` with `math.tau` --> FURB152.py:5:5 @@ -42,6 +43,7 @@ help: Use `math.tau` 6 + C = math.tau * r # FURB152 7 | | +note: This is an unsafe fix and may change runtime behavior FURB152 [*] Replace `2.71` with `math.e` --> FURB152.py:7:5 @@ -63,6 +65,7 @@ help: Use `math.e` 8 + e = math.e # FURB152 9 | | +note: This is an unsafe fix and may change runtime behavior FURB152 [*] Replace `3.141` with `math.pi` --> FURB152.py:11:5 @@ -84,6 +87,7 @@ help: Use `math.pi` 12 + r = math.pi # FURB152 13 | | +note: This is an unsafe fix and may change runtime behavior FURB152 [*] Replace `3.142` with `math.pi` --> FURB152.py:13:5 @@ -105,6 +109,7 @@ help: Use `math.pi` 14 + r = math.pi # FURB152 15 | | +note: This is an unsafe fix and may change runtime behavior FURB152 [*] Replace `3.1415` with `math.pi` --> FURB152.py:15:5 @@ -126,6 +131,7 @@ help: Use `math.pi` 16 + r = math.pi # FURB152 17 | | +note: This is an unsafe fix and may change runtime behavior FURB152 [*] Replace `3.1416` with `math.pi` --> FURB152.py:17:5 @@ -147,6 +153,7 @@ help: Use `math.pi` 18 + r = math.pi # FURB152 19 | | +note: This is an unsafe fix and may change runtime behavior FURB152 [*] Replace `3.141592` with `math.pi` --> FURB152.py:19:5 @@ -168,6 +175,7 @@ help: Use `math.pi` 20 + r = math.pi # FURB152 21 | | +note: This is an unsafe fix and may change runtime behavior FURB152 [*] Replace `3.141593` with `math.pi` --> FURB152.py:21:5 @@ -189,6 +197,7 @@ help: Use `math.pi` 22 + r = math.pi # FURB152 23 | | +note: This is an unsafe fix and may change runtime behavior FURB152 [*] Replace `3.14159265` with `math.pi` --> FURB152.py:23:5 @@ -210,6 +219,7 @@ help: Use `math.pi` 24 + r = math.pi # FURB152 25 | | +note: This is an unsafe fix and may change runtime behavior FURB152 [*] Replace `3.141592653589793238462643383279` with `math.pi` --> FURB152.py:25:5 @@ -231,6 +241,7 @@ help: Use `math.pi` 26 + r = math.pi # FURB152 27 | | +note: This is an unsafe fix and may change runtime behavior FURB152 [*] Replace `2.718` with `math.e` --> FURB152.py:31:5 @@ -252,6 +263,7 @@ help: Use `math.e` 32 + e = math.e # FURB152 33 | | +note: This is an unsafe fix and may change runtime behavior FURB152 [*] Replace `2.7182` with `math.e` --> FURB152.py:33:5 @@ -273,6 +285,7 @@ help: Use `math.e` 34 + e = math.e # FURB152 35 | | +note: This is an unsafe fix and may change runtime behavior FURB152 [*] Replace `2.7183` with `math.e` --> FURB152.py:35:5 @@ -294,6 +307,7 @@ help: Use `math.e` 36 + e = math.e # FURB152 37 | | +note: This is an unsafe fix and may change runtime behavior FURB152 [*] Replace `2.7182000000000001` with `math.e` --> FURB152.py:45:5 @@ -302,6 +316,9 @@ FURB152 [*] Replace `2.7182000000000001` with `math.e` 44 | 45 | e = 2.7182000000000001 # FURB152 | ^^^^^^^^^^^^^^^^^^ +46 | +47 | # Full-precision literals are still reported, with the same unsafe fix. + | help: Use `math.e` | 1 + import math @@ -310,4 +327,67 @@ help: Use `math.e` 45 | - e = 2.7182000000000001 # FURB152 46 + e = math.e # FURB152 +47 | + | +note: This is an unsafe fix and may change runtime behavior + +FURB152 [*] Replace `3.141592653589793` with `math.pi` + --> FURB152.py:48:5 + | +47 | # Full-precision literals are still reported, with the same unsafe fix. +48 | r = 3.141592653589793 # FURB152 + | ^^^^^^^^^^^^^^^^^ +49 | +50 | e = 2.718281828459045 # FURB152 + | +help: Use `math.pi` + | +1 + import math +2 | r = 3.1 # OK +-------------------------------------------------------------------------------- +48 | # Full-precision literals are still reported, with the same unsafe fix. + - r = 3.141592653589793 # FURB152 +49 + r = math.pi # FURB152 +50 | + | +note: This is an unsafe fix and may change runtime behavior + +FURB152 [*] Replace `2.718281828459045` with `math.e` + --> FURB152.py:50:5 + | +48 | r = 3.141592653589793 # FURB152 +49 | +50 | e = 2.718281828459045 # FURB152 + | ^^^^^^^^^^^^^^^^^ +51 | +52 | t = 6.283185307179586 # FURB152 + | +help: Use `math.e` + | +1 + import math +2 | r = 3.1 # OK +-------------------------------------------------------------------------------- +50 | + - e = 2.718281828459045 # FURB152 +51 + e = math.e # FURB152 +52 | + | +note: This is an unsafe fix and may change runtime behavior + +FURB152 [*] Replace `6.283185307179586` with `math.tau` + --> FURB152.py:52:5 + | +50 | e = 2.718281828459045 # FURB152 +51 | +52 | t = 6.283185307179586 # FURB152 + | ^^^^^^^^^^^^^^^^^ +help: Use `math.tau` + | +1 + import math +2 | r = 3.1 # OK +-------------------------------------------------------------------------------- +52 | + - t = 6.283185307179586 # FURB152 +53 + t = math.tau # FURB152 | +note: This is an unsafe fix and may change runtime behavior