Skip to content

Commit 0039c64

Browse files
authored
ptr_arg: clean-up a bit (#15507)
changelog: none
2 parents 743405d + e4d9449 commit 0039c64

File tree

1 file changed

+30
-31
lines changed

1 file changed

+30
-31
lines changed

clippy_lints/src/ptr.rs

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ impl<'tcx> LateLintPass<'tcx> for Ptr {
237237
.collect();
238238
let results = check_ptr_arg_usage(cx, body, &lint_args);
239239

240-
for (result, args) in results.iter().zip(lint_args.iter()).filter(|(r, _)| !r.skip) {
240+
for (result, args) in iter::zip(&results, &lint_args).filter(|(r, _)| !r.skip) {
241241
span_lint_hir_and_then(cx, PTR_ARG, args.emission_id, args.span, args.build_msg(), |diag| {
242242
diag.multipart_suggestion(
243243
"change this to",
@@ -386,7 +386,6 @@ impl<'tcx> DerefTy<'tcx> {
386386
}
387387
}
388388

389-
#[expect(clippy::too_many_lines)]
390389
fn check_fn_args<'cx, 'tcx: 'cx>(
391390
cx: &'cx LateContext<'tcx>,
392391
fn_sig: ty::FnSig<'tcx>,
@@ -413,13 +412,13 @@ fn check_fn_args<'cx, 'tcx: 'cx>(
413412
Some(sym::Vec) => (
414413
[(sym::clone, ".to_owned()")].as_slice(),
415414
DerefTy::Slice(
416-
name.args.and_then(|args| args.args.first()).and_then(|arg| {
417-
if let GenericArg::Type(ty) = arg {
418-
Some(ty.span)
419-
} else {
420-
None
421-
}
422-
}),
415+
if let Some(name_args) = name.args
416+
&& let [GenericArg::Type(ty), ..] = name_args.args
417+
{
418+
Some(ty.span)
419+
} else {
420+
None
421+
},
423422
args.type_at(0),
424423
),
425424
),
@@ -432,33 +431,29 @@ fn check_fn_args<'cx, 'tcx: 'cx>(
432431
DerefTy::Path,
433432
),
434433
Some(sym::Cow) if mutability == Mutability::Not => {
435-
if let Some((lifetime, ty)) = name.args.and_then(|args| {
436-
if let [GenericArg::Lifetime(lifetime), ty] = args.args {
437-
return Some((lifetime, ty));
438-
}
439-
None
440-
}) {
434+
if let Some(name_args) = name.args
435+
&& let [GenericArg::Lifetime(lifetime), ty] = name_args.args
436+
{
441437
if let LifetimeKind::Param(param_def_id) = lifetime.kind
442438
&& !lifetime.is_anonymous()
443439
&& fn_sig
444440
.output()
445441
.walk()
446-
.filter_map(|arg| {
447-
arg.as_region().and_then(|lifetime| match lifetime.kind() {
448-
ty::ReEarlyParam(r) => Some(
449-
cx.tcx
450-
.generics_of(cx.tcx.parent(param_def_id.to_def_id()))
451-
.region_param(r, cx.tcx)
452-
.def_id,
453-
),
454-
ty::ReBound(_, r) => r.kind.get_id(),
455-
ty::ReLateParam(r) => r.kind.get_id(),
456-
ty::ReStatic
457-
| ty::ReVar(_)
458-
| ty::RePlaceholder(_)
459-
| ty::ReErased
460-
| ty::ReError(_) => None,
461-
})
442+
.filter_map(ty::GenericArg::as_region)
443+
.filter_map(|lifetime| match lifetime.kind() {
444+
ty::ReEarlyParam(r) => Some(
445+
cx.tcx
446+
.generics_of(cx.tcx.parent(param_def_id.to_def_id()))
447+
.region_param(r, cx.tcx)
448+
.def_id,
449+
),
450+
ty::ReBound(_, r) => r.kind.get_id(),
451+
ty::ReLateParam(r) => r.kind.get_id(),
452+
ty::ReStatic
453+
| ty::ReVar(_)
454+
| ty::RePlaceholder(_)
455+
| ty::ReErased
456+
| ty::ReError(_) => None,
462457
})
463458
.any(|def_id| def_id.as_local().is_some_and(|def_id| def_id == param_def_id))
464459
{
@@ -627,12 +622,16 @@ fn check_ptr_arg_usage<'tcx>(cx: &LateContext<'tcx>, body: &Body<'tcx>, args: &[
627622
}
628623
}
629624

625+
// If the expression's type gets adjusted down to the deref type, we might as
626+
// well have started with that deref type -- the lint should fire
630627
let deref_ty = args.deref_ty.ty(self.cx);
631628
let adjusted_ty = self.cx.typeck_results().expr_ty_adjusted(e).peel_refs();
632629
if adjusted_ty == deref_ty {
633630
return;
634631
}
635632

633+
// If the expression's type is constrained by `dyn Trait`, see if the deref
634+
// type implements the trait(s) as well, and if so, the lint should fire
636635
if let ty::Dynamic(preds, ..) = adjusted_ty.kind()
637636
&& matches_preds(self.cx, deref_ty, preds)
638637
{

0 commit comments

Comments
 (0)