-
Notifications
You must be signed in to change notification settings - Fork 1.7k
WIP: Add lint on cast Fn to all numerical except usize. #2814
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
cf8f379
01be53f
f6e0388
e4b2a97
b69520f
e6811b9
ded2576
24ab207
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -679,6 +679,23 @@ 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. | ||
/// | ||
/// **Why is this bad?** Cast pointer not to usize truncate value. | ||
/// | ||
/// **Known problems:** None. | ||
/// | ||
/// **Example:** | ||
/// ```rust | ||
/// fn test_fn() -> i16; | ||
/// let _ = test_fn as i32 | ||
/// ``` | ||
declare_clippy_lint! { | ||
pub FN_TO_NUMERIC_CAST, | ||
correctness, | ||
"cast function pointer to the numeric type" | ||
} | ||
|
||
/// **What it does:** Checks for casts from a less-strictly-aligned pointer to a | ||
/// more-strictly-aligned pointer | ||
/// | ||
|
@@ -891,7 +908,8 @@ impl LintPass for CastPass { | |
CAST_POSSIBLE_WRAP, | ||
CAST_LOSSLESS, | ||
UNNECESSARY_CAST, | ||
CAST_PTR_ALIGNMENT | ||
CAST_PTR_ALIGNMENT, | ||
FN_TO_NUMERIC_CAST | ||
) | ||
} | ||
} | ||
|
@@ -975,6 +993,24 @@ 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_and_sugg( | ||
cx, | ||
FN_TO_NUMERIC_CAST, | ||
expr.span, | ||
&format!("casting a Fn to {} may truncate the function address value.", cast_to), | ||
"if you need address of function, use cast to `usize` instead:", | ||
format!("{} as usize", &snippet(cx, ex.span, "x")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks good! I think for the suggestion message |
||
); | ||
} | ||
} | ||
_ => () | ||
} | ||
|
||
if_chain!{ | ||
if let ty::TyRawPtr(from_ptr_ty) = &cast_from.sty; | ||
if let ty::TyRawPtr(to_ptr_ty) = &cast_to.sty; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
enum Foo { | ||
A(usize), | ||
B | ||
} | ||
|
||
fn main() { | ||
let x = Foo::A; | ||
let y = x as i32; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you also add a check for |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
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: aborting due to previous error | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What it does: "Checks for casts of a function pointer to a numeric type except `usize`."
Why is this bad? Casting a function pointer to something other than `usize` could truncate the address value.
This would be a little more precise, what this lint does.