Skip to content

Commit 7451f9f

Browse files
committed
Auto merge of #147897 - Zalathar:rollup-gzveh7z, r=Zalathar
Rollup of 2 pull requests Successful merges: - #146167 (Deny-by-default never type lints) - #147382 (unused_must_use: Don't warn on `Result<(), Uninhabited>` or `ControlFlow<Uninhabited, ()>`) r? `@ghost` `@rustbot` modify labels: rollup
2 parents c0c37ca + acd42f6 commit 7451f9f

33 files changed

+427
-409
lines changed

compiler/rustc_lint/src/unused.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -273,13 +273,13 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
273273
expr: &hir::Expr<'_>,
274274
span: Span,
275275
) -> Option<MustUsePath> {
276-
if ty.is_unit()
277-
|| !ty.is_inhabited_from(
278-
cx.tcx,
279-
cx.tcx.parent_module(expr.hir_id).to_def_id(),
280-
cx.typing_env(),
281-
)
282-
{
276+
if ty.is_unit() {
277+
return Some(MustUsePath::Suppressed);
278+
}
279+
let parent_mod_did = cx.tcx.parent_module(expr.hir_id).to_def_id();
280+
let is_uninhabited =
281+
|t: Ty<'tcx>| !t.is_inhabited_from(cx.tcx, parent_mod_did, cx.typing_env());
282+
if is_uninhabited(ty) {
283283
return Some(MustUsePath::Suppressed);
284284
}
285285

@@ -293,6 +293,22 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
293293
is_ty_must_use(cx, pinned_ty, expr, span)
294294
.map(|inner| MustUsePath::Pinned(Box::new(inner)))
295295
}
296+
// Suppress warnings on `Result<(), Uninhabited>` (e.g. `Result<(), !>`).
297+
ty::Adt(def, args)
298+
if cx.tcx.is_diagnostic_item(sym::Result, def.did())
299+
&& args.type_at(0).is_unit()
300+
&& is_uninhabited(args.type_at(1)) =>
301+
{
302+
Some(MustUsePath::Suppressed)
303+
}
304+
// Suppress warnings on `ControlFlow<Uninhabited, ()>` (e.g. `ControlFlow<!, ()>`).
305+
ty::Adt(def, args)
306+
if cx.tcx.is_diagnostic_item(sym::ControlFlow, def.did())
307+
&& args.type_at(1).is_unit()
308+
&& is_uninhabited(args.type_at(0)) =>
309+
{
310+
Some(MustUsePath::Suppressed)
311+
}
296312
ty::Adt(def, _) => is_def_must_use(cx, def.did(), span),
297313
ty::Alias(ty::Opaque | ty::Projection, ty::AliasTy { def_id: def, .. }) => {
298314
elaborate(cx.tcx, cx.tcx.explicit_item_self_bounds(def).iter_identity_copied())

compiler/rustc_lint_defs/src/builtin.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4065,7 +4065,6 @@ declare_lint! {
40654065
/// ### Example
40664066
///
40674067
/// ```rust,compile_fail
4068-
/// #![deny(never_type_fallback_flowing_into_unsafe)]
40694068
/// fn main() {
40704069
/// if true {
40714070
/// // return has type `!` which, is some cases, causes never type fallback
@@ -4100,7 +4099,7 @@ declare_lint! {
41004099
/// [`!`]: https://doc.rust-lang.org/core/primitive.never.html
41014100
/// [`()`]: https://doc.rust-lang.org/core/primitive.unit.html
41024101
pub NEVER_TYPE_FALLBACK_FLOWING_INTO_UNSAFE,
4103-
Warn,
4102+
Deny,
41044103
"never type fallback affecting unsafe function calls",
41054104
@future_incompatible = FutureIncompatibleInfo {
41064105
reason: FutureIncompatibilityReason::EditionAndFutureReleaseSemanticsChange(Edition::Edition2024),
@@ -4122,7 +4121,7 @@ declare_lint! {
41224121
/// ### Example
41234122
///
41244123
/// ```rust,compile_fail,edition2021
4125-
/// #![deny(dependency_on_unit_never_type_fallback)]
4124+
/// # #![deny(dependency_on_unit_never_type_fallback)]
41264125
/// fn main() {
41274126
/// if true {
41284127
/// // return has type `!` which, is some cases, causes never type fallback
@@ -4155,7 +4154,7 @@ declare_lint! {
41554154
///
41564155
/// See [Tracking Issue for making `!` fall back to `!`](https://github.com/rust-lang/rust/issues/123748).
41574156
pub DEPENDENCY_ON_UNIT_NEVER_TYPE_FALLBACK,
4158-
Warn,
4157+
Deny,
41594158
"never type fallback affecting unsafe function calls",
41604159
@future_incompatible = FutureIncompatibleInfo {
41614160
reason: FutureIncompatibilityReason::EditionAndFutureReleaseError(Edition::Edition2024),

tests/ui/editions/never-type-fallback-breaking.e2021.fixed

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
//@[e2021] edition: 2021
44
//@[e2024] edition: 2024
55
//
6-
//@[e2021] run-pass
76
//@[e2021] run-rustfix
8-
//@[e2024] check-fail
97

108
fn main() {
119
m();
@@ -16,8 +14,8 @@ fn main() {
1614
}
1715

1816
fn m() {
19-
//[e2021]~^ WARN this function depends on never type fallback being `()`
20-
//[e2021]~| WARN this was previously accepted by the compiler but is being phased out; it will become a hard error in Rust 2024 and in a future release in all editions!
17+
//[e2021]~^ error: this function depends on never type fallback being `()`
18+
//[e2021]~| warn: this was previously accepted by the compiler but is being phased out; it will become a hard error in Rust 2024 and in a future release in all editions!
2119
let x: () = match true {
2220
true => Default::default(),
2321
//[e2024]~^ error: the trait bound `!: Default` is not satisfied
@@ -28,8 +26,8 @@ fn m() {
2826
}
2927

3028
fn q() -> Option<()> {
31-
//[e2021]~^ WARN this function depends on never type fallback being `()`
32-
//[e2021]~| WARN this was previously accepted by the compiler but is being phased out; it will become a hard error in Rust 2024 and in a future release in all editions!
29+
//[e2021]~^ error: this function depends on never type fallback being `()`
30+
//[e2021]~| warn: this was previously accepted by the compiler but is being phased out; it will become a hard error in Rust 2024 and in a future release in all editions!
3331
fn deserialize<T: Default>() -> Option<T> {
3432
Some(T::default())
3533
}
@@ -45,8 +43,8 @@ fn help<'a: 'a, T: Into<()>, U>(_: U) -> Result<T, ()> {
4543
Err(())
4644
}
4745
fn meow() -> Result<(), ()> {
48-
//[e2021]~^ WARN this function depends on never type fallback being `()`
49-
//[e2021]~| WARN this was previously accepted by the compiler but is being phased out; it will become a hard error in Rust 2024 and in a future release in all editions!
46+
//[e2021]~^ error: this function depends on never type fallback being `()`
47+
//[e2021]~| warn: this was previously accepted by the compiler but is being phased out; it will become a hard error in Rust 2024 and in a future release in all editions!
5048
help::<(), _>(1)?;
5149
//[e2024]~^ error: the trait bound `(): From<!>` is not satisfied
5250
Ok(())
@@ -57,8 +55,8 @@ pub fn takes_apit<T>(_y: impl Fn() -> T) -> Result<T, ()> {
5755
}
5856

5957
pub fn fallback_return() -> Result<(), ()> {
60-
//[e2021]~^ WARN this function depends on never type fallback being `()`
61-
//[e2021]~| WARN this was previously accepted by the compiler but is being phased out; it will become a hard error in Rust 2024 and in a future release in all editions!
58+
//[e2021]~^ error: this function depends on never type fallback being `()`
59+
//[e2021]~| warn: this was previously accepted by the compiler but is being phased out; it will become a hard error in Rust 2024 and in a future release in all editions!
6260
takes_apit::<()>(|| Default::default())?;
6361
//[e2024]~^ error: the trait bound `!: Default` is not satisfied
6462
Ok(())
@@ -71,8 +69,8 @@ fn mk<T>() -> Result<T, ()> {
7169
fn takes_apit2(_x: impl Default) {}
7270

7371
fn fully_apit() -> Result<(), ()> {
74-
//[e2021]~^ WARN this function depends on never type fallback being `()`
75-
//[e2021]~| WARN this was previously accepted by the compiler but is being phased out; it will become a hard error in Rust 2024 and in a future release in all editions!
72+
//[e2021]~^ error: this function depends on never type fallback being `()`
73+
//[e2021]~| warn: this was previously accepted by the compiler but is being phased out; it will become a hard error in Rust 2024 and in a future release in all editions!
7674
takes_apit2(mk::<()>()?);
7775
//[e2024]~^ error: the trait bound `!: Default` is not satisfied
7876
Ok(())

tests/ui/editions/never-type-fallback-breaking.e2021.stderr

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
warning: this function depends on never type fallback being `()`
2-
--> $DIR/never-type-fallback-breaking.rs:18:1
1+
error: this function depends on never type fallback being `()`
2+
--> $DIR/never-type-fallback-breaking.rs:16:1
33
|
44
LL | fn m() {
55
| ^^^^^^
@@ -8,18 +8,18 @@ LL | fn m() {
88
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/never-type-fallback.html>
99
= help: specify the types explicitly
1010
note: in edition 2024, the requirement `!: Default` will fail
11-
--> $DIR/never-type-fallback-breaking.rs:22:17
11+
--> $DIR/never-type-fallback-breaking.rs:20:17
1212
|
1313
LL | true => Default::default(),
1414
| ^^^^^^^^^^^^^^^^^^
15-
= note: `#[warn(dependency_on_unit_never_type_fallback)]` (part of `#[warn(rust_2024_compatibility)]`) on by default
15+
= note: `#[deny(dependency_on_unit_never_type_fallback)]` (part of `#[deny(rust_2024_compatibility)]`) on by default
1616
help: use `()` annotations to avoid fallback changes
1717
|
1818
LL | let x: () = match true {
1919
| ++++
2020

21-
warning: this function depends on never type fallback being `()`
22-
--> $DIR/never-type-fallback-breaking.rs:30:1
21+
error: this function depends on never type fallback being `()`
22+
--> $DIR/never-type-fallback-breaking.rs:28:1
2323
|
2424
LL | fn q() -> Option<()> {
2525
| ^^^^^^^^^^^^^^^^^^^^
@@ -28,7 +28,7 @@ LL | fn q() -> Option<()> {
2828
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/never-type-fallback.html>
2929
= help: specify the types explicitly
3030
note: in edition 2024, the requirement `!: Default` will fail
31-
--> $DIR/never-type-fallback-breaking.rs:37:5
31+
--> $DIR/never-type-fallback-breaking.rs:35:5
3232
|
3333
LL | deserialize()?;
3434
| ^^^^^^^^^^^^^
@@ -37,8 +37,8 @@ help: use `()` annotations to avoid fallback changes
3737
LL | deserialize::<()>()?;
3838
| ++++++
3939

40-
warning: this function depends on never type fallback being `()`
41-
--> $DIR/never-type-fallback-breaking.rs:47:1
40+
error: this function depends on never type fallback being `()`
41+
--> $DIR/never-type-fallback-breaking.rs:45:1
4242
|
4343
LL | fn meow() -> Result<(), ()> {
4444
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -47,7 +47,7 @@ LL | fn meow() -> Result<(), ()> {
4747
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/never-type-fallback.html>
4848
= help: specify the types explicitly
4949
note: in edition 2024, the requirement `(): From<!>` will fail
50-
--> $DIR/never-type-fallback-breaking.rs:50:5
50+
--> $DIR/never-type-fallback-breaking.rs:48:5
5151
|
5252
LL | help(1)?;
5353
| ^^^^^^^
@@ -56,8 +56,8 @@ help: use `()` annotations to avoid fallback changes
5656
LL | help::<(), _>(1)?;
5757
| +++++++++
5858

59-
warning: this function depends on never type fallback being `()`
60-
--> $DIR/never-type-fallback-breaking.rs:59:1
59+
error: this function depends on never type fallback being `()`
60+
--> $DIR/never-type-fallback-breaking.rs:57:1
6161
|
6262
LL | pub fn fallback_return() -> Result<(), ()> {
6363
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -66,7 +66,7 @@ LL | pub fn fallback_return() -> Result<(), ()> {
6666
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/never-type-fallback.html>
6767
= help: specify the types explicitly
6868
note: in edition 2024, the requirement `!: Default` will fail
69-
--> $DIR/never-type-fallback-breaking.rs:62:19
69+
--> $DIR/never-type-fallback-breaking.rs:60:19
7070
|
7171
LL | takes_apit(|| Default::default())?;
7272
| ^^^^^^^^^^^^^^^^^^
@@ -75,8 +75,8 @@ help: use `()` annotations to avoid fallback changes
7575
LL | takes_apit::<()>(|| Default::default())?;
7676
| ++++++
7777

78-
warning: this function depends on never type fallback being `()`
79-
--> $DIR/never-type-fallback-breaking.rs:73:1
78+
error: this function depends on never type fallback being `()`
79+
--> $DIR/never-type-fallback-breaking.rs:71:1
8080
|
8181
LL | fn fully_apit() -> Result<(), ()> {
8282
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -85,7 +85,7 @@ LL | fn fully_apit() -> Result<(), ()> {
8585
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/never-type-fallback.html>
8686
= help: specify the types explicitly
8787
note: in edition 2024, the requirement `!: Default` will fail
88-
--> $DIR/never-type-fallback-breaking.rs:76:17
88+
--> $DIR/never-type-fallback-breaking.rs:74:17
8989
|
9090
LL | takes_apit2(mk()?);
9191
| ^^^^^
@@ -94,11 +94,11 @@ help: use `()` annotations to avoid fallback changes
9494
LL | takes_apit2(mk::<()>()?);
9595
| ++++++
9696

97-
warning: 5 warnings emitted
97+
error: aborting due to 5 previous errors
9898

9999
Future incompatibility report: Future breakage diagnostic:
100-
warning: this function depends on never type fallback being `()`
101-
--> $DIR/never-type-fallback-breaking.rs:18:1
100+
error: this function depends on never type fallback being `()`
101+
--> $DIR/never-type-fallback-breaking.rs:16:1
102102
|
103103
LL | fn m() {
104104
| ^^^^^^
@@ -107,19 +107,19 @@ LL | fn m() {
107107
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/never-type-fallback.html>
108108
= help: specify the types explicitly
109109
note: in edition 2024, the requirement `!: Default` will fail
110-
--> $DIR/never-type-fallback-breaking.rs:22:17
110+
--> $DIR/never-type-fallback-breaking.rs:20:17
111111
|
112112
LL | true => Default::default(),
113113
| ^^^^^^^^^^^^^^^^^^
114-
= note: `#[warn(dependency_on_unit_never_type_fallback)]` (part of `#[warn(rust_2024_compatibility)]`) on by default
114+
= note: `#[deny(dependency_on_unit_never_type_fallback)]` (part of `#[deny(rust_2024_compatibility)]`) on by default
115115
help: use `()` annotations to avoid fallback changes
116116
|
117117
LL | let x: () = match true {
118118
| ++++
119119

120120
Future breakage diagnostic:
121-
warning: this function depends on never type fallback being `()`
122-
--> $DIR/never-type-fallback-breaking.rs:30:1
121+
error: this function depends on never type fallback being `()`
122+
--> $DIR/never-type-fallback-breaking.rs:28:1
123123
|
124124
LL | fn q() -> Option<()> {
125125
| ^^^^^^^^^^^^^^^^^^^^
@@ -128,19 +128,19 @@ LL | fn q() -> Option<()> {
128128
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/never-type-fallback.html>
129129
= help: specify the types explicitly
130130
note: in edition 2024, the requirement `!: Default` will fail
131-
--> $DIR/never-type-fallback-breaking.rs:37:5
131+
--> $DIR/never-type-fallback-breaking.rs:35:5
132132
|
133133
LL | deserialize()?;
134134
| ^^^^^^^^^^^^^
135-
= note: `#[warn(dependency_on_unit_never_type_fallback)]` (part of `#[warn(rust_2024_compatibility)]`) on by default
135+
= note: `#[deny(dependency_on_unit_never_type_fallback)]` (part of `#[deny(rust_2024_compatibility)]`) on by default
136136
help: use `()` annotations to avoid fallback changes
137137
|
138138
LL | deserialize::<()>()?;
139139
| ++++++
140140

141141
Future breakage diagnostic:
142-
warning: this function depends on never type fallback being `()`
143-
--> $DIR/never-type-fallback-breaking.rs:47:1
142+
error: this function depends on never type fallback being `()`
143+
--> $DIR/never-type-fallback-breaking.rs:45:1
144144
|
145145
LL | fn meow() -> Result<(), ()> {
146146
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -149,19 +149,19 @@ LL | fn meow() -> Result<(), ()> {
149149
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/never-type-fallback.html>
150150
= help: specify the types explicitly
151151
note: in edition 2024, the requirement `(): From<!>` will fail
152-
--> $DIR/never-type-fallback-breaking.rs:50:5
152+
--> $DIR/never-type-fallback-breaking.rs:48:5
153153
|
154154
LL | help(1)?;
155155
| ^^^^^^^
156-
= note: `#[warn(dependency_on_unit_never_type_fallback)]` (part of `#[warn(rust_2024_compatibility)]`) on by default
156+
= note: `#[deny(dependency_on_unit_never_type_fallback)]` (part of `#[deny(rust_2024_compatibility)]`) on by default
157157
help: use `()` annotations to avoid fallback changes
158158
|
159159
LL | help::<(), _>(1)?;
160160
| +++++++++
161161

162162
Future breakage diagnostic:
163-
warning: this function depends on never type fallback being `()`
164-
--> $DIR/never-type-fallback-breaking.rs:59:1
163+
error: this function depends on never type fallback being `()`
164+
--> $DIR/never-type-fallback-breaking.rs:57:1
165165
|
166166
LL | pub fn fallback_return() -> Result<(), ()> {
167167
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -170,19 +170,19 @@ LL | pub fn fallback_return() -> Result<(), ()> {
170170
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/never-type-fallback.html>
171171
= help: specify the types explicitly
172172
note: in edition 2024, the requirement `!: Default` will fail
173-
--> $DIR/never-type-fallback-breaking.rs:62:19
173+
--> $DIR/never-type-fallback-breaking.rs:60:19
174174
|
175175
LL | takes_apit(|| Default::default())?;
176176
| ^^^^^^^^^^^^^^^^^^
177-
= note: `#[warn(dependency_on_unit_never_type_fallback)]` (part of `#[warn(rust_2024_compatibility)]`) on by default
177+
= note: `#[deny(dependency_on_unit_never_type_fallback)]` (part of `#[deny(rust_2024_compatibility)]`) on by default
178178
help: use `()` annotations to avoid fallback changes
179179
|
180180
LL | takes_apit::<()>(|| Default::default())?;
181181
| ++++++
182182

183183
Future breakage diagnostic:
184-
warning: this function depends on never type fallback being `()`
185-
--> $DIR/never-type-fallback-breaking.rs:73:1
184+
error: this function depends on never type fallback being `()`
185+
--> $DIR/never-type-fallback-breaking.rs:71:1
186186
|
187187
LL | fn fully_apit() -> Result<(), ()> {
188188
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -191,11 +191,11 @@ LL | fn fully_apit() -> Result<(), ()> {
191191
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/never-type-fallback.html>
192192
= help: specify the types explicitly
193193
note: in edition 2024, the requirement `!: Default` will fail
194-
--> $DIR/never-type-fallback-breaking.rs:76:17
194+
--> $DIR/never-type-fallback-breaking.rs:74:17
195195
|
196196
LL | takes_apit2(mk()?);
197197
| ^^^^^
198-
= note: `#[warn(dependency_on_unit_never_type_fallback)]` (part of `#[warn(rust_2024_compatibility)]`) on by default
198+
= note: `#[deny(dependency_on_unit_never_type_fallback)]` (part of `#[deny(rust_2024_compatibility)]`) on by default
199199
help: use `()` annotations to avoid fallback changes
200200
|
201201
LL | takes_apit2(mk::<()>()?);

0 commit comments

Comments
 (0)