Skip to content

Commit 6e14e60

Browse files
committed
Fix {subopt,imprec}_float not lint const.*(const)
Fixes rust-lang#9402 Fixes rust-lang#9201
1 parent 09e4659 commit 6e14e60

File tree

4 files changed

+62
-28
lines changed

4 files changed

+62
-28
lines changed

clippy_lints/src/floating_point_arithmetic.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -238,23 +238,23 @@ fn get_integer_from_float_constant(value: &Constant) -> Option<i32> {
238238
fn check_powf(cx: &LateContext<'_>, expr: &Expr<'_>, args: &[Expr<'_>]) {
239239
// Check receiver
240240
if let Some((value, _)) = constant(cx, cx.typeck_results(), &args[0]) {
241-
let method = if F32(f32_consts::E) == value || F64(f64_consts::E) == value {
242-
"exp"
241+
if let Some(method) = if F32(f32_consts::E) == value || F64(f64_consts::E) == value {
242+
Some("exp")
243243
} else if F32(2.0) == value || F64(2.0) == value {
244-
"exp2"
244+
Some("exp2")
245245
} else {
246-
return;
247-
};
248-
249-
span_lint_and_sugg(
250-
cx,
251-
SUBOPTIMAL_FLOPS,
252-
expr.span,
253-
"exponent for bases 2 and e can be computed more accurately",
254-
"consider using",
255-
format!("{}.{}()", prepare_receiver_sugg(cx, &args[1]), method),
256-
Applicability::MachineApplicable,
257-
);
246+
None
247+
} {
248+
span_lint_and_sugg(
249+
cx,
250+
SUBOPTIMAL_FLOPS,
251+
expr.span,
252+
"exponent for bases 2 and e can be computed more accurately",
253+
"consider using",
254+
format!("{}.{}()", prepare_receiver_sugg(cx, &args[1]), method),
255+
Applicability::MachineApplicable,
256+
);
257+
}
258258
}
259259

260260
// Check argument

tests/ui/floating_point_powf.fixed

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ fn main() {
1818
let _ = x.powi(-16_777_215);
1919
let _ = (x as f32).powi(-16_777_215);
2020
let _ = (x as f32).powi(3);
21+
let _ = (1.5_f32 + 1.0).cbrt();
22+
let _ = 1.5_f64.cbrt();
23+
let _ = 1.5_f64.sqrt();
24+
let _ = 1.5_f64.powi(3);
25+
2126
// Cases where the lint shouldn't be applied
2227
let _ = x.powf(2.1);
2328
let _ = x.powf(-2.1);

tests/ui/floating_point_powf.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ fn main() {
1818
let _ = x.powf(-16_777_215.0);
1919
let _ = (x as f32).powf(-16_777_215.0);
2020
let _ = (x as f32).powf(3.0);
21+
let _ = (1.5_f32 + 1.0).powf(1.0 / 3.0);
22+
let _ = 1.5_f64.powf(1.0 / 3.0);
23+
let _ = 1.5_f64.powf(1.0 / 2.0);
24+
let _ = 1.5_f64.powf(3.0);
25+
2126
// Cases where the lint shouldn't be applied
2227
let _ = x.powf(2.1);
2328
let _ = x.powf(-2.1);

tests/ui/floating_point_powf.stderr

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -92,77 +92,101 @@ error: exponentiation with integer powers can be computed more efficiently
9292
LL | let _ = (x as f32).powf(3.0);
9393
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x as f32).powi(3)`
9494

95+
error: cube-root of a number can be computed more accurately
96+
--> $DIR/floating_point_powf.rs:21:13
97+
|
98+
LL | let _ = (1.5_f32 + 1.0).powf(1.0 / 3.0);
99+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(1.5_f32 + 1.0).cbrt()`
100+
101+
error: cube-root of a number can be computed more accurately
102+
--> $DIR/floating_point_powf.rs:22:13
103+
|
104+
LL | let _ = 1.5_f64.powf(1.0 / 3.0);
105+
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `1.5_f64.cbrt()`
106+
107+
error: square-root of a number can be computed more efficiently and accurately
108+
--> $DIR/floating_point_powf.rs:23:13
109+
|
110+
LL | let _ = 1.5_f64.powf(1.0 / 2.0);
111+
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `1.5_f64.sqrt()`
112+
113+
error: exponentiation with integer powers can be computed more efficiently
114+
--> $DIR/floating_point_powf.rs:24:13
115+
|
116+
LL | let _ = 1.5_f64.powf(3.0);
117+
| ^^^^^^^^^^^^^^^^^ help: consider using: `1.5_f64.powi(3)`
118+
95119
error: exponent for bases 2 and e can be computed more accurately
96-
--> $DIR/floating_point_powf.rs:28:13
120+
--> $DIR/floating_point_powf.rs:33:13
97121
|
98122
LL | let _ = 2f64.powf(x);
99123
| ^^^^^^^^^^^^ help: consider using: `x.exp2()`
100124

101125
error: exponent for bases 2 and e can be computed more accurately
102-
--> $DIR/floating_point_powf.rs:29:13
126+
--> $DIR/floating_point_powf.rs:34:13
103127
|
104128
LL | let _ = 2f64.powf(3.1);
105129
| ^^^^^^^^^^^^^^ help: consider using: `3.1f64.exp2()`
106130

107131
error: exponent for bases 2 and e can be computed more accurately
108-
--> $DIR/floating_point_powf.rs:30:13
132+
--> $DIR/floating_point_powf.rs:35:13
109133
|
110134
LL | let _ = 2f64.powf(-3.1);
111135
| ^^^^^^^^^^^^^^^ help: consider using: `(-3.1f64).exp2()`
112136

113137
error: exponent for bases 2 and e can be computed more accurately
114-
--> $DIR/floating_point_powf.rs:31:13
138+
--> $DIR/floating_point_powf.rs:36:13
115139
|
116140
LL | let _ = std::f64::consts::E.powf(x);
117141
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.exp()`
118142

119143
error: exponent for bases 2 and e can be computed more accurately
120-
--> $DIR/floating_point_powf.rs:32:13
144+
--> $DIR/floating_point_powf.rs:37:13
121145
|
122146
LL | let _ = std::f64::consts::E.powf(3.1);
123147
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `3.1f64.exp()`
124148

125149
error: exponent for bases 2 and e can be computed more accurately
126-
--> $DIR/floating_point_powf.rs:33:13
150+
--> $DIR/floating_point_powf.rs:38:13
127151
|
128152
LL | let _ = std::f64::consts::E.powf(-3.1);
129153
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(-3.1f64).exp()`
130154

131155
error: square-root of a number can be computed more efficiently and accurately
132-
--> $DIR/floating_point_powf.rs:34:13
156+
--> $DIR/floating_point_powf.rs:39:13
133157
|
134158
LL | let _ = x.powf(1.0 / 2.0);
135159
| ^^^^^^^^^^^^^^^^^ help: consider using: `x.sqrt()`
136160

137161
error: cube-root of a number can be computed more accurately
138-
--> $DIR/floating_point_powf.rs:35:13
162+
--> $DIR/floating_point_powf.rs:40:13
139163
|
140164
LL | let _ = x.powf(1.0 / 3.0);
141165
| ^^^^^^^^^^^^^^^^^ help: consider using: `x.cbrt()`
142166

143167
error: exponentiation with integer powers can be computed more efficiently
144-
--> $DIR/floating_point_powf.rs:36:13
168+
--> $DIR/floating_point_powf.rs:41:13
145169
|
146170
LL | let _ = x.powf(3.0);
147171
| ^^^^^^^^^^^ help: consider using: `x.powi(3)`
148172

149173
error: exponentiation with integer powers can be computed more efficiently
150-
--> $DIR/floating_point_powf.rs:37:13
174+
--> $DIR/floating_point_powf.rs:42:13
151175
|
152176
LL | let _ = x.powf(-2.0);
153177
| ^^^^^^^^^^^^ help: consider using: `x.powi(-2)`
154178

155179
error: exponentiation with integer powers can be computed more efficiently
156-
--> $DIR/floating_point_powf.rs:38:13
180+
--> $DIR/floating_point_powf.rs:43:13
157181
|
158182
LL | let _ = x.powf(-2_147_483_648.0);
159183
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.powi(-2_147_483_648)`
160184

161185
error: exponentiation with integer powers can be computed more efficiently
162-
--> $DIR/floating_point_powf.rs:39:13
186+
--> $DIR/floating_point_powf.rs:44:13
163187
|
164188
LL | let _ = x.powf(2_147_483_647.0);
165189
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.powi(2_147_483_647)`
166190

167-
error: aborting due to 27 previous errors
191+
error: aborting due to 31 previous errors
168192

0 commit comments

Comments
 (0)