Files
rust/tests/ui/fn_to_numeric_cast.rs

79 lines
1.9 KiB
Rust
Raw Normal View History

2023-09-06 14:58:44 +00:00
//@stderr-per-bitwidth
//@no-rustfix
#![warn(clippy::fn_to_numeric_cast, clippy::fn_to_numeric_cast_with_truncation)]
2018-12-09 23:26:16 +01:00
fn foo() -> String {
String::new()
}
fn test_function_to_numeric_cast() {
let _ = foo as i8;
2025-02-11 17:57:08 +01:00
//~^ fn_to_numeric_cast_with_truncation
let _ = foo as i16;
2025-02-11 17:57:08 +01:00
//~^ fn_to_numeric_cast_with_truncation
let _ = foo as i32;
2025-02-11 17:57:08 +01:00
//~^ fn_to_numeric_cast_with_truncation
let _ = foo as i64;
2025-02-11 17:57:08 +01:00
//~^ fn_to_numeric_cast
let _ = foo as i128;
2025-02-11 17:57:08 +01:00
//~^ fn_to_numeric_cast
let _ = foo as isize;
2025-02-11 17:57:08 +01:00
//~^ fn_to_numeric_cast
let _ = foo as u8;
2025-02-11 17:57:08 +01:00
//~^ fn_to_numeric_cast_with_truncation
let _ = foo as u16;
2025-02-11 17:57:08 +01:00
//~^ fn_to_numeric_cast_with_truncation
let _ = foo as u32;
2025-02-11 17:57:08 +01:00
//~^ fn_to_numeric_cast_with_truncation
let _ = foo as u64;
2025-02-11 17:57:08 +01:00
//~^ fn_to_numeric_cast
let _ = foo as u128;
2025-02-11 17:57:08 +01:00
//~^ fn_to_numeric_cast
// Casting to usize is OK and should not warn
let _ = foo as usize;
// Cast `f` (a `FnDef`) to `fn()` should not warn
fn f() {}
let _ = f as fn();
}
fn test_function_var_to_numeric_cast() {
let abc: fn() -> String = foo;
let _ = abc as i8;
2025-02-11 17:57:08 +01:00
//~^ fn_to_numeric_cast_with_truncation
let _ = abc as i16;
2025-02-11 17:57:08 +01:00
//~^ fn_to_numeric_cast_with_truncation
let _ = abc as i32;
2025-02-11 17:57:08 +01:00
//~^ fn_to_numeric_cast_with_truncation
let _ = abc as i64;
2025-02-11 17:57:08 +01:00
//~^ fn_to_numeric_cast
let _ = abc as i128;
2025-02-11 17:57:08 +01:00
//~^ fn_to_numeric_cast
let _ = abc as isize;
2025-02-11 17:57:08 +01:00
//~^ fn_to_numeric_cast
let _ = abc as u8;
2025-02-11 17:57:08 +01:00
//~^ fn_to_numeric_cast_with_truncation
let _ = abc as u16;
2025-02-11 17:57:08 +01:00
//~^ fn_to_numeric_cast_with_truncation
let _ = abc as u32;
2025-02-11 17:57:08 +01:00
//~^ fn_to_numeric_cast_with_truncation
let _ = abc as u64;
2025-02-11 17:57:08 +01:00
//~^ fn_to_numeric_cast
let _ = abc as u128;
2025-02-11 17:57:08 +01:00
//~^ fn_to_numeric_cast
// 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
2025-02-11 17:57:08 +01:00
//~^ fn_to_numeric_cast_with_truncation
}
fn main() {}