]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/cast_lossless_float.rs
Add 'src/tools/rust-analyzer/' from commit '977e12a0bdc3e329af179ef3a9d466af9eb613bb'
[rust.git] / src / tools / clippy / tests / ui / cast_lossless_float.rs
1 // run-rustfix
2
3 #![allow(clippy::no_effect, clippy::unnecessary_operation, dead_code)]
4 #![warn(clippy::cast_lossless)]
5
6 fn main() {
7     // Test clippy::cast_lossless with casts to floating-point types
8     let x0 = 1i8;
9     let _ = x0 as f32;
10     let _ = x0 as f64;
11     let x1 = 1u8;
12     let _ = x1 as f32;
13     let _ = x1 as f64;
14     let x2 = 1i16;
15     let _ = x2 as f32;
16     let _ = x2 as f64;
17     let x3 = 1u16;
18     let _ = x3 as f32;
19     let _ = x3 as f64;
20     let x4 = 1i32;
21     let _ = x4 as f64;
22     let x5 = 1u32;
23     let _ = x5 as f64;
24
25     // Test with casts from floating-point types
26     let _ = 1.0f32 as f64;
27 }
28
29 // The lint would suggest using `f64::from(input)` here but the `XX::from` function is not const,
30 // so we skip the lint if the expression is in a const fn.
31 // See #3656
32 const fn abc(input: f32) -> f64 {
33     input as f64
34 }
35
36 // Same as the above issue. We can't suggest `::from` in const fns in impls
37 mod cast_lossless_in_impl {
38     struct A;
39
40     impl A {
41         pub const fn convert(x: f32) -> f64 {
42             x as f64
43         }
44     }
45 }