Added suggestion to function_item_references lint and fixed warning message

Also updated tests accordingly and tweaked some wording in the lint declaration.
This commit is contained in:
Ayrton 2020-10-27 09:00:19 -04:00
parent 935fc3642a
commit c791c64e84
4 changed files with 102 additions and 95 deletions

View file

@ -1,3 +1,4 @@
use rustc_errors::Applicability;
use rustc_hir::def_id::DefId;
use rustc_middle::mir::visit::Visitor;
use rustc_middle::mir::*;
@ -183,16 +184,22 @@ impl<'a, 'tcx> FunctionItemRefChecker<'a, 'tcx> {
let variadic = if fn_sig.c_variadic() { ", ..." } else { "" };
let ret = if fn_sig.output().skip_binder().is_unit() { "" } else { " -> _" };
self.tcx.struct_span_lint_hir(FUNCTION_ITEM_REFERENCES, lint_root, span, |lint| {
lint.build(&format!(
"cast `{}` with `as {}{}fn({}{}){}` to obtain a function pointer",
ident,
unsafety,
abi,
vec!["_"; num_args].join(", "),
variadic,
ret,
))
.emit();
lint.build("taking a reference to a function item does not give a function pointer")
.span_suggestion(
span,
&format!("cast `{}` to obtain a function pointer", ident),
format!(
"{} as {}{}fn({}{}){}",
ident,
unsafety,
abi,
vec!["_"; num_args].join(", "),
variadic,
ret,
),
Applicability::Unspecified,
)
.emit();
});
}
}