]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/pass_by_ref_or_value.rs
Auto merge of #9148 - arieluy:then_some_unwrap_or, r=Jarcho
[rust.git] / clippy_lints / src / pass_by_ref_or_value.rs
index 6edf0e7a7b2ed9ac9d6662fe7d528f763f4383f3..5fa4fd74853f1d09ee164aa0846dbbe424190b6b 100644 (file)
@@ -14,6 +14,7 @@
 use rustc_hir::intravisit::FnKind;
 use rustc_hir::{BindingAnnotation, Body, FnDecl, HirId, Impl, ItemKind, MutTy, Mutability, Node, PatKind};
 use rustc_lint::{LateContext, LateLintPass};
+use rustc_middle::ty::adjustment::{Adjust, PointerCast};
 use rustc_middle::ty::layout::LayoutOf;
 use rustc_middle::ty::{self, RegionKind};
 use rustc_session::{declare_tool_lint, impl_lint_pass};
@@ -185,6 +186,20 @@ fn check_poly_fn(&mut self, cx: &LateContext<'tcx>, def_id: LocalDefId, decl: &F
                         && size <= self.ref_min_size
                         && let hir::TyKind::Rptr(_, MutTy { ty: decl_ty, .. }) = input.kind
                     {
+                        if let Some(typeck) = cx.maybe_typeck_results() {
+                            // Don't lint if an unsafe pointer is created.
+                            // TODO: Limit the check only to unsafe pointers to the argument (or part of the argument)
+                            //       which escape the current function.
+                            if typeck.node_types().iter().any(|(_, &ty)| ty.is_unsafe_ptr())
+                                || typeck
+                                    .adjustments()
+                                    .iter()
+                                    .flat_map(|(_, a)| a)
+                                    .any(|a| matches!(a.kind, Adjust::Pointer(PointerCast::UnsafeFnPointer)))
+                            {
+                                continue;
+                            }
+                        }
                         let value_type = if fn_body.and_then(|body| body.params.get(index)).map_or(false, is_self) {
                             "self".into()
                         } else {