]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/fn_to_numeric_cast_any.rs
Rollup merge of #89876 - AlexApps99:const_ops, r=oli-obk
[rust.git] / src / tools / clippy / tests / ui / fn_to_numeric_cast_any.rs
1 #![warn(clippy::fn_to_numeric_cast_any)]
2 #![allow(clippy::fn_to_numeric_cast, clippy::fn_to_numeric_cast_with_truncation)]
3
4 fn foo() -> u8 {
5     0
6 }
7
8 fn generic_foo<T>(x: T) -> T {
9     x
10 }
11
12 trait Trait {
13     fn static_method() -> u32 {
14         2
15     }
16 }
17
18 struct Struct;
19
20 impl Trait for Struct {}
21
22 fn 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
38 fn static_method_to_integer() {
39     let _ = Struct::static_method as usize;
40 }
41
42 fn fn_with_fn_arg(f: fn(i32) -> u32) -> usize {
43     f as usize
44 }
45
46 fn fn_with_generic_static_trait_method<T: Trait>() -> usize {
47     T::static_method as usize
48 }
49
50 fn closure_to_fn_to_integer() {
51     let clos = |x| x * 2_u32;
52
53     let _ = (clos as fn(u32) -> u32) as usize;
54 }
55
56 fn fn_to_raw_ptr() {
57     let _ = foo as *const ();
58 }
59
60 fn cast_fn_to_self() {
61     // Casting to the same function pointer type should be permitted.
62     let _ = foo as fn() -> u8;
63 }
64
65 fn 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
70 fn 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
76 fn main() {}