2023-04-20 09:37:15 -05:00
|
|
|
//@ignore-32bit
|
2023-07-27 06:40:22 -05:00
|
|
|
//@no-rustfix
|
2018-10-05 00:49:08 -05:00
|
|
|
#![warn(clippy::fn_to_numeric_cast, clippy::fn_to_numeric_cast_with_truncation)]
|
2018-10-03 05:02:06 -05:00
|
|
|
|
2018-12-09 16:26:16 -06:00
|
|
|
fn foo() -> String {
|
|
|
|
String::new()
|
|
|
|
}
|
2018-10-03 05:02:06 -05:00
|
|
|
|
|
|
|
fn test_function_to_numeric_cast() {
|
|
|
|
let _ = foo as i8;
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: casting function pointer `foo` to `i8`, which truncates the value
|
|
|
|
//~| NOTE: `-D clippy::fn-to-numeric-cast-with-truncation` implied by `-D warnings`
|
2018-10-03 05:02:06 -05:00
|
|
|
let _ = foo as i16;
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: casting function pointer `foo` to `i16`, which truncates the value
|
2018-10-03 05:02:06 -05:00
|
|
|
let _ = foo as i32;
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: casting function pointer `foo` to `i32`, which truncates the value
|
2018-10-03 05:02:06 -05:00
|
|
|
let _ = foo as i64;
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: casting function pointer `foo` to `i64`
|
|
|
|
//~| NOTE: `-D clippy::fn-to-numeric-cast` implied by `-D warnings`
|
2018-10-03 05:02:06 -05:00
|
|
|
let _ = foo as i128;
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: casting function pointer `foo` to `i128`
|
2018-10-03 05:02:06 -05:00
|
|
|
let _ = foo as isize;
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: casting function pointer `foo` to `isize`
|
2018-10-03 05:02:06 -05:00
|
|
|
|
|
|
|
let _ = foo as u8;
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: casting function pointer `foo` to `u8`, which truncates the value
|
2018-10-03 05:02:06 -05:00
|
|
|
let _ = foo as u16;
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: casting function pointer `foo` to `u16`, which truncates the value
|
2018-10-03 05:02:06 -05:00
|
|
|
let _ = foo as u32;
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: casting function pointer `foo` to `u32`, which truncates the value
|
2018-10-03 05:02:06 -05:00
|
|
|
let _ = foo as u64;
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: casting function pointer `foo` to `u64`
|
2018-10-03 05:02:06 -05:00
|
|
|
let _ = foo as u128;
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: casting function pointer `foo` to `u128`
|
2018-10-03 05:02:06 -05:00
|
|
|
|
|
|
|
// Casting to usize is OK and should not warn
|
|
|
|
let _ = foo as usize;
|
2018-10-07 05:18:40 -05:00
|
|
|
|
|
|
|
// Cast `f` (a `FnDef`) to `fn()` should not warn
|
|
|
|
fn f() {}
|
|
|
|
let _ = f as fn();
|
2018-10-03 05:02:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_function_var_to_numeric_cast() {
|
|
|
|
let abc: fn() -> String = foo;
|
|
|
|
|
|
|
|
let _ = abc as i8;
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: casting function pointer `abc` to `i8`, which truncates the value
|
2018-10-03 05:02:06 -05:00
|
|
|
let _ = abc as i16;
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: casting function pointer `abc` to `i16`, which truncates the value
|
2018-10-03 05:02:06 -05:00
|
|
|
let _ = abc as i32;
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: casting function pointer `abc` to `i32`, which truncates the value
|
2018-10-03 05:02:06 -05:00
|
|
|
let _ = abc as i64;
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: casting function pointer `abc` to `i64`
|
2018-10-03 05:02:06 -05:00
|
|
|
let _ = abc as i128;
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: casting function pointer `abc` to `i128`
|
2018-10-03 05:02:06 -05:00
|
|
|
let _ = abc as isize;
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: casting function pointer `abc` to `isize`
|
2018-10-03 05:02:06 -05:00
|
|
|
|
|
|
|
let _ = abc as u8;
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: casting function pointer `abc` to `u8`, which truncates the value
|
2018-10-03 05:02:06 -05:00
|
|
|
let _ = abc as u16;
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: casting function pointer `abc` to `u16`, which truncates the value
|
2018-10-03 05:02:06 -05:00
|
|
|
let _ = abc as u32;
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: casting function pointer `abc` to `u32`, which truncates the value
|
2018-10-03 05:02:06 -05:00
|
|
|
let _ = abc as u64;
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: casting function pointer `abc` to `u64`
|
2018-10-03 05:02:06 -05:00
|
|
|
let _ = abc as u128;
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: casting function pointer `abc` to `u128`
|
2018-10-03 05:02:06 -05:00
|
|
|
|
|
|
|
// Casting to usize is OK and should not warn
|
|
|
|
let _ = abc as usize;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fn_with_fn_args(f: fn(i32) -> i32) -> i32 {
|
|
|
|
f as i32
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: casting function pointer `f` to `i32`, which truncates the value
|
2018-10-03 05:02:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {}
|