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

This commit is contained in:
John Kugelman 2021-10-11 10:13:50 -04:00
parent 86d6d2b738
commit f9692b5619
9 changed files with 154 additions and 151 deletions

View File

@ -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;
}
@ -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;
}

View File

@ -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`

View File

@ -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

View File

@ -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,

View File

@ -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,

View File

@ -1,69 +1,69 @@
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
LL | let _ = 1.0f32 as f64;
| ^^^^^^^^^^^^^ help: try: `f64::from(1.0f32)`
error: aborting due to 11 previous errors

View File

@ -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,

View File

@ -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,

View File

@ -1,117 +1,117 @@
error: casting `i8` to `i16` may become silently lossy if you later change the type
--> $DIR/cast_lossless_integer.rs:8:5
--> $DIR/cast_lossless_integer.rs:8:13
|
LL | 1i8 as i16;
LL | let _ = 1i8 as i16;
| ^^^^^^^^^^ help: try: `i16::from(1i8)`
|
= note: `-D clippy::cast-lossless` implied by `-D warnings`
error: casting `i8` to `i32` may become silently lossy if you later change the type
--> $DIR/cast_lossless_integer.rs:9:5
--> $DIR/cast_lossless_integer.rs:9:13
|
LL | 1i8 as i32;
LL | let _ = 1i8 as i32;
| ^^^^^^^^^^ help: try: `i32::from(1i8)`
error: casting `i8` to `i64` may become silently lossy if you later change the type
--> $DIR/cast_lossless_integer.rs:10:5
--> $DIR/cast_lossless_integer.rs:10:13
|
LL | 1i8 as i64;
LL | let _ = 1i8 as i64;
| ^^^^^^^^^^ help: try: `i64::from(1i8)`
error: casting `u8` to `i16` may become silently lossy if you later change the type
--> $DIR/cast_lossless_integer.rs:11:5
--> $DIR/cast_lossless_integer.rs:11:13
|
LL | 1u8 as i16;
LL | let _ = 1u8 as i16;
| ^^^^^^^^^^ help: try: `i16::from(1u8)`
error: casting `u8` to `i32` may become silently lossy if you later change the type
--> $DIR/cast_lossless_integer.rs:12:5
--> $DIR/cast_lossless_integer.rs:12:13
|
LL | 1u8 as i32;
LL | let _ = 1u8 as i32;
| ^^^^^^^^^^ help: try: `i32::from(1u8)`
error: casting `u8` to `i64` may become silently lossy if you later change the type
--> $DIR/cast_lossless_integer.rs:13:5
--> $DIR/cast_lossless_integer.rs:13:13
|
LL | 1u8 as i64;
LL | let _ = 1u8 as i64;
| ^^^^^^^^^^ help: try: `i64::from(1u8)`
error: casting `u8` to `u16` may become silently lossy if you later change the type
--> $DIR/cast_lossless_integer.rs:14:5
--> $DIR/cast_lossless_integer.rs:14:13
|
LL | 1u8 as u16;
LL | let _ = 1u8 as u16;
| ^^^^^^^^^^ help: try: `u16::from(1u8)`
error: casting `u8` to `u32` may become silently lossy if you later change the type
--> $DIR/cast_lossless_integer.rs:15:5
--> $DIR/cast_lossless_integer.rs:15:13
|
LL | 1u8 as u32;
LL | let _ = 1u8 as u32;
| ^^^^^^^^^^ help: try: `u32::from(1u8)`
error: casting `u8` to `u64` may become silently lossy if you later change the type
--> $DIR/cast_lossless_integer.rs:16:5
--> $DIR/cast_lossless_integer.rs:16:13
|
LL | 1u8 as u64;
LL | let _ = 1u8 as u64;
| ^^^^^^^^^^ help: try: `u64::from(1u8)`
error: casting `i16` to `i32` may become silently lossy if you later change the type
--> $DIR/cast_lossless_integer.rs:17:5
--> $DIR/cast_lossless_integer.rs:17:13
|
LL | 1i16 as i32;
LL | let _ = 1i16 as i32;
| ^^^^^^^^^^^ help: try: `i32::from(1i16)`
error: casting `i16` to `i64` may become silently lossy if you later change the type
--> $DIR/cast_lossless_integer.rs:18:5
--> $DIR/cast_lossless_integer.rs:18:13
|
LL | 1i16 as i64;
LL | let _ = 1i16 as i64;
| ^^^^^^^^^^^ help: try: `i64::from(1i16)`
error: casting `u16` to `i32` may become silently lossy if you later change the type
--> $DIR/cast_lossless_integer.rs:19:5
--> $DIR/cast_lossless_integer.rs:19:13
|
LL | 1u16 as i32;
LL | let _ = 1u16 as i32;
| ^^^^^^^^^^^ help: try: `i32::from(1u16)`
error: casting `u16` to `i64` may become silently lossy if you later change the type
--> $DIR/cast_lossless_integer.rs:20:5
--> $DIR/cast_lossless_integer.rs:20:13
|
LL | 1u16 as i64;
LL | let _ = 1u16 as i64;
| ^^^^^^^^^^^ help: try: `i64::from(1u16)`
error: casting `u16` to `u32` may become silently lossy if you later change the type
--> $DIR/cast_lossless_integer.rs:21:5
--> $DIR/cast_lossless_integer.rs:21:13
|
LL | 1u16 as u32;
LL | let _ = 1u16 as u32;
| ^^^^^^^^^^^ help: try: `u32::from(1u16)`
error: casting `u16` to `u64` may become silently lossy if you later change the type
--> $DIR/cast_lossless_integer.rs:22:5
--> $DIR/cast_lossless_integer.rs:22:13
|
LL | 1u16 as u64;
LL | let _ = 1u16 as u64;
| ^^^^^^^^^^^ help: try: `u64::from(1u16)`
error: casting `i32` to `i64` may become silently lossy if you later change the type
--> $DIR/cast_lossless_integer.rs:23:5
--> $DIR/cast_lossless_integer.rs:23:13
|
LL | 1i32 as i64;
LL | let _ = 1i32 as i64;
| ^^^^^^^^^^^ help: try: `i64::from(1i32)`
error: casting `u32` to `i64` may become silently lossy if you later change the type
--> $DIR/cast_lossless_integer.rs:24:5
--> $DIR/cast_lossless_integer.rs:24:13
|
LL | 1u32 as i64;
LL | let _ = 1u32 as i64;
| ^^^^^^^^^^^ help: try: `i64::from(1u32)`
error: casting `u32` to `u64` may become silently lossy if you later change the type
--> $DIR/cast_lossless_integer.rs:25:5
--> $DIR/cast_lossless_integer.rs:25:13
|
LL | 1u32 as u64;
LL | let _ = 1u32 as u64;
| ^^^^^^^^^^^ help: try: `u64::from(1u32)`
error: casting `u8` to `u16` may become silently lossy if you later change the type
--> $DIR/cast_lossless_integer.rs:28:5
--> $DIR/cast_lossless_integer.rs:28:13
|
LL | (1u8 + 1u8) as u16;
LL | let _ = (1u8 + 1u8) as u16;
| ^^^^^^^^^^^^^^^^^^ help: try: `u16::from(1u8 + 1u8)`
error: aborting due to 19 previous errors