diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ce63c0a1574..d140531c6e8b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1501,6 +1501,7 @@ Released 2018-09-13 [`fn_address_comparisons`]: https://rust-lang.github.io/rust-clippy/master/index.html#fn_address_comparisons [`fn_params_excessive_bools`]: https://rust-lang.github.io/rust-clippy/master/index.html#fn_params_excessive_bools [`fn_to_numeric_cast`]: https://rust-lang.github.io/rust-clippy/master/index.html#fn_to_numeric_cast +[`fn_to_numeric_cast_usize`]: https://rust-lang.github.io/rust-clippy/master/index.html#fn_to_numeric_cast_usize [`fn_to_numeric_cast_with_truncation`]: https://rust-lang.github.io/rust-clippy/master/index.html#fn_to_numeric_cast_with_truncation [`for_kv_map`]: https://rust-lang.github.io/rust-clippy/master/index.html#for_kv_map [`for_loops_over_fallibles`]: https://rust-lang.github.io/rust-clippy/master/index.html#for_loops_over_fallibles diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 87c297e72eb0..3d0111ac2347 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -822,6 +822,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: &types::CAST_SIGN_LOSS, &types::CHAR_LIT_AS_U8, &types::FN_TO_NUMERIC_CAST, + &types::FN_TO_NUMERIC_CAST_USIZE, &types::FN_TO_NUMERIC_CAST_WITH_TRUNCATION, &types::IMPLICIT_HASHER, &types::INVALID_UPCAST_COMPARISONS, @@ -1457,6 +1458,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: LintId::of(&types::CAST_REF_TO_MUT), LintId::of(&types::CHAR_LIT_AS_U8), LintId::of(&types::FN_TO_NUMERIC_CAST), + LintId::of(&types::FN_TO_NUMERIC_CAST_USIZE), LintId::of(&types::FN_TO_NUMERIC_CAST_WITH_TRUNCATION), LintId::of(&types::REDUNDANT_ALLOCATION), LintId::of(&types::TYPE_COMPLEXITY), @@ -1575,6 +1577,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: LintId::of(&to_digit_is_some::TO_DIGIT_IS_SOME), LintId::of(&try_err::TRY_ERR), LintId::of(&types::FN_TO_NUMERIC_CAST), + LintId::of(&types::FN_TO_NUMERIC_CAST_USIZE), LintId::of(&types::FN_TO_NUMERIC_CAST_WITH_TRUNCATION), LintId::of(&unsafe_removed_from_name::UNSAFE_REMOVED_FROM_NAME), LintId::of(&unused_unit::UNUSED_UNIT), diff --git a/clippy_lints/src/transmute.rs b/clippy_lints/src/transmute.rs index 7b5e92eb5ee1..5f8a3f397476 100644 --- a/clippy_lints/src/transmute.rs +++ b/clippy_lints/src/transmute.rs @@ -630,6 +630,25 @@ impl<'tcx> LateLintPass<'tcx> for Transmute { ); } }, + (ty::FnDef(..), ty::Int(_) | ty::Uint(_)) => span_lint_and_then( + cx, + TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS, + e.span, + &format!( + "transmute from `{}` to `{}` which could be expressed as a pointer cast instead", + from_ty, + to_ty + ), + |diag| { + if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) { + let sugg = arg + .as_ty(&from_ty.fn_sig(cx.tcx).skip_binder().to_string()) + .as_ty(&to_ty.to_string()) + .to_string(); + diag.span_suggestion(e.span, "try", sugg, Applicability::MachineApplicable); + } + } + ), (_, _) if can_be_expressed_as_pointer_cast(cx, e, from_ty, to_ty) => span_lint_and_then( cx, TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS, diff --git a/clippy_lints/src/types.rs b/clippy_lints/src/types.rs index e0204273197f..29b9fd8fe6ba 100644 --- a/clippy_lints/src/types.rs +++ b/clippy_lints/src/types.rs @@ -1100,6 +1100,31 @@ declare_clippy_lint! { "casting a function pointer to a numeric type other than usize" } +declare_clippy_lint! { + /// **What it does:** Checks for casts of function directly to a usize + /// + /// **Why is this bad?** + /// Casting a function directly to a usize is likely a mistake. Most likely, the intended behavior + /// is calling the function. If casting to a usize is intended, it would be more clearly expressed + /// by casting to a function pointer, then to a usize. + /// + /// **Example** + /// + /// ```rust + /// // Bad + /// fn fun() -> usize { 1 } + /// let a: usize = fun as usize; + /// + /// // Good + /// fn fun2() -> usize { 1 } + /// let a: usize = fun2 as fn() -> usize as usize; + /// let b: usize = fun2(); + /// ``` + pub FN_TO_NUMERIC_CAST_USIZE, + style, + "casting a function to a usize" +} + declare_clippy_lint! { /// **What it does:** Checks for casts of a function pointer to a numeric type not wide enough to /// store address. @@ -1374,6 +1399,7 @@ declare_lint_pass!(Casts => [ UNNECESSARY_CAST, CAST_PTR_ALIGNMENT, FN_TO_NUMERIC_CAST, + FN_TO_NUMERIC_CAST_USIZE, FN_TO_NUMERIC_CAST_WITH_TRUNCATION, ]); @@ -1558,15 +1584,55 @@ fn lint_fn_to_numeric_cast( ) { // We only want to check casts to `ty::Uint` or `ty::Int` match cast_to.kind { - ty::Uint(_) | ty::Int(..) => { /* continue on */ }, + ty::Uint(_) | ty::Int(_) => { /* continue on */ }, _ => return, } - match cast_from.kind { - ty::FnDef(..) | ty::FnPtr(_) => { - let mut applicability = Applicability::MaybeIncorrect; - let from_snippet = snippet_with_applicability(cx, cast_expr.span, "x", &mut applicability); - let to_nbits = int_ty_to_nbits(cast_to, cx.tcx); + let is_def = matches!(cast_from.kind, ty::FnDef(..)); + let is_ptr = matches!(cast_from.kind, ty::FnPtr(_)); + + if is_def || is_ptr { + let mut applicability = Applicability::MaybeIncorrect; + let from_snippet = snippet_with_applicability(cx, cast_expr.span, "x", &mut applicability); + + let to_nbits = int_ty_to_nbits(cast_to, cx.tcx); + + if is_def { + if to_nbits < cx.tcx.data_layout.pointer_size.bits() { + span_lint_and_sugg( + cx, + FN_TO_NUMERIC_CAST_WITH_TRUNCATION, + expr.span, + &format!( + "casting *pointer* to function `{}` as `{}`, which truncates the value", + from_snippet, cast_to + ), + "try", + format!("{}() as {}", from_snippet, cast_to), + applicability, + ); + } else if cast_to.kind == ty::Uint(UintTy::Usize) { + span_lint_and_sugg( + cx, + FN_TO_NUMERIC_CAST_USIZE, + expr.span, + &format!("casting *pointer* to function `{}` as `{}`", from_snippet, cast_to), + "try", + format!("{}() as {}", from_snippet, cast_to), + applicability, + ); + } else { + span_lint_and_sugg( + cx, + FN_TO_NUMERIC_CAST, + expr.span, + &format!("casting *pointer* to function `{}` as `{}`", from_snippet, cast_to), + "try", + format!("{}() as {}", from_snippet, cast_to), + applicability, + ); + } + } else if is_ptr { if to_nbits < cx.tcx.data_layout.pointer_size.bits() { span_lint_and_sugg( cx, @@ -1591,8 +1657,7 @@ fn lint_fn_to_numeric_cast( applicability, ); } - }, - _ => {}, + } } } diff --git a/src/lintlist/mod.rs b/src/lintlist/mod.rs index bf58c117aaaa..6aad11927ef6 100644 --- a/src/lintlist/mod.rs +++ b/src/lintlist/mod.rs @@ -682,6 +682,13 @@ pub static ref ALL_LINTS: Vec = vec![ deprecation: None, module: "types", }, + Lint { + name: "fn_to_numeric_cast_usize", + group: "style", + desc: "casting a function to a usize", + deprecation: None, + module: "types", + }, Lint { name: "fn_to_numeric_cast_with_truncation", group: "style", diff --git a/tests/ui/fn_to_numeric_cast.rs b/tests/ui/fn_to_numeric_cast.rs index a456c085c876..e92b8947e580 100644 --- a/tests/ui/fn_to_numeric_cast.rs +++ b/tests/ui/fn_to_numeric_cast.rs @@ -1,6 +1,10 @@ // ignore-32bit -#![warn(clippy::fn_to_numeric_cast, clippy::fn_to_numeric_cast_with_truncation)] +#![warn( + clippy::fn_to_numeric_cast, + clippy::fn_to_numeric_cast_usize, + clippy::fn_to_numeric_cast_with_truncation +)] fn foo() -> String { String::new() @@ -19,10 +23,11 @@ fn test_function_to_numeric_cast() { let _ = foo as u32; let _ = foo as u64; let _ = foo as u128; - - // Casting to usize is OK and should not warn let _ = foo as usize; + // Casting to fn pointer is OK and should not warn + let _ = foo as fn() -> String; + // Cast `f` (a `FnDef`) to `fn()` should not warn fn f() {} let _ = f as fn(); diff --git a/tests/ui/fn_to_numeric_cast.stderr b/tests/ui/fn_to_numeric_cast.stderr index e9549e157cd9..a42e7ec236c4 100644 --- a/tests/ui/fn_to_numeric_cast.stderr +++ b/tests/ui/fn_to_numeric_cast.stderr @@ -1,144 +1,152 @@ -error: casting function pointer `foo` to `i8`, which truncates the value - --> $DIR/fn_to_numeric_cast.rs:10:13 +error: casting *pointer* to function `foo` as `i8`, which truncates the value + --> $DIR/fn_to_numeric_cast.rs:14:13 | LL | let _ = foo as i8; - | ^^^^^^^^^ help: try: `foo as usize` + | ^^^^^^^^^ help: try: `foo() as i8` | = note: `-D clippy::fn-to-numeric-cast-with-truncation` implied by `-D warnings` -error: casting function pointer `foo` to `i16`, which truncates the value - --> $DIR/fn_to_numeric_cast.rs:11:13 +error: casting *pointer* to function `foo` as `i16`, which truncates the value + --> $DIR/fn_to_numeric_cast.rs:15:13 | LL | let _ = foo as i16; - | ^^^^^^^^^^ help: try: `foo as usize` + | ^^^^^^^^^^ help: try: `foo() as i16` -error: casting function pointer `foo` to `i32`, which truncates the value - --> $DIR/fn_to_numeric_cast.rs:12:13 +error: casting *pointer* to function `foo` as `i32`, which truncates the value + --> $DIR/fn_to_numeric_cast.rs:16:13 | LL | let _ = foo as i32; - | ^^^^^^^^^^ help: try: `foo as usize` + | ^^^^^^^^^^ help: try: `foo() as i32` -error: casting function pointer `foo` to `i64` - --> $DIR/fn_to_numeric_cast.rs:13:13 +error: casting *pointer* to function `foo` as `i64` + --> $DIR/fn_to_numeric_cast.rs:17:13 | LL | let _ = foo as i64; - | ^^^^^^^^^^ help: try: `foo as usize` + | ^^^^^^^^^^ help: try: `foo() as i64` | = note: `-D clippy::fn-to-numeric-cast` implied by `-D warnings` -error: casting function pointer `foo` to `i128` - --> $DIR/fn_to_numeric_cast.rs:14:13 +error: casting *pointer* to function `foo` as `i128` + --> $DIR/fn_to_numeric_cast.rs:18:13 | LL | let _ = foo as i128; - | ^^^^^^^^^^^ help: try: `foo as usize` + | ^^^^^^^^^^^ help: try: `foo() as i128` -error: casting function pointer `foo` to `isize` - --> $DIR/fn_to_numeric_cast.rs:15:13 +error: casting *pointer* to function `foo` as `isize` + --> $DIR/fn_to_numeric_cast.rs:19:13 | LL | let _ = foo as isize; - | ^^^^^^^^^^^^ help: try: `foo as usize` + | ^^^^^^^^^^^^ help: try: `foo() as isize` -error: casting function pointer `foo` to `u8`, which truncates the value - --> $DIR/fn_to_numeric_cast.rs:17:13 +error: casting *pointer* to function `foo` as `u8`, which truncates the value + --> $DIR/fn_to_numeric_cast.rs:21:13 | LL | let _ = foo as u8; - | ^^^^^^^^^ help: try: `foo as usize` + | ^^^^^^^^^ help: try: `foo() as u8` -error: casting function pointer `foo` to `u16`, which truncates the value - --> $DIR/fn_to_numeric_cast.rs:18:13 +error: casting *pointer* to function `foo` as `u16`, which truncates the value + --> $DIR/fn_to_numeric_cast.rs:22:13 | LL | let _ = foo as u16; - | ^^^^^^^^^^ help: try: `foo as usize` + | ^^^^^^^^^^ help: try: `foo() as u16` -error: casting function pointer `foo` to `u32`, which truncates the value - --> $DIR/fn_to_numeric_cast.rs:19:13 +error: casting *pointer* to function `foo` as `u32`, which truncates the value + --> $DIR/fn_to_numeric_cast.rs:23:13 | LL | let _ = foo as u32; - | ^^^^^^^^^^ help: try: `foo as usize` + | ^^^^^^^^^^ help: try: `foo() as u32` -error: casting function pointer `foo` to `u64` - --> $DIR/fn_to_numeric_cast.rs:20:13 +error: casting *pointer* to function `foo` as `u64` + --> $DIR/fn_to_numeric_cast.rs:24:13 | LL | let _ = foo as u64; - | ^^^^^^^^^^ help: try: `foo as usize` + | ^^^^^^^^^^ help: try: `foo() as u64` -error: casting function pointer `foo` to `u128` - --> $DIR/fn_to_numeric_cast.rs:21:13 +error: casting *pointer* to function `foo` as `u128` + --> $DIR/fn_to_numeric_cast.rs:25:13 | LL | let _ = foo as u128; - | ^^^^^^^^^^^ help: try: `foo as usize` + | ^^^^^^^^^^^ help: try: `foo() as u128` + +error: casting *pointer* to function `foo` as `usize` + --> $DIR/fn_to_numeric_cast.rs:26:13 + | +LL | let _ = foo as usize; + | ^^^^^^^^^^^^ help: try: `foo() as usize` + | + = note: `-D clippy::fn-to-numeric-cast-usize` implied by `-D warnings` error: casting function pointer `abc` to `i8`, which truncates the value - --> $DIR/fn_to_numeric_cast.rs:34:13 + --> $DIR/fn_to_numeric_cast.rs:39:13 | LL | let _ = abc as i8; | ^^^^^^^^^ help: try: `abc as usize` error: casting function pointer `abc` to `i16`, which truncates the value - --> $DIR/fn_to_numeric_cast.rs:35:13 + --> $DIR/fn_to_numeric_cast.rs:40:13 | LL | let _ = abc as i16; | ^^^^^^^^^^ help: try: `abc as usize` error: casting function pointer `abc` to `i32`, which truncates the value - --> $DIR/fn_to_numeric_cast.rs:36:13 + --> $DIR/fn_to_numeric_cast.rs:41:13 | LL | let _ = abc as i32; | ^^^^^^^^^^ help: try: `abc as usize` error: casting function pointer `abc` to `i64` - --> $DIR/fn_to_numeric_cast.rs:37:13 + --> $DIR/fn_to_numeric_cast.rs:42:13 | LL | let _ = abc as i64; | ^^^^^^^^^^ help: try: `abc as usize` error: casting function pointer `abc` to `i128` - --> $DIR/fn_to_numeric_cast.rs:38:13 + --> $DIR/fn_to_numeric_cast.rs:43:13 | LL | let _ = abc as i128; | ^^^^^^^^^^^ help: try: `abc as usize` error: casting function pointer `abc` to `isize` - --> $DIR/fn_to_numeric_cast.rs:39:13 + --> $DIR/fn_to_numeric_cast.rs:44:13 | LL | let _ = abc as isize; | ^^^^^^^^^^^^ help: try: `abc as usize` error: casting function pointer `abc` to `u8`, which truncates the value - --> $DIR/fn_to_numeric_cast.rs:41:13 + --> $DIR/fn_to_numeric_cast.rs:46:13 | LL | let _ = abc as u8; | ^^^^^^^^^ help: try: `abc as usize` error: casting function pointer `abc` to `u16`, which truncates the value - --> $DIR/fn_to_numeric_cast.rs:42:13 + --> $DIR/fn_to_numeric_cast.rs:47:13 | LL | let _ = abc as u16; | ^^^^^^^^^^ help: try: `abc as usize` error: casting function pointer `abc` to `u32`, which truncates the value - --> $DIR/fn_to_numeric_cast.rs:43:13 + --> $DIR/fn_to_numeric_cast.rs:48:13 | LL | let _ = abc as u32; | ^^^^^^^^^^ help: try: `abc as usize` error: casting function pointer `abc` to `u64` - --> $DIR/fn_to_numeric_cast.rs:44:13 + --> $DIR/fn_to_numeric_cast.rs:49:13 | LL | let _ = abc as u64; | ^^^^^^^^^^ help: try: `abc as usize` error: casting function pointer `abc` to `u128` - --> $DIR/fn_to_numeric_cast.rs:45:13 + --> $DIR/fn_to_numeric_cast.rs:50:13 | LL | let _ = abc as u128; | ^^^^^^^^^^^ help: try: `abc as usize` error: casting function pointer `f` to `i32`, which truncates the value - --> $DIR/fn_to_numeric_cast.rs:52:5 + --> $DIR/fn_to_numeric_cast.rs:57:5 | LL | f as i32 | ^^^^^^^^ help: try: `f as usize` -error: aborting due to 23 previous errors +error: aborting due to 24 previous errors diff --git a/tests/ui/fn_to_numeric_cast_32bit.rs b/tests/ui/fn_to_numeric_cast_32bit.rs index 04ee985c0863..4e20eaf66f6b 100644 --- a/tests/ui/fn_to_numeric_cast_32bit.rs +++ b/tests/ui/fn_to_numeric_cast_32bit.rs @@ -1,6 +1,10 @@ // ignore-64bit -#![warn(clippy::fn_to_numeric_cast, clippy::fn_to_numeric_cast_with_truncation)] +#![warn( + clippy::fn_to_numeric_cast, + clippy::fn_to_numeric_cast_usize, + clippy::fn_to_numeric_cast_with_truncation +)] fn foo() -> String { String::new() @@ -19,10 +23,11 @@ fn test_function_to_numeric_cast() { let _ = foo as u32; let _ = foo as u64; let _ = foo as u128; - - // Casting to usize is OK and should not warn let _ = foo as usize; + // Casting to fn pointer is OK and should not warn + let _ = foo as fn() -> String; + // Cast `f` (a `FnDef`) to `fn()` should not warn fn f() {} let _ = f as fn(); diff --git a/tests/ui/fn_to_numeric_cast_32bit.stderr b/tests/ui/fn_to_numeric_cast_32bit.stderr index 08dd611d6752..601cb48ba934 100644 --- a/tests/ui/fn_to_numeric_cast_32bit.stderr +++ b/tests/ui/fn_to_numeric_cast_32bit.stderr @@ -1,144 +1,152 @@ -error: casting function pointer `foo` to `i8`, which truncates the value - --> $DIR/fn_to_numeric_cast_32bit.rs:10:13 +error: casting *pointer* to function `foo` as `i8`, which truncates the value + --> $DIR/fn_to_numeric_cast_32bit.rs:14:13 | LL | let _ = foo as i8; - | ^^^^^^^^^ help: try: `foo as usize` + | ^^^^^^^^^ help: try: `foo() as i8` | = note: `-D clippy::fn-to-numeric-cast-with-truncation` implied by `-D warnings` -error: casting function pointer `foo` to `i16`, which truncates the value - --> $DIR/fn_to_numeric_cast_32bit.rs:11:13 +error: casting *pointer* to function `foo` as `i16`, which truncates the value + --> $DIR/fn_to_numeric_cast_32bit.rs:15:13 | LL | let _ = foo as i16; - | ^^^^^^^^^^ help: try: `foo as usize` + | ^^^^^^^^^^ help: try: `foo() as i16` -error: casting function pointer `foo` to `i32` - --> $DIR/fn_to_numeric_cast_32bit.rs:12:13 +error: casting *pointer* to function `foo` as `i32`, which truncates the value + --> $DIR/fn_to_numeric_cast_32bit.rs:16:13 | LL | let _ = foo as i32; - | ^^^^^^^^^^ help: try: `foo as usize` - | - = note: `-D clippy::fn-to-numeric-cast` implied by `-D warnings` + | ^^^^^^^^^^ help: try: `foo() as i32` -error: casting function pointer `foo` to `i64` - --> $DIR/fn_to_numeric_cast_32bit.rs:13:13 +error: casting *pointer* to function `foo` as `i64` + --> $DIR/fn_to_numeric_cast_32bit.rs:17:13 | LL | let _ = foo as i64; - | ^^^^^^^^^^ help: try: `foo as usize` + | ^^^^^^^^^^ help: try: `foo() as i64` + | + = note: `-D clippy::fn-to-numeric-cast` implied by `-D warnings` -error: casting function pointer `foo` to `i128` - --> $DIR/fn_to_numeric_cast_32bit.rs:14:13 +error: casting *pointer* to function `foo` as `i128` + --> $DIR/fn_to_numeric_cast_32bit.rs:18:13 | LL | let _ = foo as i128; - | ^^^^^^^^^^^ help: try: `foo as usize` + | ^^^^^^^^^^^ help: try: `foo() as i128` -error: casting function pointer `foo` to `isize` - --> $DIR/fn_to_numeric_cast_32bit.rs:15:13 +error: casting *pointer* to function `foo` as `isize` + --> $DIR/fn_to_numeric_cast_32bit.rs:19:13 | LL | let _ = foo as isize; - | ^^^^^^^^^^^^ help: try: `foo as usize` + | ^^^^^^^^^^^^ help: try: `foo() as isize` -error: casting function pointer `foo` to `u8`, which truncates the value - --> $DIR/fn_to_numeric_cast_32bit.rs:17:13 +error: casting *pointer* to function `foo` as `u8`, which truncates the value + --> $DIR/fn_to_numeric_cast_32bit.rs:21:13 | LL | let _ = foo as u8; - | ^^^^^^^^^ help: try: `foo as usize` + | ^^^^^^^^^ help: try: `foo() as u8` -error: casting function pointer `foo` to `u16`, which truncates the value - --> $DIR/fn_to_numeric_cast_32bit.rs:18:13 +error: casting *pointer* to function `foo` as `u16`, which truncates the value + --> $DIR/fn_to_numeric_cast_32bit.rs:22:13 | LL | let _ = foo as u16; - | ^^^^^^^^^^ help: try: `foo as usize` + | ^^^^^^^^^^ help: try: `foo() as u16` -error: casting function pointer `foo` to `u32` - --> $DIR/fn_to_numeric_cast_32bit.rs:19:13 +error: casting *pointer* to function `foo` as `u32`, which truncates the value + --> $DIR/fn_to_numeric_cast_32bit.rs:23:13 | LL | let _ = foo as u32; - | ^^^^^^^^^^ help: try: `foo as usize` + | ^^^^^^^^^^ help: try: `foo() as u32` -error: casting function pointer `foo` to `u64` - --> $DIR/fn_to_numeric_cast_32bit.rs:20:13 +error: casting *pointer* to function `foo` as `u64` + --> $DIR/fn_to_numeric_cast_32bit.rs:24:13 | LL | let _ = foo as u64; - | ^^^^^^^^^^ help: try: `foo as usize` + | ^^^^^^^^^^ help: try: `foo() as u64` -error: casting function pointer `foo` to `u128` - --> $DIR/fn_to_numeric_cast_32bit.rs:21:13 +error: casting *pointer* to function `foo` as `u128` + --> $DIR/fn_to_numeric_cast_32bit.rs:25:13 | LL | let _ = foo as u128; - | ^^^^^^^^^^^ help: try: `foo as usize` + | ^^^^^^^^^^^ help: try: `foo() as u128` + +error: casting *pointer* to function `foo` as `usize` + --> $DIR/fn_to_numeric_cast_32bit.rs:26:13 + | +LL | let _ = foo as usize; + | ^^^^^^^^^^^^ help: try: `foo() as usize` + | + = note: `-D clippy::fn-to-numeric-cast-usize` implied by `-D warnings` error: casting function pointer `abc` to `i8`, which truncates the value - --> $DIR/fn_to_numeric_cast_32bit.rs:34:13 + --> $DIR/fn_to_numeric_cast_32bit.rs:39:13 | LL | let _ = abc as i8; | ^^^^^^^^^ help: try: `abc as usize` error: casting function pointer `abc` to `i16`, which truncates the value - --> $DIR/fn_to_numeric_cast_32bit.rs:35:13 + --> $DIR/fn_to_numeric_cast_32bit.rs:40:13 | LL | let _ = abc as i16; | ^^^^^^^^^^ help: try: `abc as usize` -error: casting function pointer `abc` to `i32` - --> $DIR/fn_to_numeric_cast_32bit.rs:36:13 +error: casting function pointer `abc` to `i32`, which truncates the value + --> $DIR/fn_to_numeric_cast_32bit.rs:41:13 | LL | let _ = abc as i32; | ^^^^^^^^^^ help: try: `abc as usize` error: casting function pointer `abc` to `i64` - --> $DIR/fn_to_numeric_cast_32bit.rs:37:13 + --> $DIR/fn_to_numeric_cast_32bit.rs:42:13 | LL | let _ = abc as i64; | ^^^^^^^^^^ help: try: `abc as usize` error: casting function pointer `abc` to `i128` - --> $DIR/fn_to_numeric_cast_32bit.rs:38:13 + --> $DIR/fn_to_numeric_cast_32bit.rs:43:13 | LL | let _ = abc as i128; | ^^^^^^^^^^^ help: try: `abc as usize` error: casting function pointer `abc` to `isize` - --> $DIR/fn_to_numeric_cast_32bit.rs:39:13 + --> $DIR/fn_to_numeric_cast_32bit.rs:44:13 | LL | let _ = abc as isize; | ^^^^^^^^^^^^ help: try: `abc as usize` error: casting function pointer `abc` to `u8`, which truncates the value - --> $DIR/fn_to_numeric_cast_32bit.rs:41:13 + --> $DIR/fn_to_numeric_cast_32bit.rs:46:13 | LL | let _ = abc as u8; | ^^^^^^^^^ help: try: `abc as usize` error: casting function pointer `abc` to `u16`, which truncates the value - --> $DIR/fn_to_numeric_cast_32bit.rs:42:13 + --> $DIR/fn_to_numeric_cast_32bit.rs:47:13 | LL | let _ = abc as u16; | ^^^^^^^^^^ help: try: `abc as usize` -error: casting function pointer `abc` to `u32` - --> $DIR/fn_to_numeric_cast_32bit.rs:43:13 +error: casting function pointer `abc` to `u32`, which truncates the value + --> $DIR/fn_to_numeric_cast_32bit.rs:48:13 | LL | let _ = abc as u32; | ^^^^^^^^^^ help: try: `abc as usize` error: casting function pointer `abc` to `u64` - --> $DIR/fn_to_numeric_cast_32bit.rs:44:13 + --> $DIR/fn_to_numeric_cast_32bit.rs:49:13 | LL | let _ = abc as u64; | ^^^^^^^^^^ help: try: `abc as usize` error: casting function pointer `abc` to `u128` - --> $DIR/fn_to_numeric_cast_32bit.rs:45:13 + --> $DIR/fn_to_numeric_cast_32bit.rs:50:13 | LL | let _ = abc as u128; | ^^^^^^^^^^^ help: try: `abc as usize` -error: casting function pointer `f` to `i32` - --> $DIR/fn_to_numeric_cast_32bit.rs:52:5 +error: casting function pointer `f` to `i32`, which truncates the value + --> $DIR/fn_to_numeric_cast_32bit.rs:57:5 | LL | f as i32 | ^^^^^^^^ help: try: `f as usize` -error: aborting due to 23 previous errors +error: aborting due to 24 previous errors diff --git a/tests/ui/transmutes_expressible_as_ptr_casts.fixed b/tests/ui/transmutes_expressible_as_ptr_casts.fixed index b6f1e83181cc..fb8668796f7a 100644 --- a/tests/ui/transmutes_expressible_as_ptr_casts.fixed +++ b/tests/ui/transmutes_expressible_as_ptr_casts.fixed @@ -50,7 +50,7 @@ fn main() { let _usize_ptr_transmute = foo as *const usize; // e is a function pointer type and U is an integer; fptr-addr-cast - let _usize_from_fn_ptr_transmute = unsafe { foo as usize }; + let _usize_from_fn_ptr_transmute = unsafe { foo as fn(usize) -> u8 as usize }; let _usize_from_fn_ptr = foo as *const usize; } diff --git a/tests/ui/transmutes_expressible_as_ptr_casts.stderr b/tests/ui/transmutes_expressible_as_ptr_casts.stderr index 1157b179317e..63e593e9e95f 100644 --- a/tests/ui/transmutes_expressible_as_ptr_casts.stderr +++ b/tests/ui/transmutes_expressible_as_ptr_casts.stderr @@ -44,7 +44,7 @@ error: transmute from `fn(usize) -> u8 {main::foo}` to `usize` which could be ex --> $DIR/transmutes_expressible_as_ptr_casts.rs:53:49 | LL | let _usize_from_fn_ptr_transmute = unsafe { transmute:: u8, usize>(foo) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `foo as usize` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `foo as fn(usize) -> u8 as usize` error: transmute from a reference to a pointer --> $DIR/transmutes_expressible_as_ptr_casts.rs:65:14