Skip to content

Commit

Permalink
[refurb] Avoid false positives for math-constant (FURB152) (#9290)
Browse files Browse the repository at this point in the history
Fixes #9281
  • Loading branch information
hauntsaninja authored Dec 27, 2023
1 parent 2951339 commit 5018701
Show file tree
Hide file tree
Showing 3 changed files with 311 additions and 11 deletions.
30 changes: 30 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/refurb/FURB152.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
20 changes: 17 additions & 3 deletions crates/ruff_linter/src/rules/refurb/rules/math_constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -94,11 +108,11 @@ impl Constant {
#[allow(clippy::approx_constant)]
fn from_value(value: f64) -> Option<Self> {
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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand All @@ -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`

Expand All @@ -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


0 comments on commit 5018701

Please sign in to comment.