Add lint on cast Fn to numerical.

This commit is contained in:
Victor Korkin 2018-05-28 23:31:55 +07:00
parent fc008aa14c
commit cf8f379657
3 changed files with 36 additions and 0 deletions

View file

@ -975,6 +975,22 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CastPass {
},
}
}
match &cast_from.sty {
ty::TyFnDef(..) |
ty::TyFnPtr(..) => {
if cast_to.is_numeric() && cast_to.sty != ty::TyUint(UintTy::Usize){
span_lint(
cx,
UNNECESSARY_CAST,
expr.span,
"casting Fn not to usize may truncate the value",
);
}
}
_ => ()
}
if_chain!{
if let ty::TyRawPtr(from_ptr_ty) = &cast_from.sty;
if let ty::TyRawPtr(to_ptr_ty) = &cast_to.sty;

View file

@ -0,0 +1,10 @@
#![feature(tool_attributes)]
enum Foo {
A(usize),
B
}
fn main() {
let x = Foo::A;
let y = x as i32;
}

View file

@ -0,0 +1,10 @@
error: casting Fn not to usize may truncate the value
--> $DIR/types_fn_to_int.rs:9:13
|
9 | let y = x as i32;
| ^^^^^^^^
|
= note: `-D unnecessary-cast` implied by `-D warnings`
error: aborting due to previous error