Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions compiler/rustc_lint/src/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,13 +273,11 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
expr: &hir::Expr<'_>,
span: Span,
) -> Option<MustUsePath> {
if ty.is_unit()
|| !ty.is_inhabited_from(
cx.tcx,
cx.tcx.parent_module(expr.hir_id).to_def_id(),
cx.typing_env(),
)
{
if ty.is_unit() {
return Some(MustUsePath::Suppressed);
}
let parent_mod_did = cx.tcx.parent_module(expr.hir_id).to_def_id();
if !ty.is_inhabited_from(cx.tcx, parent_mod_did, cx.typing_env()) {
return Some(MustUsePath::Suppressed);
}

Expand All @@ -293,6 +291,30 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
is_ty_must_use(cx, pinned_ty, expr, span)
.map(|inner| MustUsePath::Pinned(Box::new(inner)))
}
// Suppress warnings on `Result<(), UninhabitedType>` (e.g. `Result<(), !>`).
ty::Adt(def, args)
if cx.tcx.is_diagnostic_item(sym::Result, def.did())
&& args.type_at(0).is_unit()
&& !args.type_at(1).is_inhabited_from(
cx.tcx,
parent_mod_did,
cx.typing_env(),
) =>
{
Some(MustUsePath::Suppressed)
}
// Suppress warnings on `ControlFlow<Uninhabited, ()>` (e.g. `ControlFlow<!, ()>`).
ty::Adt(def, args)
if cx.tcx.is_diagnostic_item(sym::ControlFlow, def.did())
&& args.type_at(1).is_unit()
&& !args.type_at(0).is_inhabited_from(
cx.tcx,
parent_mod_did,
cx.typing_env(),
) =>
{
Some(MustUsePath::Suppressed)
}
ty::Adt(def, _) => is_def_must_use(cx, def.did(), span),
ty::Alias(ty::Opaque | ty::Projection, ty::AliasTy { def_id: def, .. }) => {
elaborate(cx.tcx, cx.tcx.explicit_item_self_bounds(def).iter_identity_copied())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub enum MyUninhabited {}

#[non_exhaustive]
pub enum MyUninhabitedNonexhaustive {}
93 changes: 93 additions & 0 deletions tests/ui/lint/unused/must_use-result-unit-uninhabited.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//@ edition: 2024
//@ aux-crate:dep=must_use_result_unit_uninhabited_extern_crate.rs

#![deny(unused_must_use)]
#![feature(never_type)]

use core::ops::{ControlFlow, ControlFlow::Continue};
use dep::{MyUninhabited, MyUninhabitedNonexhaustive};

fn f1() -> Result<(), ()> {
Ok(())
}

fn f2() -> Result<(), core::convert::Infallible> {
Ok(())
}

fn f3() -> Result<(), !> {
Ok(())
}

fn f4() -> Result<(), MyUninhabited> {
Ok(())
}

fn f5() -> Result<(), MyUninhabitedNonexhaustive> {
Ok(())
}

trait AssocType {
type Error;
}

struct S1;
impl AssocType for S1 {
type Error = !;
}

struct S2;
impl AssocType for S2 {
type Error = ();
}

fn f6<AT: AssocType>(_: AT) -> Result<(), AT::Error> {
Ok(())
}

trait UsesAssocType {
type Error;
fn method(&self) -> Result<(), Self::Error>;
}

impl UsesAssocType for S1 {
type Error = !;
fn method(&self) -> Result<(), Self::Error> {
Ok(())
}
}

impl UsesAssocType for S2 {
type Error = ();
fn method(&self) -> Result<(), Self::Error> {
Err(())
}
}

fn c1() -> ControlFlow<()> {
Continue(())
}

fn c2() -> ControlFlow<core::convert::Infallible, ()> {
Continue(())
}

fn c3() -> ControlFlow<!> {
Continue(())
}

fn main() {
f1(); //~ ERROR: unused `Result` that must be used
f2();
f3();
f4();
f5(); //~ ERROR: unused `Result` that must be used
f6(S1);
f6(S2); //~ ERROR: unused `Result` that must be used
S1.method();
S2.method(); //~ ERROR: unused `Result` that must be used

c1(); //~ ERROR: unused `ControlFlow` that must be used
c2();
c3();
}
66 changes: 66 additions & 0 deletions tests/ui/lint/unused/must_use-result-unit-uninhabited.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
error: unused `Result` that must be used
--> $DIR/must_use-result-unit-uninhabited.rs:80:5
|
LL | f1();
| ^^^^
|
= note: this `Result` may be an `Err` variant, which should be handled
note: the lint level is defined here
--> $DIR/must_use-result-unit-uninhabited.rs:4:9
|
LL | #![deny(unused_must_use)]
| ^^^^^^^^^^^^^^^
help: use `let _ = ...` to ignore the resulting value
|
LL | let _ = f1();
| +++++++

error: unused `Result` that must be used
--> $DIR/must_use-result-unit-uninhabited.rs:84:5
|
LL | f5();
| ^^^^
|
= note: this `Result` may be an `Err` variant, which should be handled
help: use `let _ = ...` to ignore the resulting value
|
LL | let _ = f5();
| +++++++

error: unused `Result` that must be used
--> $DIR/must_use-result-unit-uninhabited.rs:86:5
|
LL | f6(S2);
| ^^^^^^
|
= note: this `Result` may be an `Err` variant, which should be handled
help: use `let _ = ...` to ignore the resulting value
|
LL | let _ = f6(S2);
| +++++++

error: unused `Result` that must be used
--> $DIR/must_use-result-unit-uninhabited.rs:88:5
|
LL | S2.method();
| ^^^^^^^^^^^
|
= note: this `Result` may be an `Err` variant, which should be handled
help: use `let _ = ...` to ignore the resulting value
|
LL | let _ = S2.method();
| +++++++

error: unused `ControlFlow` that must be used
--> $DIR/must_use-result-unit-uninhabited.rs:90:5
|
LL | c1();
| ^^^^
|
help: use `let _ = ...` to ignore the resulting value
|
LL | let _ = c1();
| +++++++

error: aborting due to 5 previous errors

Loading