From d1009a42d820ded1d446c62d3acb69dffc5117c8 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Thu, 30 Nov 2023 02:24:11 +0000 Subject: [PATCH] Enforce must_use on associated types and RPITITs --- compiler/rustc_lint/src/unused.rs | 2 +- .../lint/unused/assoc-types.assoc_ty.stderr | 15 ++++++++++++ .../ui/lint/unused/assoc-types.rpitit.stderr | 15 ++++++++++++ tests/ui/lint/unused/assoc-types.rs | 23 +++++++++++++++++++ 4 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 tests/ui/lint/unused/assoc-types.assoc_ty.stderr create mode 100644 tests/ui/lint/unused/assoc-types.rpitit.stderr create mode 100644 tests/ui/lint/unused/assoc-types.rs diff --git a/compiler/rustc_lint/src/unused.rs b/compiler/rustc_lint/src/unused.rs index 0a167b0893c59..c492e7c6fbfb8 100644 --- a/compiler/rustc_lint/src/unused.rs +++ b/compiler/rustc_lint/src/unused.rs @@ -291,7 +291,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults { .map(|inner| MustUsePath::Pinned(Box::new(inner))) } ty::Adt(def, _) => is_def_must_use(cx, def.did(), span), - ty::Alias(ty::Opaque, ty::AliasTy { def_id: def, .. }) => { + ty::Alias(ty::Opaque | ty::Projection, ty::AliasTy { def_id: def, .. }) => { elaborate( cx.tcx, cx.tcx.explicit_item_bounds(def).instantiate_identity_iter_copied(), diff --git a/tests/ui/lint/unused/assoc-types.assoc_ty.stderr b/tests/ui/lint/unused/assoc-types.assoc_ty.stderr new file mode 100644 index 0000000000000..190c4ef0cea0d --- /dev/null +++ b/tests/ui/lint/unused/assoc-types.assoc_ty.stderr @@ -0,0 +1,15 @@ +error: unused implementer of `Future` that must be used + --> $DIR/assoc-types.rs:19:5 + | +LL | T::foo(); + | ^^^^^^^^ + | + = note: futures do nothing unless you `.await` or poll them +note: the lint level is defined here + --> $DIR/assoc-types.rs:4:9 + | +LL | #![deny(unused_must_use)] + | ^^^^^^^^^^^^^^^ + +error: aborting due to 1 previous error + diff --git a/tests/ui/lint/unused/assoc-types.rpitit.stderr b/tests/ui/lint/unused/assoc-types.rpitit.stderr new file mode 100644 index 0000000000000..190c4ef0cea0d --- /dev/null +++ b/tests/ui/lint/unused/assoc-types.rpitit.stderr @@ -0,0 +1,15 @@ +error: unused implementer of `Future` that must be used + --> $DIR/assoc-types.rs:19:5 + | +LL | T::foo(); + | ^^^^^^^^ + | + = note: futures do nothing unless you `.await` or poll them +note: the lint level is defined here + --> $DIR/assoc-types.rs:4:9 + | +LL | #![deny(unused_must_use)] + | ^^^^^^^^^^^^^^^ + +error: aborting due to 1 previous error + diff --git a/tests/ui/lint/unused/assoc-types.rs b/tests/ui/lint/unused/assoc-types.rs new file mode 100644 index 0000000000000..cebb9b4090ce9 --- /dev/null +++ b/tests/ui/lint/unused/assoc-types.rs @@ -0,0 +1,23 @@ +// edition: 2021 +// revisions: rpitit assoc_ty + +#![deny(unused_must_use)] + +use std::future::Future; + +pub trait Tr { + type Fut: Future; + + #[cfg(rpitit)] + fn foo() -> impl Future; + + #[cfg(assoc_ty)] + fn foo() -> Self::Fut; +} + +pub async fn bar() { + T::foo(); + //~^ ERROR unused implementer of `Future` that must be used +} + +fn main() {}