diff options
| -rw-r--r-- | compiler/rustc_lint/src/builtin.rs | 32 | ||||
| -rw-r--r-- | compiler/rustc_lint/src/lib.rs | 1 |
2 files changed, 33 insertions, 0 deletions
diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index e5f66611d0f..b089d4dcfba 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -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() + }); + } + } + }); + } + } + } + } +} diff --git a/compiler/rustc_lint/src/lib.rs b/compiler/rustc_lint/src/lib.rs index 1db59bfc39d..b574a7da0aa 100644 --- a/compiler/rustc_lint/src/lib.rs +++ b/compiler/rustc_lint/src/lib.rs @@ -194,6 +194,7 @@ macro_rules! late_lint_mod_passes { UnreachablePub: UnreachablePub, ExplicitOutlivesRequirements: ExplicitOutlivesRequirements, InvalidValue: InvalidValue, + FunctionReferences: FunctionReferences, ] ); }; |
