Skip to content

Commit cd0b5fd

Browse files
Allow must_use on associated types
1 parent 3bc767e commit cd0b5fd

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

compiler/rustc_lint/src/unused.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,15 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
290290
.map(|inner| MustUsePath::Pinned(Box::new(inner)))
291291
}
292292
ty::Adt(def, _) => is_def_must_use(cx, def.did(), span),
293-
ty::Alias(ty::Opaque | ty::Projection, ty::AliasTy { def_id: def, .. }) => {
294-
elaborate(cx.tcx, cx.tcx.explicit_item_self_bounds(def).iter_identity_copied())
293+
ty::Alias(ty::Opaque | ty::Projection, ty::AliasTy { def_id, .. }) => {
294+
if let Some(def) = is_def_must_use(cx, def_id, span) {
295+
Some(def)
296+
} else {
297+
// Look at the bounds for a `#[must_use]` trait.
298+
elaborate(
299+
cx.tcx,
300+
cx.tcx.explicit_item_self_bounds(def_id).iter_identity_copied(),
301+
)
295302
// We only care about self bounds for the impl-trait
296303
.filter_only_self()
297304
.find_map(|(pred, _span)| {
@@ -307,6 +314,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
307314
}
308315
})
309316
.map(|inner| MustUsePath::Opaque(Box::new(inner)))
317+
}
310318
}
311319
ty::Dynamic(binders, _, _) => binders.iter().find_map(|predicate| {
312320
if let ty::ExistentialPredicate::Trait(ref trait_ref) = predicate.skip_binder()

compiler/rustc_passes/src/check_attr.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1560,6 +1560,16 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
15601560
return;
15611561
}
15621562

1563+
// `#[must_use]` can be applied to an associated type in a trait, so that it will
1564+
// warn on unused associated types in generic contexts.
1565+
if let Target::AssocTy = target
1566+
&& let parent_def_id = self.tcx.hir_get_parent_item(hir_id).def_id
1567+
&& let containing_item = self.tcx.hir_expect_item(parent_def_id)
1568+
&& let hir::ItemKind::Trait(..) = containing_item.kind
1569+
{
1570+
return;
1571+
}
1572+
15631573
let article = match target {
15641574
Target::ExternCrate
15651575
| Target::Enum

0 commit comments

Comments
 (0)