Skip to content

Commit

Permalink
Auto merge of #8369 - Jarcho:ptr_arg_8366, r=flip1995
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Jan 31, 2022
2 parents 0ed8ca4 + 66bb726 commit 7bb69c0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
20 changes: 14 additions & 6 deletions clippy_lints/src/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ impl<'tcx> LateLintPass<'tcx> for Ptr {
cx.tcx.fn_sig(item.def_id).skip_binder().inputs(),
sig.decl.inputs,
&[],
) {
)
.filter(|arg| arg.mutability() == Mutability::Not)
{
span_lint_and_sugg(
cx,
PTR_ARG,
Expand All @@ -170,10 +172,10 @@ impl<'tcx> LateLintPass<'tcx> for Ptr {
fn check_body(&mut self, cx: &LateContext<'tcx>, body: &'tcx Body<'_>) {
let hir = cx.tcx.hir();
let mut parents = hir.parent_iter(body.value.hir_id);
let (item_id, decl) = match parents.next() {
let (item_id, decl, is_trait_item) = match parents.next() {
Some((_, Node::Item(i))) => {
if let ItemKind::Fn(sig, ..) = &i.kind {
(i.def_id, sig.decl)
(i.def_id, sig.decl, false)
} else {
return;
}
Expand All @@ -185,14 +187,14 @@ impl<'tcx> LateLintPass<'tcx> for Ptr {
return;
}
if let ImplItemKind::Fn(sig, _) = &i.kind {
(i.def_id, sig.decl)
(i.def_id, sig.decl, false)
} else {
return;
}
},
Some((_, Node::TraitItem(i))) => {
if let TraitItemKind::Fn(sig, _) = &i.kind {
(i.def_id, sig.decl)
(i.def_id, sig.decl, true)
} else {
return;
}
Expand All @@ -202,7 +204,9 @@ impl<'tcx> LateLintPass<'tcx> for Ptr {

check_mut_from_ref(cx, decl);
let sig = cx.tcx.fn_sig(item_id).skip_binder();
let lint_args: Vec<_> = check_fn_args(cx, sig.inputs(), decl.inputs, body.params).collect();
let lint_args: Vec<_> = check_fn_args(cx, sig.inputs(), decl.inputs, body.params)
.filter(|arg| !is_trait_item || arg.mutability() == Mutability::Not)
.collect();
let results = check_ptr_arg_usage(cx, body, &lint_args);

for (result, args) in results.iter().zip(lint_args.iter()).filter(|(r, _)| !r.skip) {
Expand Down Expand Up @@ -318,6 +322,10 @@ impl PtrArg<'_> {
self.deref_ty.argless_str(),
)
}

fn mutability(&self) -> Mutability {
self.ref_prefix.mutability
}
}

struct RefPrefix {
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/ptr_arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,9 @@ fn dyn_fn_requires_vec(v: &Vec<u32>, f: &dyn Fn(&Vec<u32>)) {
// No error for types behind an alias (#7699)
type A = Vec<u8>;
fn aliased(a: &A) {}

// Issue #8366
pub trait Trait {
fn f(v: &mut Vec<i32>);
fn f2(v: &mut Vec<i32>) {}
}

0 comments on commit 7bb69c0

Please sign in to comment.