fork/rust

git clone https://git.julia.blue/fork/rust

borsAuto merge of #103046 - JanBeh:PR_clarify_cmp_terminology, r=workingjubilee3bcad65fbf6

master
1.6 KiB62 linesraw
1#![allow(clippy::no_effect, clippy::unnecessary_operation, dead_code)]
2#![warn(clippy::cast_lossless)]
3
4fn main() {
5    // Test clippy::cast_lossless with casts to integer types
6    let _ = i16::from(1i8);
7    let _ = i32::from(1i8);
8    let _ = i64::from(1i8);
9    let _ = i16::from(1u8);
10    let _ = i32::from(1u8);
11    let _ = i64::from(1u8);
12    let _ = u16::from(1u8);
13    let _ = u32::from(1u8);
14    let _ = u64::from(1u8);
15    let _ = i32::from(1i16);
16    let _ = i64::from(1i16);
17    let _ = i32::from(1u16);
18    let _ = i64::from(1u16);
19    let _ = u32::from(1u16);
20    let _ = u64::from(1u16);
21    let _ = i64::from(1i32);
22    let _ = i64::from(1u32);
23    let _ = u64::from(1u32);
24
25    // Test with an expression wrapped in parens
26    let _ = u16::from(1u8 + 1u8);
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
32const fn abc(input: u16) -> u32 {
33    input as u32
34}
35
36// Same as the above issue. We can't suggest `::from` in const fns in impls
37mod cast_lossless_in_impl {
38    struct A;
39
40    impl A {
41        pub const fn convert(x: u32) -> u64 {
42            x as u64
43        }
44    }
45}
46
47#[derive(PartialEq, Debug)]
48#[repr(i64)]
49enum Test {
50    A = u32::MAX as i64 + 1,
51}
52
53fn issue11458() {
54    macro_rules! sign_cast {
55        ($var: ident, $src: ty, $dest: ty) => {
56            <$dest>::from_ne_bytes(($var as $src).to_ne_bytes())
57        };
58    }
59    let x = 10_u128;
60    let _ = i32::from(sign_cast!(x, u8, i8));
61    let _ = i32::from(sign_cast!(x, u8, i8) + 1);
62}