Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/refurb/FURB152.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 12 additions & 2 deletions crates/ruff_linter/src/rules/refurb/rules/math_constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,20 @@ use crate::{Edit, Fix, FixAvailability, Violation};
/// A = math.pi * r**2
/// ```
///
/// ## Known problems
/// A literal is matched on its digits alone, so a value that merely happens to
/// begin with the same digits as a mathematical constant is flagged even when it
/// means something else entirely. A measurement that rounds to `3.14`, or a price
/// of `2.718`, is indistinguishable here from an approximation of π or e.
///
Comment thread
ntBre marked this conversation as resolved.
Outdated
/// ## 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,
Expand Down Expand Up @@ -78,7 +88,7 @@ fn convert_to_constant(
literal.start(),
checker.semantic(),
)?;
Ok(Fix::safe_edits(
Ok(Fix::unsafe_edits(
Edit::range_replacement(binding, literal.range()),
[edit],
))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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