Skip to content

Commit

Permalink
changed lint to suggest casting to the proper function type and added…
Browse files Browse the repository at this point in the history
… a test
  • Loading branch information
ayrtonm committed Oct 27, 2020
1 parent dd4d4e2 commit 975547d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
10 changes: 6 additions & 4 deletions compiler/rustc_lint/src/builtin.rs
Expand Up @@ -2957,17 +2957,19 @@ impl<'tcx> LateLintPass<'tcx> for FunctionReferences {
if let hir::ExprKind::Path(qpath) = &referent.kind {
if let Some(def_id) = cx.qpath_res(qpath, referent.hir_id).opt_def_id() {
cx.tcx.hir().get_if_local(def_id).map(|node| {
if node.fn_decl().is_some() {
node.fn_decl().map(|decl| {
if let Some(ident) = node.ident() {
cx.struct_span_lint(FUNCTION_REFERENCES, referent.span, |lint| {
let num_args = decl.inputs.len();
lint.build(&format!(
"cast {} with `as *const ()` to use it as a pointer",
ident.to_string()
"cast `{}` with `as *const fn({}) -> _` to use it as a pointer",
ident.to_string(),
vec!["_"; num_args].join(", ")
))
.emit()
});
}
}
});
});
}
}
Expand Down
22 changes: 22 additions & 0 deletions src/test/ui/lint/function-references.rs
@@ -0,0 +1,22 @@
// check-pass
fn foo() -> usize { 42 }
fn bar(x: usize) -> usize { x }
fn baz(x: usize, y: usize) -> usize { x + y }

fn main() {
println!("{:p}", &foo);
//~^ WARN cast `foo` with `as *const fn() -> _` to use it as a pointer
println!("{:p}", &bar);
//~^ WARN cast `bar` with `as *const fn(_) -> _` to use it as a pointer
println!("{:p}", &baz);
//~^ WARN cast `baz` with `as *const fn(_, _) -> _` to use it as a pointer

//should not produce any warnings
println!("{:p}", foo as *const fn() -> usize);
println!("{:p}", bar as *const fn(usize) -> usize);
println!("{:p}", baz as *const fn(usize, usize) -> usize);

//should not produce any warnings
let fn_thing = foo;
println!("{:p}", &fn_thing);
}
22 changes: 22 additions & 0 deletions src/test/ui/lint/function-references.stderr
@@ -0,0 +1,22 @@
warning: cast `foo` with `as *const fn() -> _` to use it as a pointer
--> $DIR/function-references.rs:7:23
|
LL | println!("{:p}", &foo);
| ^^^
|
= note: `#[warn(function_references)]` on by default

warning: cast `bar` with `as *const fn(_) -> _` to use it as a pointer
--> $DIR/function-references.rs:9:23
|
LL | println!("{:p}", &bar);
| ^^^

warning: cast `baz` with `as *const fn(_, _) -> _` to use it as a pointer
--> $DIR/function-references.rs:11:23
|
LL | println!("{:p}", &baz);
| ^^^

warning: 3 warnings emitted

0 comments on commit 975547d

Please sign in to comment.