]> git.lizzy.rs Git - rust.git/blob - tests/ui/fn_to_numeric_cast.rs
Auto merge of #3635 - matthiaskrgr:revert_random_state_3603, r=xfix
[rust.git] / tests / ui / fn_to_numeric_cast.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 // only-64bit
11
12 #![warn(clippy::fn_to_numeric_cast, clippy::fn_to_numeric_cast_with_truncation)]
13
14 fn foo() -> String {
15     String::new()
16 }
17
18 fn test_function_to_numeric_cast() {
19     let _ = foo as i8;
20     let _ = foo as i16;
21     let _ = foo as i32;
22     let _ = foo as i64;
23     let _ = foo as i128;
24     let _ = foo as isize;
25
26     let _ = foo as u8;
27     let _ = foo as u16;
28     let _ = foo as u32;
29     let _ = foo as u64;
30     let _ = foo as u128;
31
32     // Casting to usize is OK and should not warn
33     let _ = foo as usize;
34
35     // Cast `f` (a `FnDef`) to `fn()` should not warn
36     fn f() {}
37     let _ = f as fn();
38 }
39
40 fn test_function_var_to_numeric_cast() {
41     let abc: fn() -> String = foo;
42
43     let _ = abc as i8;
44     let _ = abc as i16;
45     let _ = abc as i32;
46     let _ = abc as i64;
47     let _ = abc as i128;
48     let _ = abc as isize;
49
50     let _ = abc as u8;
51     let _ = abc as u16;
52     let _ = abc as u32;
53     let _ = abc as u64;
54     let _ = abc as u128;
55
56     // Casting to usize is OK and should not warn
57     let _ = abc as usize;
58 }
59
60 fn fn_with_fn_args(f: fn(i32) -> i32) -> i32 {
61     f as i32
62 }
63
64 fn main() {}