rustc_mir_transform/
function_item_references.rs

1use itertools::Itertools;
2use rustc_abi::ExternAbi;
3use rustc_hir::def_id::DefId;
4use rustc_middle::mir::visit::Visitor;
5use rustc_middle::mir::*;
6use rustc_middle::ty::{self, EarlyBinder, GenericArgsRef, Ty, TyCtxt};
7use rustc_session::lint::builtin::FUNCTION_ITEM_REFERENCES;
8use rustc_span::source_map::Spanned;
9use rustc_span::{Span, sym};
10
11use crate::errors;
12
13pub(super) struct FunctionItemReferences;
14
15impl<'tcx> crate::MirLint<'tcx> for FunctionItemReferences {
16    fn run_lint(&self, tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
17        let mut checker = FunctionItemRefChecker { tcx, body };
18        checker.visit_body(body);
19    }
20}
21
22struct FunctionItemRefChecker<'a, 'tcx> {
23    tcx: TyCtxt<'tcx>,
24    body: &'a Body<'tcx>,
25}
26
27impl<'tcx> Visitor<'tcx> for FunctionItemRefChecker<'_, 'tcx> {
28    /// Emits a lint for function reference arguments bound by `fmt::Pointer` or passed to
29    /// `transmute`. This only handles arguments in calls outside macro expansions to avoid double
30    /// counting function references formatted as pointers by macros.
31    fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
32        if let TerminatorKind::Call {
33            func,
34            args,
35            destination: _,
36            target: _,
37            unwind: _,
38            call_source: _,
39            fn_span: _,
40        } = &terminator.kind
41        {
42            let source_info = *self.body.source_info(location);
43            let func_ty = func.ty(self.body, self.tcx);
44            if let ty::FnDef(def_id, args_ref) = *func_ty.kind() {
45                // Handle calls to `transmute`
46                if self.tcx.is_diagnostic_item(sym::transmute, def_id) {
47                    let arg_ty = args[0].node.ty(self.body, self.tcx);
48                    for inner_ty in arg_ty.walk().filter_map(|arg| arg.as_type()) {
49                        if let Some((fn_id, fn_args)) = FunctionItemRefChecker::is_fn_ref(inner_ty)
50                        {
51                            let span = self.nth_arg_span(args, 0);
52                            self.emit_lint(fn_id, fn_args, source_info, span);
53                        }
54                    }
55                } else {
56                    self.check_bound_args(def_id, args_ref, args, source_info);
57                }
58            }
59        }
60        self.super_terminator(terminator, location);
61    }
62}
63
64impl<'tcx> FunctionItemRefChecker<'_, 'tcx> {
65    /// Emits a lint for function reference arguments bound by `fmt::Pointer` in calls to the
66    /// function defined by `def_id` with the generic parameters `args_ref`.
67    fn check_bound_args(
68        &self,
69        def_id: DefId,
70        args_ref: GenericArgsRef<'tcx>,
71        args: &[Spanned<Operand<'tcx>>],
72        source_info: SourceInfo,
73    ) {
74        let param_env = self.tcx.param_env(def_id);
75        let bounds = param_env.caller_bounds();
76        for bound in bounds {
77            if let Some(bound_ty) = self.is_pointer_trait(bound) {
78                // Get the argument types as they appear in the function signature.
79                let arg_defs =
80                    self.tcx.fn_sig(def_id).instantiate_identity().skip_binder().inputs();
81                for (arg_num, arg_def) in arg_defs.iter().enumerate() {
82                    // For all types reachable from the argument type in the fn sig
83                    for inner_ty in arg_def.walk().filter_map(|arg| arg.as_type()) {
84                        // If the inner type matches the type bound by `Pointer`
85                        if inner_ty == bound_ty {
86                            // Do an instantiation using the parameters from the callsite
87                            let instantiated_ty =
88                                EarlyBinder::bind(inner_ty).instantiate(self.tcx, args_ref);
89                            if let Some((fn_id, fn_args)) =
90                                FunctionItemRefChecker::is_fn_ref(instantiated_ty)
91                            {
92                                let mut span = self.nth_arg_span(args, arg_num);
93                                if span.from_expansion() {
94                                    // The operand's ctxt wouldn't display the lint since it's
95                                    // inside a macro so we have to use the callsite's ctxt.
96                                    let callsite_ctxt = span.source_callsite().ctxt();
97                                    span = span.with_ctxt(callsite_ctxt);
98                                }
99                                self.emit_lint(fn_id, fn_args, source_info, span);
100                            }
101                        }
102                    }
103                }
104            }
105        }
106    }
107
108    /// If the given predicate is the trait `fmt::Pointer`, returns the bound parameter type.
109    fn is_pointer_trait(&self, bound: ty::Clause<'tcx>) -> Option<Ty<'tcx>> {
110        if let ty::ClauseKind::Trait(predicate) = bound.kind().skip_binder() {
111            self.tcx
112                .is_diagnostic_item(sym::Pointer, predicate.def_id())
113                .then(|| predicate.trait_ref.self_ty())
114        } else {
115            None
116        }
117    }
118
119    /// If a type is a reference or raw pointer to the anonymous type of a function definition,
120    /// returns that function's `DefId` and `GenericArgsRef`.
121    fn is_fn_ref(ty: Ty<'tcx>) -> Option<(DefId, GenericArgsRef<'tcx>)> {
122        let referent_ty = match ty.kind() {
123            ty::Ref(_, referent_ty, _) => Some(referent_ty),
124            ty::RawPtr(referent_ty, _) => Some(referent_ty),
125            _ => None,
126        };
127        referent_ty
128            .map(|ref_ty| {
129                if let ty::FnDef(def_id, args_ref) = *ref_ty.kind() {
130                    Some((def_id, args_ref))
131                } else {
132                    None
133                }
134            })
135            .unwrap_or(None)
136    }
137
138    fn nth_arg_span(&self, args: &[Spanned<Operand<'tcx>>], n: usize) -> Span {
139        args[n].node.span(&self.body.local_decls)
140    }
141
142    fn emit_lint(
143        &self,
144        fn_id: DefId,
145        fn_args: GenericArgsRef<'tcx>,
146        source_info: SourceInfo,
147        span: Span,
148    ) {
149        let lint_root = self.body.source_scopes[source_info.scope]
150            .local_data
151            .as_ref()
152            .unwrap_crate_local()
153            .lint_root;
154        // FIXME: use existing printing routines to print the function signature
155        let fn_sig = self.tcx.fn_sig(fn_id).instantiate(self.tcx, fn_args);
156        let unsafety = fn_sig.safety().prefix_str();
157        let abi = match fn_sig.abi() {
158            ExternAbi::Rust => String::from(""),
159            other_abi => format!("extern {other_abi} "),
160        };
161        let ident = self.tcx.item_ident(fn_id);
162        let ty_params = fn_args.types().map(|ty| format!("{ty}"));
163        let const_params = fn_args.consts().map(|c| format!("{c}"));
164        let params = ty_params.chain(const_params).join(", ");
165        let num_args = fn_sig.inputs().map_bound(|inputs| inputs.len()).skip_binder();
166        let variadic = if fn_sig.c_variadic() { ", ..." } else { "" };
167        let ret = if fn_sig.output().skip_binder().is_unit() { "" } else { " -> _" };
168        let sugg = format!(
169            "{} as {}{}fn({}{}){}",
170            if params.is_empty() { ident.to_string() } else { format!("{ident}::<{params}>") },
171            unsafety,
172            abi,
173            vec!["_"; num_args].join(", "),
174            variadic,
175            ret,
176        );
177
178        self.tcx.emit_node_span_lint(
179            FUNCTION_ITEM_REFERENCES,
180            lint_root,
181            span,
182            errors::FnItemRef { span, sugg, ident },
183        );
184    }
185}