Skip to content

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

Merged
merged 8 commits into from
Jun 5, 2018
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
types::UNIT_ARG,
types::UNIT_CMP,
types::UNNECESSARY_CAST,
types::FN_TO_NUMERIC_CAST,
unicode::ZERO_WIDTH_SPACE,
unsafe_removed_from_name::UNSAFE_REMOVED_FROM_NAME,
unused_io_amount::UNUSED_IO_AMOUNT,
Expand Down
38 changes: 37 additions & 1 deletion clippy_lints/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 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.
///
/// **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
///
Expand Down Expand Up @@ -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
)
}
}
Expand Down Expand Up @@ -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 `{}` to `{}` may truncate the function address value.", cast_from, cast_to),
"if you need the address of the function, consider",
format!("{} as usize", &snippet(cx, ex.span, "x"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good! I think for the suggestion message "if you need the address of the function, consider:" would be enough, since the usize hint is already in the suggestion.

);
}
}
_ => ()
}

if_chain!{
if let ty::TyRawPtr(from_ptr_ty) = &cast_from.sty;
if let ty::TyRawPtr(to_ptr_ty) = &cast_to.sty;
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/types_fn_to_int.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
enum Foo {
A(usize),
B
}

fn bar() -> i32 {
0i32
}

fn main() {
let x = Foo::A;
let y = x as i32;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add a check for Foo::A as i32 directly without an intermediate variable?


let z = bar as u32;
}
16 changes: 16 additions & 0 deletions tests/ui/types_fn_to_int.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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;
| ^^^^^^^^ help: if you need the address of the function, consider: `x as usize`
|
= note: #[deny(fn_to_numeric_cast)] on by default

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: `bar as usize`

error: aborting due to 2 previous errors