Skip to content

Commit

Permalink
Elaborate supertrait bounds when triggering unused_must_use on impl T…
Browse files Browse the repository at this point in the history
…rait
  • Loading branch information
compiler-errors committed Sep 25, 2022
1 parent f3fafbb commit 00a7bfc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
8 changes: 6 additions & 2 deletions compiler/rustc_lint/src/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use rustc_errors::{fluent, pluralize, Applicability, MultiSpan};
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::DefId;
use rustc_infer::traits::util::elaborate_predicates_with_span;
use rustc_middle::ty::adjustment;
use rustc_middle::ty::{self, Ty};
use rustc_span::symbol::Symbol;
Expand Down Expand Up @@ -206,10 +207,13 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
ty::Adt(def, _) => check_must_use_def(cx, def.did(), span, descr_pre, descr_post),
ty::Opaque(def, _) => {
let mut has_emitted = false;
for &(predicate, _) in cx.tcx.explicit_item_bounds(def) {
for obligation in elaborate_predicates_with_span(
cx.tcx,
cx.tcx.explicit_item_bounds(def).iter().cloned(),
) {
// We only look at the `DefId`, so it is safe to skip the binder here.
if let ty::PredicateKind::Trait(ref poly_trait_predicate) =
predicate.kind().skip_binder()
obligation.predicate.kind().skip_binder()
{
let def_id = poly_trait_predicate.trait_ref.def_id;
let descr_pre =
Expand Down
11 changes: 11 additions & 0 deletions src/test/ui/lint/unused/unused-supertrait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#![deny(unused_must_use)]

fn it() -> impl ExactSizeIterator<Item = ()> {
let x: Box<dyn ExactSizeIterator<Item = ()>> = todo!();
x
}

fn main() {
it();
//~^ ERROR unused implementer of `Iterator` that must be used
}
15 changes: 15 additions & 0 deletions src/test/ui/lint/unused/unused-supertrait.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error: unused implementer of `Iterator` that must be used
--> $DIR/unused-supertrait.rs:9:5
|
LL | it();
| ^^^^^
|
note: the lint level is defined here
--> $DIR/unused-supertrait.rs:1:9
|
LL | #![deny(unused_must_use)]
| ^^^^^^^^^^^^^^^
= note: iterators are lazy and do nothing unless consumed

error: aborting due to previous error

0 comments on commit 00a7bfc

Please sign in to comment.