Skip to content

Commit 7bb69c0

Browse files
committed
Auto merge of #8369 - Jarcho:ptr_arg_8366, r=flip1995
Don't lint `ptr_arg` for `&mut _` types in trait items fixes #8366 changelog: Don't lint `ptr_arg` for `&mut _` types in trait items
2 parents 0ed8ca4 + 66bb726 commit 7bb69c0

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

clippy_lints/src/ptr.rs

+14-6
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,9 @@ impl<'tcx> LateLintPass<'tcx> for Ptr {
153153
cx.tcx.fn_sig(item.def_id).skip_binder().inputs(),
154154
sig.decl.inputs,
155155
&[],
156-
) {
156+
)
157+
.filter(|arg| arg.mutability() == Mutability::Not)
158+
{
157159
span_lint_and_sugg(
158160
cx,
159161
PTR_ARG,
@@ -170,10 +172,10 @@ impl<'tcx> LateLintPass<'tcx> for Ptr {
170172
fn check_body(&mut self, cx: &LateContext<'tcx>, body: &'tcx Body<'_>) {
171173
let hir = cx.tcx.hir();
172174
let mut parents = hir.parent_iter(body.value.hir_id);
173-
let (item_id, decl) = match parents.next() {
175+
let (item_id, decl, is_trait_item) = match parents.next() {
174176
Some((_, Node::Item(i))) => {
175177
if let ItemKind::Fn(sig, ..) = &i.kind {
176-
(i.def_id, sig.decl)
178+
(i.def_id, sig.decl, false)
177179
} else {
178180
return;
179181
}
@@ -185,14 +187,14 @@ impl<'tcx> LateLintPass<'tcx> for Ptr {
185187
return;
186188
}
187189
if let ImplItemKind::Fn(sig, _) = &i.kind {
188-
(i.def_id, sig.decl)
190+
(i.def_id, sig.decl, false)
189191
} else {
190192
return;
191193
}
192194
},
193195
Some((_, Node::TraitItem(i))) => {
194196
if let TraitItemKind::Fn(sig, _) = &i.kind {
195-
(i.def_id, sig.decl)
197+
(i.def_id, sig.decl, true)
196198
} else {
197199
return;
198200
}
@@ -202,7 +204,9 @@ impl<'tcx> LateLintPass<'tcx> for Ptr {
202204

203205
check_mut_from_ref(cx, decl);
204206
let sig = cx.tcx.fn_sig(item_id).skip_binder();
205-
let lint_args: Vec<_> = check_fn_args(cx, sig.inputs(), decl.inputs, body.params).collect();
207+
let lint_args: Vec<_> = check_fn_args(cx, sig.inputs(), decl.inputs, body.params)
208+
.filter(|arg| !is_trait_item || arg.mutability() == Mutability::Not)
209+
.collect();
206210
let results = check_ptr_arg_usage(cx, body, &lint_args);
207211

208212
for (result, args) in results.iter().zip(lint_args.iter()).filter(|(r, _)| !r.skip) {
@@ -318,6 +322,10 @@ impl PtrArg<'_> {
318322
self.deref_ty.argless_str(),
319323
)
320324
}
325+
326+
fn mutability(&self) -> Mutability {
327+
self.ref_prefix.mutability
328+
}
321329
}
322330

323331
struct RefPrefix {

tests/ui/ptr_arg.rs

+6
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,9 @@ fn dyn_fn_requires_vec(v: &Vec<u32>, f: &dyn Fn(&Vec<u32>)) {
180180
// No error for types behind an alias (#7699)
181181
type A = Vec<u8>;
182182
fn aliased(a: &A) {}
183+
184+
// Issue #8366
185+
pub trait Trait {
186+
fn f(v: &mut Vec<i32>);
187+
fn f2(v: &mut Vec<i32>) {}
188+
}

0 commit comments

Comments
 (0)