未验证 提交 66d91d82 编写于 作者: D Dylan DPC 提交者: GitHub

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.
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res}; use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::DefId; use rustc_hir::def_id::DefId;
use rustc_infer::traits::util::elaborate_predicates_with_span;
use rustc_middle::ty::adjustment; use rustc_middle::ty::adjustment;
use rustc_middle::ty::{self, Ty}; use rustc_middle::ty::{self, Ty};
use rustc_span::symbol::Symbol; use rustc_span::symbol::Symbol;
...@@ -204,10 +205,13 @@ fn check_must_use_ty<'tcx>( ...@@ -204,10 +205,13 @@ fn check_must_use_ty<'tcx>(
ty::Adt(def, _) => check_must_use_def(cx, def.did(), span, descr_pre, descr_post), ty::Adt(def, _) => check_must_use_def(cx, def.did(), span, descr_pre, descr_post),
ty::Opaque(def, _) => { ty::Opaque(def, _) => {
let mut has_emitted = false; 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. // We only look at the `DefId`, so it is safe to skip the binder here.
if let ty::PredicateKind::Trait(ref poly_trait_predicate) = 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 def_id = poly_trait_predicate.trait_ref.def_id;
let descr_pre = let descr_pre =
......
#![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
}
error: unused implementer of `Iterator` that must be used
--> $DIR/unused-supertrait.rs:9:5
|
LL | it();
| ^^^^^
|
= note: iterators are lazy and do nothing unless consumed
note: the lint level is defined here
--> $DIR/unused-supertrait.rs:1:9
|
LL | #![deny(unused_must_use)]
| ^^^^^^^^^^^^^^^
error: aborting due to previous error
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册