Skip to content

Commit 66d91d8

Browse files
authored
Rollup merge of #102287 - compiler-errors:unused-must-use-also-supertrait, r=fee1-dead
Elaborate supertrait bounds when triggering `unused_must_use` on `impl Trait` Given `impl Trait`, if one of its supertraits has a `#[must_use]`, then trigger the lint. This means that, for example, `-> impl ExactSizeIterator` also triggers the `must_use` on `trait Iterator`, which fixes #102183. This might need `@rust-lang/lang` sign-off, since it changes the behavior of the lint, so cc'ing them.
2 parents b1ab3b7 + 8509819 commit 66d91d8

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

Diff for: compiler/rustc_lint/src/unused.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_errors::{fluent, pluralize, Applicability, MultiSpan};
77
use rustc_hir as hir;
88
use rustc_hir::def::{DefKind, Res};
99
use rustc_hir::def_id::DefId;
10+
use rustc_infer::traits::util::elaborate_predicates_with_span;
1011
use rustc_middle::ty::adjustment;
1112
use rustc_middle::ty::{self, Ty};
1213
use rustc_span::symbol::Symbol;
@@ -204,10 +205,13 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
204205
ty::Adt(def, _) => check_must_use_def(cx, def.did(), span, descr_pre, descr_post),
205206
ty::Opaque(def, _) => {
206207
let mut has_emitted = false;
207-
for &(predicate, _) in cx.tcx.explicit_item_bounds(def) {
208+
for obligation in elaborate_predicates_with_span(
209+
cx.tcx,
210+
cx.tcx.explicit_item_bounds(def).iter().cloned(),
211+
) {
208212
// We only look at the `DefId`, so it is safe to skip the binder here.
209213
if let ty::PredicateKind::Trait(ref poly_trait_predicate) =
210-
predicate.kind().skip_binder()
214+
obligation.predicate.kind().skip_binder()
211215
{
212216
let def_id = poly_trait_predicate.trait_ref.def_id;
213217
let descr_pre =

Diff for: src/test/ui/lint/unused/unused-supertrait.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![deny(unused_must_use)]
2+
3+
fn it() -> impl ExactSizeIterator<Item = ()> {
4+
let x: Box<dyn ExactSizeIterator<Item = ()>> = todo!();
5+
x
6+
}
7+
8+
fn main() {
9+
it();
10+
//~^ ERROR unused implementer of `Iterator` that must be used
11+
}

Diff for: src/test/ui/lint/unused/unused-supertrait.stderr

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: unused implementer of `Iterator` that must be used
2+
--> $DIR/unused-supertrait.rs:9:5
3+
|
4+
LL | it();
5+
| ^^^^^
6+
|
7+
= note: iterators are lazy and do nothing unless consumed
8+
note: the lint level is defined here
9+
--> $DIR/unused-supertrait.rs:1:9
10+
|
11+
LL | #![deny(unused_must_use)]
12+
| ^^^^^^^^^^^^^^^
13+
14+
error: aborting due to previous error
15+

0 commit comments

Comments
 (0)