From b69520f5fd1309741445fe069edca9e4b1e6d48c Mon Sep 17 00:00:00 2001 From: Victor Korkin Date: Wed, 30 May 2018 11:48:46 +0700 Subject: [PATCH] Fixes for suggestion message, tests and lint explanation. --- clippy_lints/src/types.rs | 7 +++---- tests/ui/types_fn_to_int.rs | 3 --- tests/ui/types_fn_to_int.stderr | 34 +++++++++++++++++++++------------ 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/clippy_lints/src/types.rs b/clippy_lints/src/types.rs index 940a360ca18..fc65ebcc147 100644 --- a/clippy_lints/src/types.rs +++ b/clippy_lints/src/types.rs @@ -679,9 +679,9 @@ declare_clippy_lint! { "cast to the same type, e.g. `x as i32` where `x: i32`" } -/// **What it does:** Checks for casts function pointer to the numeric type. +/// **What it does:** Checks for casts of a function pointer to a numeric type except `usize`. /// -/// **Why is this bad?** Cast pointer not to usize truncate value. +/// **Why is this bad?** Casting a function pointer to something other than `usize` could truncate the address value. /// /// **Known problems:** None. /// @@ -1003,8 +1003,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CastPass { FN_TO_NUMERIC_CAST, expr.span, &format!("casting a `{}` to `{}` may truncate the function address value.", cast_from, cast_to), - // &format!("if you need address of function, use cast zz `usize`:"), - &format!("if you need the address of the function, z consider:"), + "if you need the address of the function, consider :", format!("{} as usize", &snippet(cx, ex.span, "x")) ); } diff --git a/tests/ui/types_fn_to_int.rs b/tests/ui/types_fn_to_int.rs index f37a82f0d71..0cce6579813 100644 --- a/tests/ui/types_fn_to_int.rs +++ b/tests/ui/types_fn_to_int.rs @@ -12,7 +12,4 @@ fn main() { let y = x as i32; let z = bar as u32; - - //let c = || {0i32}; - //let ac = c as u32; } diff --git a/tests/ui/types_fn_to_int.stderr b/tests/ui/types_fn_to_int.stderr index 8f0b2c64523..d5e8ad8bbb2 100644 --- a/tests/ui/types_fn_to_int.stderr +++ b/tests/ui/types_fn_to_int.stderr @@ -1,14 +1,24 @@ -error: casting a Fn to i32 may truncate the function address value. - --> $DIR/types_fn_to_int.rs:8:13 - | -8 | let y = x as i32; - | ^^^^^^^^ - | - = note: #[deny(fn_to_numeric_cast)] on by default -help: if you need address of function, use cast to `usize` instead: - | -8 | let y = x as usize; - | ^^^^^^^^^^ +error: casting a `fn(usize) -> Foo {Foo::A}` to `i32` may truncate the function address value. + --> $DIR/types_fn_to_int.rs:12:13 + | +12 | let y = x as i32; + | ^^^^^^^^ + | + = note: #[deny(fn_to_numeric_cast)] on by default +help: if you need the address of the function, consider : + | +12 | let y = x as usize; + | ^^^^^^^^^^ -error: aborting due to previous error +error: casting a `fn() -> i32 {bar}` to `u32` may truncate the function address value. + --> $DIR/types_fn_to_int.rs:14:13 + | +14 | let z = bar as u32; + | ^^^^^^^^^^ +help: if you need the address of the function, consider : + | +14 | let z = bar as usize; + | ^^^^^^^^^^^^ + +error: aborting due to 2 previous errors