blob: 9ceb2ec96062f1719f4f9fe6cbfdab3ab6bc29ff (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
#![warn(clippy::suboptimal_flops)]
/// Allow suboptimal_ops in constant context
pub const fn in_const_context() {
let a: f64 = 1234.567;
let b: f64 = 45.67834;
let c: f64 = 0.0004;
let _ = a * b + c;
let _ = c + a * b;
}
fn main() {
let a: f64 = 1234.567;
let b: f64 = 45.67834;
let c: f64 = 0.0004;
let d: f64 = 0.0001;
let _ = a * b + c;
//~^ suboptimal_flops
let _ = a * b - c;
//~^ suboptimal_flops
let _ = c + a * b;
//~^ suboptimal_flops
let _ = c - a * b;
//~^ suboptimal_flops
let _ = a + 2.0 * 4.0;
//~^ suboptimal_flops
let _ = a + 2. * 4.;
//~^ suboptimal_flops
let _ = (a * b) + c;
//~^ suboptimal_flops
let _ = c + (a * b);
//~^ suboptimal_flops
let _ = a * b * c + d;
//~^ suboptimal_flops
let _ = a.mul_add(b, c) * a.mul_add(b, c) + a.mul_add(b, c) + c;
//~^ suboptimal_flops
let _ = 1234.567_f64 * 45.67834_f64 + 0.0004_f64;
//~^ suboptimal_flops
let _ = (a * a + b).sqrt();
//~^ suboptimal_flops
let u = 1usize;
let _ = a - (b * u as f64);
//~^ suboptimal_flops
// Cases where the lint shouldn't be applied
let _ = (a * a + b * b).sqrt();
}
fn _issue11831() {
struct NotAFloat;
impl std::ops::Add<f64> for NotAFloat {
type Output = Self;
fn add(self, _: f64) -> Self {
NotAFloat
}
}
let a = NotAFloat;
let b = 1.0_f64;
let c = 1.0;
let _ = a + b * c;
}
fn _issue14897() {
let x = 1.0;
let _ = x * 2.0 + 0.5; // should not suggest mul_add
let _ = 0.5 + x * 2.0; // should not suggest mul_add
let _ = 0.5 + x * 1.2; // should not suggest mul_add
let _ = 1.2 + x * 1.2; // should not suggest mul_add
let x = -1.0;
let _ = 0.5 + x * 1.2; // should not suggest mul_add
let x = { 4.0 };
let _ = 0.5 + x * 1.2; // should not suggest mul_add
let x = if 1 > 2 { 1.0 } else { 2.0 };
let _ = 0.5 + x * 1.2; // should not suggest mul_add
let x = 2.4 + 1.2;
let _ = 0.5 + x * 1.2; // should not suggest mul_add
let f = || 4.0;
let x = f();
let _ = 0.5 + f() * 1.2; // should not suggest mul_add
let _ = 0.5 + x * 1.2; // should not suggest mul_add
let x = 0.1;
let y = x;
let z = y;
let _ = 0.5 + z * 1.2; // should not suggest mul_add
let _ = 0.5 + 2.0 * x;
//~^ suboptimal_flops
let _ = 2.0 * x + 0.5;
//~^ suboptimal_flops
let _ = x + 2.0 * 4.0;
//~^ suboptimal_flops
let y: f64 = 1.0;
let _ = y * 2.0 + 0.5;
//~^ suboptimal_flops
let _ = 1.0 * 2.0 + 0.5;
//~^ suboptimal_flops
}
|