Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[refurb] Avoid false positives for math-constant (FURB152) #9290

Merged
merged 5 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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


Loading