Skip to content

Commit 9475e60

Browse files
committed
Auto merge of #89770 - jkugelman:must-use-from-and-into, r=joshtriplett
Add #[must_use] to From::from and Into::into Risk of churn: **High** Magic 8-Ball says: **Outlook not so good** I figured I'd put this out there. If we don't do it now maybe we save it for a rainy day. Parent issue: #89692 r? `@joshtriplett`
2 parents 02f2b31 + f9692b5 commit 9475e60

9 files changed

+154
-151
lines changed

library/core/src/convert/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ pub trait AsMut<T: ?Sized> {
273273
#[stable(feature = "rust1", since = "1.0.0")]
274274
pub trait Into<T>: Sized {
275275
/// Performs the conversion.
276+
#[must_use]
276277
#[stable(feature = "rust1", since = "1.0.0")]
277278
fn into(self) -> T;
278279
}
@@ -367,6 +368,7 @@ pub trait Into<T>: Sized {
367368
pub trait From<T>: Sized {
368369
/// Performs the conversion.
369370
#[lang = "from"]
371+
#[must_use]
370372
#[stable(feature = "rust1", since = "1.0.0")]
371373
fn from(_: T) -> Self;
372374
}

src/test/ui/never_type/never-value-fallback-issue-66757.nofallback.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0277]: the trait bound `E: From<()>` is not satisfied
2-
--> $DIR/never-value-fallback-issue-66757.rs:27:5
2+
--> $DIR/never-value-fallback-issue-66757.rs:28:5
33
|
44
LL | <E as From<_>>::from(never);
55
| ^^^^^^^^^^^^^^^^^^^^ the trait `From<()>` is not implemented for `E`

src/test/ui/never_type/never-value-fallback-issue-66757.rs

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ impl From<!> for E {
2222

2323
#[allow(unreachable_code)]
2424
#[allow(dead_code)]
25+
#[allow(unused_must_use)]
2526
fn foo(never: !) {
2627
<E as From<!>>::from(never); // Ok
2728
<E as From<_>>::from(never); //[nofallback]~ ERROR trait bound `E: From<()>` is not satisfied

src/tools/clippy/tests/ui/cast_lossless_float.fixed

+11-11
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,24 @@
66
fn main() {
77
// Test clippy::cast_lossless with casts to floating-point types
88
let x0 = 1i8;
9-
f32::from(x0);
10-
f64::from(x0);
9+
let _ = f32::from(x0);
10+
let _ = f64::from(x0);
1111
let x1 = 1u8;
12-
f32::from(x1);
13-
f64::from(x1);
12+
let _ = f32::from(x1);
13+
let _ = f64::from(x1);
1414
let x2 = 1i16;
15-
f32::from(x2);
16-
f64::from(x2);
15+
let _ = f32::from(x2);
16+
let _ = f64::from(x2);
1717
let x3 = 1u16;
18-
f32::from(x3);
19-
f64::from(x3);
18+
let _ = f32::from(x3);
19+
let _ = f64::from(x3);
2020
let x4 = 1i32;
21-
f64::from(x4);
21+
let _ = f64::from(x4);
2222
let x5 = 1u32;
23-
f64::from(x5);
23+
let _ = f64::from(x5);
2424

2525
// Test with casts from floating-point types
26-
f64::from(1.0f32);
26+
let _ = f64::from(1.0f32);
2727
}
2828

2929
// The lint would suggest using `f64::from(input)` here but the `XX::from` function is not const,

src/tools/clippy/tests/ui/cast_lossless_float.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,24 @@
66
fn main() {
77
// Test clippy::cast_lossless with casts to floating-point types
88
let x0 = 1i8;
9-
x0 as f32;
10-
x0 as f64;
9+
let _ = x0 as f32;
10+
let _ = x0 as f64;
1111
let x1 = 1u8;
12-
x1 as f32;
13-
x1 as f64;
12+
let _ = x1 as f32;
13+
let _ = x1 as f64;
1414
let x2 = 1i16;
15-
x2 as f32;
16-
x2 as f64;
15+
let _ = x2 as f32;
16+
let _ = x2 as f64;
1717
let x3 = 1u16;
18-
x3 as f32;
19-
x3 as f64;
18+
let _ = x3 as f32;
19+
let _ = x3 as f64;
2020
let x4 = 1i32;
21-
x4 as f64;
21+
let _ = x4 as f64;
2222
let x5 = 1u32;
23-
x5 as f64;
23+
let _ = x5 as f64;
2424

2525
// Test with casts from floating-point types
26-
1.0f32 as f64;
26+
let _ = 1.0f32 as f64;
2727
}
2828

2929
// The lint would suggest using `f64::from(input)` here but the `XX::from` function is not const,
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,70 @@
11
error: casting `i8` to `f32` may become silently lossy if you later change the type
2-
--> $DIR/cast_lossless_float.rs:9:5
2+
--> $DIR/cast_lossless_float.rs:9:13
33
|
4-
LL | x0 as f32;
5-
| ^^^^^^^^^ help: try: `f32::from(x0)`
4+
LL | let _ = x0 as f32;
5+
| ^^^^^^^^^ help: try: `f32::from(x0)`
66
|
77
= note: `-D clippy::cast-lossless` implied by `-D warnings`
88

99
error: casting `i8` to `f64` may become silently lossy if you later change the type
10-
--> $DIR/cast_lossless_float.rs:10:5
10+
--> $DIR/cast_lossless_float.rs:10:13
1111
|
12-
LL | x0 as f64;
13-
| ^^^^^^^^^ help: try: `f64::from(x0)`
12+
LL | let _ = x0 as f64;
13+
| ^^^^^^^^^ help: try: `f64::from(x0)`
1414

1515
error: casting `u8` to `f32` may become silently lossy if you later change the type
16-
--> $DIR/cast_lossless_float.rs:12:5
16+
--> $DIR/cast_lossless_float.rs:12:13
1717
|
18-
LL | x1 as f32;
19-
| ^^^^^^^^^ help: try: `f32::from(x1)`
18+
LL | let _ = x1 as f32;
19+
| ^^^^^^^^^ help: try: `f32::from(x1)`
2020

2121
error: casting `u8` to `f64` may become silently lossy if you later change the type
22-
--> $DIR/cast_lossless_float.rs:13:5
22+
--> $DIR/cast_lossless_float.rs:13:13
2323
|
24-
LL | x1 as f64;
25-
| ^^^^^^^^^ help: try: `f64::from(x1)`
24+
LL | let _ = x1 as f64;
25+
| ^^^^^^^^^ help: try: `f64::from(x1)`
2626

2727
error: casting `i16` to `f32` may become silently lossy if you later change the type
28-
--> $DIR/cast_lossless_float.rs:15:5
28+
--> $DIR/cast_lossless_float.rs:15:13
2929
|
30-
LL | x2 as f32;
31-
| ^^^^^^^^^ help: try: `f32::from(x2)`
30+
LL | let _ = x2 as f32;
31+
| ^^^^^^^^^ help: try: `f32::from(x2)`
3232

3333
error: casting `i16` to `f64` may become silently lossy if you later change the type
34-
--> $DIR/cast_lossless_float.rs:16:5
34+
--> $DIR/cast_lossless_float.rs:16:13
3535
|
36-
LL | x2 as f64;
37-
| ^^^^^^^^^ help: try: `f64::from(x2)`
36+
LL | let _ = x2 as f64;
37+
| ^^^^^^^^^ help: try: `f64::from(x2)`
3838

3939
error: casting `u16` to `f32` may become silently lossy if you later change the type
40-
--> $DIR/cast_lossless_float.rs:18:5
40+
--> $DIR/cast_lossless_float.rs:18:13
4141
|
42-
LL | x3 as f32;
43-
| ^^^^^^^^^ help: try: `f32::from(x3)`
42+
LL | let _ = x3 as f32;
43+
| ^^^^^^^^^ help: try: `f32::from(x3)`
4444

4545
error: casting `u16` to `f64` may become silently lossy if you later change the type
46-
--> $DIR/cast_lossless_float.rs:19:5
46+
--> $DIR/cast_lossless_float.rs:19:13
4747
|
48-
LL | x3 as f64;
49-
| ^^^^^^^^^ help: try: `f64::from(x3)`
48+
LL | let _ = x3 as f64;
49+
| ^^^^^^^^^ help: try: `f64::from(x3)`
5050

5151
error: casting `i32` to `f64` may become silently lossy if you later change the type
52-
--> $DIR/cast_lossless_float.rs:21:5
52+
--> $DIR/cast_lossless_float.rs:21:13
5353
|
54-
LL | x4 as f64;
55-
| ^^^^^^^^^ help: try: `f64::from(x4)`
54+
LL | let _ = x4 as f64;
55+
| ^^^^^^^^^ help: try: `f64::from(x4)`
5656

5757
error: casting `u32` to `f64` may become silently lossy if you later change the type
58-
--> $DIR/cast_lossless_float.rs:23:5
58+
--> $DIR/cast_lossless_float.rs:23:13
5959
|
60-
LL | x5 as f64;
61-
| ^^^^^^^^^ help: try: `f64::from(x5)`
60+
LL | let _ = x5 as f64;
61+
| ^^^^^^^^^ help: try: `f64::from(x5)`
6262

6363
error: casting `f32` to `f64` may become silently lossy if you later change the type
64-
--> $DIR/cast_lossless_float.rs:26:5
64+
--> $DIR/cast_lossless_float.rs:26:13
6565
|
66-
LL | 1.0f32 as f64;
67-
| ^^^^^^^^^^^^^ help: try: `f64::from(1.0f32)`
66+
LL | let _ = 1.0f32 as f64;
67+
| ^^^^^^^^^^^^^ help: try: `f64::from(1.0f32)`
6868

6969
error: aborting due to 11 previous errors
7070

src/tools/clippy/tests/ui/cast_lossless_integer.fixed

+19-19
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,27 @@
55

66
fn main() {
77
// Test clippy::cast_lossless with casts to integer types
8-
i16::from(1i8);
9-
i32::from(1i8);
10-
i64::from(1i8);
11-
i16::from(1u8);
12-
i32::from(1u8);
13-
i64::from(1u8);
14-
u16::from(1u8);
15-
u32::from(1u8);
16-
u64::from(1u8);
17-
i32::from(1i16);
18-
i64::from(1i16);
19-
i32::from(1u16);
20-
i64::from(1u16);
21-
u32::from(1u16);
22-
u64::from(1u16);
23-
i64::from(1i32);
24-
i64::from(1u32);
25-
u64::from(1u32);
8+
let _ = i16::from(1i8);
9+
let _ = i32::from(1i8);
10+
let _ = i64::from(1i8);
11+
let _ = i16::from(1u8);
12+
let _ = i32::from(1u8);
13+
let _ = i64::from(1u8);
14+
let _ = u16::from(1u8);
15+
let _ = u32::from(1u8);
16+
let _ = u64::from(1u8);
17+
let _ = i32::from(1i16);
18+
let _ = i64::from(1i16);
19+
let _ = i32::from(1u16);
20+
let _ = i64::from(1u16);
21+
let _ = u32::from(1u16);
22+
let _ = u64::from(1u16);
23+
let _ = i64::from(1i32);
24+
let _ = i64::from(1u32);
25+
let _ = u64::from(1u32);
2626

2727
// Test with an expression wrapped in parens
28-
u16::from(1u8 + 1u8);
28+
let _ = u16::from(1u8 + 1u8);
2929
}
3030

3131
// The lint would suggest using `f64::from(input)` here but the `XX::from` function is not const,

src/tools/clippy/tests/ui/cast_lossless_integer.rs

+19-19
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,27 @@
55

66
fn main() {
77
// Test clippy::cast_lossless with casts to integer types
8-
1i8 as i16;
9-
1i8 as i32;
10-
1i8 as i64;
11-
1u8 as i16;
12-
1u8 as i32;
13-
1u8 as i64;
14-
1u8 as u16;
15-
1u8 as u32;
16-
1u8 as u64;
17-
1i16 as i32;
18-
1i16 as i64;
19-
1u16 as i32;
20-
1u16 as i64;
21-
1u16 as u32;
22-
1u16 as u64;
23-
1i32 as i64;
24-
1u32 as i64;
25-
1u32 as u64;
8+
let _ = 1i8 as i16;
9+
let _ = 1i8 as i32;
10+
let _ = 1i8 as i64;
11+
let _ = 1u8 as i16;
12+
let _ = 1u8 as i32;
13+
let _ = 1u8 as i64;
14+
let _ = 1u8 as u16;
15+
let _ = 1u8 as u32;
16+
let _ = 1u8 as u64;
17+
let _ = 1i16 as i32;
18+
let _ = 1i16 as i64;
19+
let _ = 1u16 as i32;
20+
let _ = 1u16 as i64;
21+
let _ = 1u16 as u32;
22+
let _ = 1u16 as u64;
23+
let _ = 1i32 as i64;
24+
let _ = 1u32 as i64;
25+
let _ = 1u32 as u64;
2626

2727
// Test with an expression wrapped in parens
28-
(1u8 + 1u8) as u16;
28+
let _ = (1u8 + 1u8) as u16;
2929
}
3030

3131
// The lint would suggest using `f64::from(input)` here but the `XX::from` function is not const,

0 commit comments

Comments
 (0)