mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-22 12:43:36 +00:00
changed lint to suggest casting to the proper function type and added a test
This commit is contained in:
parent
dd4d4e29c3
commit
975547d475
@ -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()
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
22
src/test/ui/lint/function-references.rs
Normal file
22
src/test/ui/lint/function-references.rs
Normal file
@ -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
src/test/ui/lint/function-references.stderr
Normal file
22
src/test/ui/lint/function-references.stderr
Normal file
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user