-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathfn_to_numeric_cast_any.rs
103 lines (77 loc) · 2 KB
/
fn_to_numeric_cast_any.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#![warn(clippy::fn_to_numeric_cast_any)]
#![allow(clippy::fn_to_numeric_cast, clippy::fn_to_numeric_cast_with_truncation)]
//@no-rustfix
fn foo() -> u8 {
0
}
fn generic_foo<T>(x: T) -> T {
x
}
trait Trait {
fn static_method() -> u32 {
2
}
}
struct Struct;
impl Trait for Struct {}
fn fn_pointer_to_integer() {
let _ = foo as i8;
//~^ fn_to_numeric_cast_any
let _ = foo as i16;
//~^ fn_to_numeric_cast_any
let _ = foo as i32;
//~^ fn_to_numeric_cast_any
let _ = foo as i64;
//~^ fn_to_numeric_cast_any
let _ = foo as i128;
//~^ fn_to_numeric_cast_any
let _ = foo as isize;
//~^ fn_to_numeric_cast_any
let _ = foo as u8;
//~^ fn_to_numeric_cast_any
let _ = foo as u16;
//~^ fn_to_numeric_cast_any
let _ = foo as u32;
//~^ fn_to_numeric_cast_any
let _ = foo as u64;
//~^ fn_to_numeric_cast_any
let _ = foo as u128;
//~^ fn_to_numeric_cast_any
let _ = foo as usize;
//~^ fn_to_numeric_cast_any
}
fn static_method_to_integer() {
let _ = Struct::static_method as usize;
//~^ fn_to_numeric_cast_any
}
fn fn_with_fn_arg(f: fn(i32) -> u32) -> usize {
f as usize
//~^ fn_to_numeric_cast_any
}
fn fn_with_generic_static_trait_method<T: Trait>() -> usize {
T::static_method as usize
//~^ fn_to_numeric_cast_any
}
fn closure_to_fn_to_integer() {
let clos = |x| x * 2_u32;
let _ = (clos as fn(u32) -> u32) as usize;
//~^ fn_to_numeric_cast_any
}
fn fn_to_raw_ptr() {
let _ = foo as *const ();
let _ = foo as *mut ();
}
fn cast_fn_to_self() {
// Casting to the same function pointer type should be permitted.
let _ = foo as fn() -> u8;
}
fn cast_generic_to_concrete() {
// Casting to a more concrete function pointer type should be permitted.
let _ = generic_foo as fn(usize) -> usize;
}
fn cast_closure_to_fn() {
// Casting a closure to a function pointer should be permitted.
let id = |x| x;
let _ = id as fn(usize) -> usize;
}
fn main() {}