Fixes for suggestion message, tests and lint explanation.
This commit is contained in:
parent
e4b2a97401
commit
b69520f5fd
@ -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"))
|
||||
);
|
||||
}
|
||||
|
@ -12,7 +12,4 @@ fn main() {
|
||||
let y = x as i32;
|
||||
|
||||
let z = bar as u32;
|
||||
|
||||
//let c = || {0i32};
|
||||
//let ac = c as u32;
|
||||
}
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user