Skip to content

Commit 00a7bfc

Browse files
Elaborate supertrait bounds when triggering unused_must_use on impl Trait
1 parent f3fafbb commit 00a7bfc

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

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;
@@ -206,10 +207,13 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
206207
ty::Adt(def, _) => check_must_use_def(cx, def.did(), span, descr_pre, descr_post),
207208
ty::Opaque(def, _) => {
208209
let mut has_emitted = false;
209-
for &(predicate, _) in cx.tcx.explicit_item_bounds(def) {
210+
for obligation in elaborate_predicates_with_span(
211+
cx.tcx,
212+
cx.tcx.explicit_item_bounds(def).iter().cloned(),
213+
) {
210214
// We only look at the `DefId`, so it is safe to skip the binder here.
211215
if let ty::PredicateKind::Trait(ref poly_trait_predicate) =
212-
predicate.kind().skip_binder()
216+
obligation.predicate.kind().skip_binder()
213217
{
214218
let def_id = poly_trait_predicate.trait_ref.def_id;
215219
let descr_pre =
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+
}
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: the lint level is defined here
8+
--> $DIR/unused-supertrait.rs:1:9
9+
|
10+
LL | #![deny(unused_must_use)]
11+
| ^^^^^^^^^^^^^^^
12+
= note: iterators are lazy and do nothing unless consumed
13+
14+
error: aborting due to previous error
15+

0 commit comments

Comments
 (0)