Add lint on cast Fn to numerical.
This commit is contained in:
parent
fc008aa14c
commit
cf8f379657
3 changed files with 36 additions and 0 deletions
|
@ -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;
|
||||
|
|
10
tests/ui/types_fn_to_int.rs
Normal file
10
tests/ui/types_fn_to_int.rs
Normal file
|
@ -0,0 +1,10 @@
|
|||
#![feature(tool_attributes)]
|
||||
enum Foo {
|
||||
A(usize),
|
||||
B
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let x = Foo::A;
|
||||
let y = x as i32;
|
||||
}
|
10
tests/ui/types_fn_to_int.stderr
Normal file
10
tests/ui/types_fn_to_int.stderr
Normal 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
|
||||
|
Loading…
Reference in a new issue