Skip to content

Add #[must_use] to From::from and Into::into #89770

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 12, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions library/core/src/convert/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ pub trait AsMut<T: ?Sized> {
#[stable(feature = "rust1", since = "1.0.0")]
pub trait Into<T>: Sized {
/// Performs the conversion.
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
fn into(self) -> T;
}
Expand Down Expand Up @@ -367,6 +368,7 @@ pub trait Into<T>: Sized {
pub trait From<T>: Sized {
/// Performs the conversion.
#[lang = "from"]
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
fn from(_: T) -> Self;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0277]: the trait bound `E: From<()>` is not satisfied
--> $DIR/never-value-fallback-issue-66757.rs:27:5
--> $DIR/never-value-fallback-issue-66757.rs:28:5
|
LL | <E as From<_>>::from(never);
| ^^^^^^^^^^^^^^^^^^^^ the trait `From<()>` is not implemented for `E`
Expand Down
1 change: 1 addition & 0 deletions src/test/ui/never_type/never-value-fallback-issue-66757.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ impl From<!> for E {

#[allow(unreachable_code)]
#[allow(dead_code)]
#[allow(unused_must_use)]
fn foo(never: !) {
<E as From<!>>::from(never); // Ok
<E as From<_>>::from(never); //[nofallback]~ ERROR trait bound `E: From<()>` is not satisfied
Expand Down
22 changes: 11 additions & 11 deletions src/tools/clippy/tests/ui/cast_lossless_float.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@
fn main() {
// Test clippy::cast_lossless with casts to floating-point types
let x0 = 1i8;
f32::from(x0);
f64::from(x0);
let _ = f32::from(x0);
let _ = f64::from(x0);
let x1 = 1u8;
f32::from(x1);
f64::from(x1);
let _ = f32::from(x1);
let _ = f64::from(x1);
let x2 = 1i16;
f32::from(x2);
f64::from(x2);
let _ = f32::from(x2);
let _ = f64::from(x2);
let x3 = 1u16;
f32::from(x3);
f64::from(x3);
let _ = f32::from(x3);
let _ = f64::from(x3);
let x4 = 1i32;
f64::from(x4);
let _ = f64::from(x4);
let x5 = 1u32;
f64::from(x5);
let _ = f64::from(x5);

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

// The lint would suggest using `f64::from(input)` here but the `XX::from` function is not const,
Expand Down
22 changes: 11 additions & 11 deletions src/tools/clippy/tests/ui/cast_lossless_float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@
fn main() {
// Test clippy::cast_lossless with casts to floating-point types
let x0 = 1i8;
x0 as f32;
x0 as f64;
let _ = x0 as f32;
let _ = x0 as f64;
let x1 = 1u8;
x1 as f32;
x1 as f64;
let _ = x1 as f32;
let _ = x1 as f64;
let x2 = 1i16;
x2 as f32;
x2 as f64;
let _ = x2 as f32;
let _ = x2 as f64;
let x3 = 1u16;
x3 as f32;
x3 as f64;
let _ = x3 as f32;
let _ = x3 as f64;
let x4 = 1i32;
x4 as f64;
let _ = x4 as f64;
let x5 = 1u32;
x5 as f64;
let _ = x5 as f64;

// Test with casts from floating-point types
1.0f32 as f64;
let _ = 1.0f32 as f64;
}

// The lint would suggest using `f64::from(input)` here but the `XX::from` function is not const,
Expand Down
66 changes: 33 additions & 33 deletions src/tools/clippy/tests/ui/cast_lossless_float.stderr
Original file line number Diff line number Diff line change
@@ -1,70 +1,70 @@
error: casting `i8` to `f32` may become silently lossy if you later change the type
--> $DIR/cast_lossless_float.rs:9:5
--> $DIR/cast_lossless_float.rs:9:13
|
LL | x0 as f32;
| ^^^^^^^^^ help: try: `f32::from(x0)`
LL | let _ = x0 as f32;
| ^^^^^^^^^ help: try: `f32::from(x0)`
|
= note: `-D clippy::cast-lossless` implied by `-D warnings`

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

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

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

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

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

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

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

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

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

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

error: aborting due to 11 previous errors

38 changes: 19 additions & 19 deletions src/tools/clippy/tests/ui/cast_lossless_integer.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@

fn main() {
// Test clippy::cast_lossless with casts to integer types
i16::from(1i8);
i32::from(1i8);
i64::from(1i8);
i16::from(1u8);
i32::from(1u8);
i64::from(1u8);
u16::from(1u8);
u32::from(1u8);
u64::from(1u8);
i32::from(1i16);
i64::from(1i16);
i32::from(1u16);
i64::from(1u16);
u32::from(1u16);
u64::from(1u16);
i64::from(1i32);
i64::from(1u32);
u64::from(1u32);
let _ = i16::from(1i8);
let _ = i32::from(1i8);
let _ = i64::from(1i8);
let _ = i16::from(1u8);
let _ = i32::from(1u8);
let _ = i64::from(1u8);
let _ = u16::from(1u8);
let _ = u32::from(1u8);
let _ = u64::from(1u8);
let _ = i32::from(1i16);
let _ = i64::from(1i16);
let _ = i32::from(1u16);
let _ = i64::from(1u16);
let _ = u32::from(1u16);
let _ = u64::from(1u16);
let _ = i64::from(1i32);
let _ = i64::from(1u32);
let _ = u64::from(1u32);

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

// The lint would suggest using `f64::from(input)` here but the `XX::from` function is not const,
Expand Down
38 changes: 19 additions & 19 deletions src/tools/clippy/tests/ui/cast_lossless_integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@

fn main() {
// Test clippy::cast_lossless with casts to integer types
1i8 as i16;
1i8 as i32;
1i8 as i64;
1u8 as i16;
1u8 as i32;
1u8 as i64;
1u8 as u16;
1u8 as u32;
1u8 as u64;
1i16 as i32;
1i16 as i64;
1u16 as i32;
1u16 as i64;
1u16 as u32;
1u16 as u64;
1i32 as i64;
1u32 as i64;
1u32 as u64;
let _ = 1i8 as i16;
let _ = 1i8 as i32;
let _ = 1i8 as i64;
let _ = 1u8 as i16;
let _ = 1u8 as i32;
let _ = 1u8 as i64;
let _ = 1u8 as u16;
let _ = 1u8 as u32;
let _ = 1u8 as u64;
let _ = 1i16 as i32;
let _ = 1i16 as i64;
let _ = 1u16 as i32;
let _ = 1u16 as i64;
let _ = 1u16 as u32;
let _ = 1u16 as u64;
let _ = 1i32 as i64;
let _ = 1u32 as i64;
let _ = 1u32 as u64;

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

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