added a lint against function references

this lint suggests casting function references to `*const ()`
This commit is contained in:
Ayrton 2020-08-19 00:30:49 -04:00
parent 56d288fa46
commit dd4d4e29c3
2 changed files with 33 additions and 0 deletions

View file

@ -2942,3 +2942,35 @@ impl<'tcx> LateLintPass<'tcx> for ClashingExternDeclarations {
}
}
}
declare_lint! {
FUNCTION_REFERENCES,
Warn,
"suggest casting functions to pointers when attempting to take references"
}
declare_lint_pass!(FunctionReferences => [FUNCTION_REFERENCES]);
impl<'tcx> LateLintPass<'tcx> for FunctionReferences {
fn check_expr(&mut self, cx: &LateContext<'_>, e: &hir::Expr<'_>) {
if let hir::ExprKind::AddrOf(hir::BorrowKind::Ref, _, referent) = e.kind {
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() {
if let Some(ident) = node.ident() {
cx.struct_span_lint(FUNCTION_REFERENCES, referent.span, |lint| {
lint.build(&format!(
"cast {} with `as *const ()` to use it as a pointer",
ident.to_string()
))
.emit()
});
}
}
});
}
}
}
}
}

View file

@ -194,6 +194,7 @@ macro_rules! late_lint_mod_passes {
UnreachablePub: UnreachablePub,
ExplicitOutlivesRequirements: ExplicitOutlivesRequirements,
InvalidValue: InvalidValue,
FunctionReferences: FunctionReferences,
]
);
};