Skip to content

Commit c10101c

Browse files
committed
Don't lint trivially_copy_pass_by_ref when unsafe pointers are used
1 parent 2315f76 commit c10101c

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

clippy_lints/src/pass_by_ref_or_value.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rustc_hir as hir;
1414
use rustc_hir::intravisit::FnKind;
1515
use rustc_hir::{BindingAnnotation, Body, FnDecl, HirId, Impl, ItemKind, MutTy, Mutability, Node, PatKind};
1616
use rustc_lint::{LateContext, LateLintPass};
17+
use rustc_middle::ty::adjustment::{Adjust, PointerCast};
1718
use rustc_middle::ty::layout::LayoutOf;
1819
use rustc_middle::ty::{self, RegionKind};
1920
use rustc_session::{declare_tool_lint, impl_lint_pass};
@@ -185,6 +186,20 @@ impl<'tcx> PassByRefOrValue {
185186
&& size <= self.ref_min_size
186187
&& let hir::TyKind::Rptr(_, MutTy { ty: decl_ty, .. }) = input.kind
187188
{
189+
if let Some(typeck) = cx.maybe_typeck_results() {
190+
// Don't lint if an unsafe pointer is created.
191+
// TODO: Limit the check only to unsafe pointers to the argument (or part of the argument)
192+
// which escape the current function.
193+
if typeck.node_types().iter().any(|(_, &ty)| ty.is_unsafe_ptr())
194+
|| typeck
195+
.adjustments()
196+
.iter()
197+
.flat_map(|(_, a)| a)
198+
.any(|a| matches!(a.kind, Adjust::Pointer(PointerCast::UnsafeFnPointer)))
199+
{
200+
continue;
201+
}
202+
}
188203
let value_type = if fn_body.and_then(|body| body.params.get(index)).map_or(false, is_self) {
189204
"self".into()
190205
} else {

tests/ui/trivially_copy_pass_by_ref.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,18 @@ fn _unrelated_lifetimes<'a, 'b>(_x: &'a u32, y: &'b u32) -> &'b u32 {
139139
y
140140
}
141141

142+
fn _return_ptr(x: &u32) -> *const u32 {
143+
x
144+
}
145+
146+
fn _return_field_ptr(x: &(u32, u32)) -> *const u32 {
147+
&x.0
148+
}
149+
150+
fn _return_field_ptr_addr_of(x: &(u32, u32)) -> *const u32 {
151+
core::ptr::addr_of!(x.0)
152+
}
153+
142154
fn main() {
143155
let (mut foo, bar) = (Foo(0), Bar([0; 24]));
144156
let (mut a, b, c, x, y, z) = (0, 0, Bar([0; 24]), 0, Foo(0), 0);

0 commit comments

Comments
 (0)