]> git.lizzy.rs Git - rust.git/blob - src/docs/fn_to_numeric_cast_any.txt
Auto merge of #9421 - xphoniex:fix-#9420, r=giraffate
[rust.git] / src / docs / fn_to_numeric_cast_any.txt
1 ### What it does
2 Checks for casts of a function pointer to any integer type.
3
4 ### Why is this bad?
5 Casting a function pointer to an integer can have surprising results and can occur
6 accidentally if parentheses are omitted from a function call. If you aren't doing anything
7 low-level with function pointers then you can opt-out of casting functions to integers in
8 order to avoid mistakes. Alternatively, you can use this lint to audit all uses of function
9 pointer casts in your code.
10
11 ### Example
12 ```
13 // fn1 is cast as `usize`
14 fn fn1() -> u16 {
15     1
16 };
17 let _ = fn1 as usize;
18 ```
19
20 Use instead:
21 ```
22 // maybe you intended to call the function?
23 fn fn2() -> u16 {
24     1
25 };
26 let _ = fn2() as usize;
27
28 // or
29
30 // maybe you intended to cast it to a function type?
31 fn fn3() -> u16 {
32     1
33 }
34 let _ = fn3 as fn() -> u16;
35 ```