1 #![warn(clippy::fn_to_numeric_cast_any)] 2 #![allow(clippy::fn_to_numeric_cast, clippy::fn_to_numeric_cast_with_truncation)] 3 foo() -> u84fn foo() -> u8 { 5 0 6 } 7 generic_foo<T>(x: T) -> T8fn generic_foo<T>(x: T) -> T { 9 x 10 } 11 12 trait Trait { static_method() -> u3213 fn static_method() -> u32 { 14 2 15 } 16 } 17 18 struct Struct; 19 20 impl Trait for Struct {} 21 fn_pointer_to_integer()22fn fn_pointer_to_integer() { 23 let _ = foo as i8; 24 let _ = foo as i16; 25 let _ = foo as i32; 26 let _ = foo as i64; 27 let _ = foo as i128; 28 let _ = foo as isize; 29 30 let _ = foo as u8; 31 let _ = foo as u16; 32 let _ = foo as u32; 33 let _ = foo as u64; 34 let _ = foo as u128; 35 let _ = foo as usize; 36 } 37 static_method_to_integer()38fn static_method_to_integer() { 39 let _ = Struct::static_method as usize; 40 } 41 fn_with_fn_arg(f: fn(i32) -> u32) -> usize42fn fn_with_fn_arg(f: fn(i32) -> u32) -> usize { 43 f as usize 44 } 45 fn_with_generic_static_trait_method<T: Trait>() -> usize46fn fn_with_generic_static_trait_method<T: Trait>() -> usize { 47 T::static_method as usize 48 } 49 closure_to_fn_to_integer()50fn closure_to_fn_to_integer() { 51 let clos = |x| x * 2_u32; 52 53 let _ = (clos as fn(u32) -> u32) as usize; 54 } 55 fn_to_raw_ptr()56fn fn_to_raw_ptr() { 57 let _ = foo as *const (); 58 } 59 cast_fn_to_self()60fn cast_fn_to_self() { 61 // Casting to the same function pointer type should be permitted. 62 let _ = foo as fn() -> u8; 63 } 64 cast_generic_to_concrete()65fn cast_generic_to_concrete() { 66 // Casting to a more concrete function pointer type should be permitted. 67 let _ = generic_foo as fn(usize) -> usize; 68 } 69 cast_closure_to_fn()70fn cast_closure_to_fn() { 71 // Casting a closure to a function pointer should be permitted. 72 let id = |x| x; 73 let _ = id as fn(usize) -> usize; 74 } 75 main()76fn main() {} 77