about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAyrton <a.munoz3327@gmail.com>2020-08-19 00:30:49 -0400
committerAyrton <a.munoz3327@gmail.com>2020-10-27 11:04:03 -0400
commitdd4d4e29c3295ed8e371f1dc5d1d463f56999ecb (patch)
tree375d4c78841b08417f2e2993460cf91e60db8fe4
parent56d288fa46e04cd5faf53d369a1a640a97e2bb08 (diff)
downloadrust-dd4d4e29c3295ed8e371f1dc5d1d463f56999ecb.tar.gz
rust-dd4d4e29c3295ed8e371f1dc5d1d463f56999ecb.zip
added a lint against function references
this lint suggests casting function references to `*const ()`
-rw-r--r--compiler/rustc_lint/src/builtin.rs32
-rw-r--r--compiler/rustc_lint/src/lib.rs1
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,
             ]
         );
     };