Remove/move comments to prevent weird rustfmt wrapping

This commit is contained in:
Guillaume Gomez 2023-07-28 20:40:44 +02:00
parent 9c96605b20
commit 3a31c05578
49 changed files with 863 additions and 620 deletions

View file

@ -6,23 +6,31 @@
fn main() {
let x = vec![0_u8; 16];
let _ = x.iter().filter(|&&a| a == 0).count(); // naive byte count
// naive byte count
let _ = x.iter().filter(|&&a| a == 0).count();
let _ = (&x[..]).iter().filter(|&a| *a == 0).count(); // naive byte count
// naive byte count
let _ = (&x[..]).iter().filter(|&a| *a == 0).count();
let _ = x.iter().filter(|a| **a > 0).count(); // not an equality count, OK.
// not an equality count, OK.
let _ = x.iter().filter(|a| **a > 0).count();
let _ = x.iter().map(|a| a + 1).filter(|&a| a < 15).count(); // not a slice
// not a slice
let _ = x.iter().map(|a| a + 1).filter(|&a| a < 15).count();
let b = 0;
let _ = x.iter().filter(|_| b > 0).count(); // woah there
// woah there
let _ = x.iter().filter(|_| b > 0).count();
let _ = x.iter().filter(|_a| b == b + 1).count(); // nothing to see here, move along
// nothing to see here, move along
let _ = x.iter().filter(|_a| b == b + 1).count();
let _ = x.iter().filter(|a| b + 1 == **a).count(); // naive byte count
// naive byte count
let _ = x.iter().filter(|a| b + 1 == **a).count();
let y = vec![0_u16; 3];
let _ = y.iter().filter(|&&a| a == 0).count(); // naive count, but not bytes
// naive count, but not bytes
let _ = y.iter().filter(|&&a| a == 0).count();
}

View file

@ -1,7 +1,7 @@
error: you appear to be counting bytes the naive way
--> $DIR/bytecount.rs:9:13
--> $DIR/bytecount.rs:10:13
|
LL | let _ = x.iter().filter(|&&a| a == 0).count(); // naive byte count
LL | let _ = x.iter().filter(|&&a| a == 0).count();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using the bytecount crate: `bytecount::count(x, 0)`
|
note: the lint level is defined here
@ -11,15 +11,15 @@ LL | #[deny(clippy::naive_bytecount)]
| ^^^^^^^^^^^^^^^^^^^^^^^
error: you appear to be counting bytes the naive way
--> $DIR/bytecount.rs:11:13
--> $DIR/bytecount.rs:13:13
|
LL | let _ = (&x[..]).iter().filter(|&a| *a == 0).count(); // naive byte count
LL | let _ = (&x[..]).iter().filter(|&a| *a == 0).count();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using the bytecount crate: `bytecount::count((&x[..]), 0)`
error: you appear to be counting bytes the naive way
--> $DIR/bytecount.rs:23:13
--> $DIR/bytecount.rs:30:13
|
LL | let _ = x.iter().filter(|a| b + 1 == **a).count(); // naive byte count
LL | let _ = x.iter().filter(|a| b + 1 == **a).count();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using the bytecount crate: `bytecount::count(x, b + 1)`
error: aborting due to 3 previous errors

View file

@ -43,14 +43,22 @@ fn main() {
1u32 as i32;
1u64 as i64;
1usize as isize;
1usize as i8; // should not wrap, usize is never 8 bits
1usize as i16; // wraps on 16 bit ptr size
1usize as i32; // wraps on 32 bit ptr size
1usize as i64; // wraps on 64 bit ptr size
1u8 as isize; // should not wrap, isize is never 8 bits
1u16 as isize; // wraps on 16 bit ptr size
1u32 as isize; // wraps on 32 bit ptr size
1u64 as isize; // wraps on 64 bit ptr size
// should not wrap, usize is never 8 bits
1usize as i8;
// wraps on 16 bit ptr size
1usize as i16;
// wraps on 32 bit ptr size
1usize as i32;
// wraps on 64 bit ptr size
1usize as i64;
// should not wrap, isize is never 8 bits
1u8 as isize;
// wraps on 16 bit ptr size
1u16 as isize;
// wraps on 32 bit ptr size
1u32 as isize;
// wraps on 64 bit ptr size
1u64 as isize;
// Test clippy::cast_sign_loss
1i32 as u32;
-1i32 as u32;
@ -122,7 +130,8 @@ fn main() {
let _ = s as i32;
// Test for signed min
(-99999999999i64).min(1) as i8; // should be linted because signed
// should be linted because signed
(-99999999999i64).min(1) as i8;
// Test for various operations that remove enough bits for the result to fit
(999999u64 & 1) as u8;
@ -134,7 +143,8 @@ fn main() {
x.min(1)
}) as u8;
999999u64.clamp(0, 255) as u8;
999999u64.clamp(0, 256) as u8; // should still be linted
// should still be linted
999999u64.clamp(0, 256) as u8;
#[derive(Clone, Copy)]
enum E1 {
@ -144,7 +154,8 @@ fn main() {
}
impl E1 {
fn test(self) {
let _ = self as u8; // Don't lint. `0..=2` fits in u8
// Don't lint. `0..=2` fits in u8
let _ = self as u8;
}
}
@ -157,8 +168,10 @@ fn main() {
fn test(self) {
let _ = self as u8;
let _ = Self::B as u8;
let _ = self as i16; // Don't lint. `255..=256` fits in i16
let _ = Self::A as u8; // Don't lint.
// Don't lint. `255..=256` fits in i16
let _ = self as i16;
// Don't lint.
let _ = Self::A as u8;
}
}
@ -170,7 +183,8 @@ fn main() {
}
impl E3 {
fn test(self) {
let _ = self as i8; // Don't lint. `-1..=50` fits in i8
// Don't lint. `-1..=50` fits in i8
let _ = self as i8;
}
}
@ -181,7 +195,8 @@ fn main() {
}
impl E4 {
fn test(self) {
let _ = self as i8; // Don't lint. `-128..=-127` fits in i8
// Don't lint. `-128..=-127` fits in i8
let _ = self as i8;
}
}
@ -194,8 +209,10 @@ fn main() {
fn test(self) {
let _ = self as i8;
let _ = Self::A as i8;
let _ = self as i16; // Don't lint. `-129..=127` fits in i16
let _ = Self::B as u8; // Don't lint.
// Don't lint. `-129..=127` fits in i16
let _ = self as i16;
// Don't lint.
let _ = Self::B as u8;
}
}
@ -208,9 +225,12 @@ fn main() {
impl E6 {
fn test(self) {
let _ = self as i16;
let _ = Self::A as u16; // Don't lint. `2^16-1` fits in u16
let _ = self as u32; // Don't lint. `2^16-1..=2^16` fits in u32
let _ = Self::A as u16; // Don't lint.
// Don't lint. `2^16-1` fits in u16
let _ = Self::A as u16;
// Don't lint. `2^16-1..=2^16` fits in u32
let _ = self as u32;
// Don't lint.
let _ = Self::A as u16;
}
}
@ -223,8 +243,10 @@ fn main() {
impl E7 {
fn test(self) {
let _ = self as usize;
let _ = Self::A as usize; // Don't lint.
let _ = self as u64; // Don't lint. `2^32-1..=2^32` fits in u64
// Don't lint.
let _ = Self::A as usize;
// Don't lint. `2^32-1..=2^32` fits in u64
let _ = self as u64;
}
}
@ -238,7 +260,8 @@ fn main() {
}
impl E8 {
fn test(self) {
let _ = self as i128; // Don't lint. `-(2^127)..=2^127-1` fits it i128
// Don't lint. `-(2^127)..=2^127-1` fits it i128
let _ = self as i128;
}
}
@ -250,8 +273,10 @@ fn main() {
}
impl E9 {
fn test(self) {
let _ = Self::A as u8; // Don't lint.
let _ = self as u128; // Don't lint. `0..=2^128-1` fits in u128
// Don't lint.
let _ = Self::A as u8;
// Don't lint. `0..=2^128-1` fits in u128
let _ = self as u128;
}
}
@ -264,8 +289,10 @@ fn main() {
impl E10 {
fn test(self) {
let _ = self as u16;
let _ = Self::B as u32; // Don't lint.
let _ = self as u64; // Don't lint.
// Don't lint.
let _ = Self::B as u32;
// Don't lint.
let _ = self as u64;
}
}
}

View file

@ -216,133 +216,133 @@ LL | 1usize as isize;
| ^^^^^^^^^^^^^^^
error: casting `usize` to `i8` may truncate the value
--> $DIR/cast.rs:46:5
--> $DIR/cast.rs:47:5
|
LL | 1usize as i8; // should not wrap, usize is never 8 bits
LL | 1usize as i8;
| ^^^^^^^^^^^^
|
= help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...
help: ... or use `try_from` and handle the error accordingly
|
LL | i8::try_from(1usize); // should not wrap, usize is never 8 bits
LL | i8::try_from(1usize);
| ~~~~~~~~~~~~~~~~~~~~
error: casting `usize` to `i16` may truncate the value
--> $DIR/cast.rs:47:5
--> $DIR/cast.rs:49:5
|
LL | 1usize as i16; // wraps on 16 bit ptr size
LL | 1usize as i16;
| ^^^^^^^^^^^^^
|
= help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...
help: ... or use `try_from` and handle the error accordingly
|
LL | i16::try_from(1usize); // wraps on 16 bit ptr size
LL | i16::try_from(1usize);
| ~~~~~~~~~~~~~~~~~~~~~
error: casting `usize` to `i16` may wrap around the value on targets with 16-bit wide pointers
--> $DIR/cast.rs:47:5
--> $DIR/cast.rs:49:5
|
LL | 1usize as i16; // wraps on 16 bit ptr size
LL | 1usize as i16;
| ^^^^^^^^^^^^^
|
= note: `usize` and `isize` may be as small as 16 bits on some platforms
= note: for more information see https://doc.rust-lang.org/reference/types/numeric.html#machine-dependent-integer-types
error: casting `usize` to `i32` may truncate the value on targets with 64-bit wide pointers
--> $DIR/cast.rs:48:5
--> $DIR/cast.rs:51:5
|
LL | 1usize as i32; // wraps on 32 bit ptr size
LL | 1usize as i32;
| ^^^^^^^^^^^^^
|
= help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...
help: ... or use `try_from` and handle the error accordingly
|
LL | i32::try_from(1usize); // wraps on 32 bit ptr size
LL | i32::try_from(1usize);
| ~~~~~~~~~~~~~~~~~~~~~
error: casting `usize` to `i32` may wrap around the value on targets with 32-bit wide pointers
--> $DIR/cast.rs:48:5
--> $DIR/cast.rs:51:5
|
LL | 1usize as i32; // wraps on 32 bit ptr size
LL | 1usize as i32;
| ^^^^^^^^^^^^^
error: casting `usize` to `i64` may wrap around the value on targets with 64-bit wide pointers
--> $DIR/cast.rs:49:5
--> $DIR/cast.rs:53:5
|
LL | 1usize as i64; // wraps on 64 bit ptr size
LL | 1usize as i64;
| ^^^^^^^^^^^^^
error: casting `u16` to `isize` may wrap around the value on targets with 16-bit wide pointers
--> $DIR/cast.rs:51:5
--> $DIR/cast.rs:57:5
|
LL | 1u16 as isize; // wraps on 16 bit ptr size
LL | 1u16 as isize;
| ^^^^^^^^^^^^^
|
= note: `usize` and `isize` may be as small as 16 bits on some platforms
= note: for more information see https://doc.rust-lang.org/reference/types/numeric.html#machine-dependent-integer-types
error: casting `u32` to `isize` may wrap around the value on targets with 32-bit wide pointers
--> $DIR/cast.rs:52:5
--> $DIR/cast.rs:59:5
|
LL | 1u32 as isize; // wraps on 32 bit ptr size
LL | 1u32 as isize;
| ^^^^^^^^^^^^^
error: casting `u64` to `isize` may truncate the value on targets with 32-bit wide pointers
--> $DIR/cast.rs:53:5
--> $DIR/cast.rs:61:5
|
LL | 1u64 as isize; // wraps on 64 bit ptr size
LL | 1u64 as isize;
| ^^^^^^^^^^^^^
|
= help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...
help: ... or use `try_from` and handle the error accordingly
|
LL | isize::try_from(1u64); // wraps on 64 bit ptr size
LL | isize::try_from(1u64);
| ~~~~~~~~~~~~~~~~~~~~~
error: casting `u64` to `isize` may wrap around the value on targets with 64-bit wide pointers
--> $DIR/cast.rs:53:5
--> $DIR/cast.rs:61:5
|
LL | 1u64 as isize; // wraps on 64 bit ptr size
LL | 1u64 as isize;
| ^^^^^^^^^^^^^
error: casting `i32` to `u32` may lose the sign of the value
--> $DIR/cast.rs:56:5
--> $DIR/cast.rs:64:5
|
LL | -1i32 as u32;
| ^^^^^^^^^^^^
error: casting `isize` to `usize` may lose the sign of the value
--> $DIR/cast.rs:58:5
--> $DIR/cast.rs:66:5
|
LL | -1isize as usize;
| ^^^^^^^^^^^^^^^^
error: casting `i64` to `i8` may truncate the value
--> $DIR/cast.rs:125:5
--> $DIR/cast.rs:134:5
|
LL | (-99999999999i64).min(1) as i8; // should be linted because signed
LL | (-99999999999i64).min(1) as i8;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...
help: ... or use `try_from` and handle the error accordingly
|
LL | i8::try_from((-99999999999i64).min(1)); // should be linted because signed
LL | i8::try_from((-99999999999i64).min(1));
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: casting `u64` to `u8` may truncate the value
--> $DIR/cast.rs:137:5
--> $DIR/cast.rs:147:5
|
LL | 999999u64.clamp(0, 256) as u8; // should still be linted
LL | 999999u64.clamp(0, 256) as u8;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...
help: ... or use `try_from` and handle the error accordingly
|
LL | u8::try_from(999999u64.clamp(0, 256)); // should still be linted
LL | u8::try_from(999999u64.clamp(0, 256));
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: casting `main::E2` to `u8` may truncate the value
--> $DIR/cast.rs:158:21
--> $DIR/cast.rs:169:21
|
LL | let _ = self as u8;
| ^^^^^^^^^^
@ -354,7 +354,7 @@ LL | let _ = u8::try_from(self);
| ~~~~~~~~~~~~~~~~~~
error: casting `main::E2::B` to `u8` will truncate the value
--> $DIR/cast.rs:159:21
--> $DIR/cast.rs:170:21
|
LL | let _ = Self::B as u8;
| ^^^^^^^^^^^^^
@ -362,7 +362,7 @@ LL | let _ = Self::B as u8;
= note: `-D clippy::cast-enum-truncation` implied by `-D warnings`
error: casting `main::E5` to `i8` may truncate the value
--> $DIR/cast.rs:195:21
--> $DIR/cast.rs:210:21
|
LL | let _ = self as i8;
| ^^^^^^^^^^
@ -374,13 +374,13 @@ LL | let _ = i8::try_from(self);
| ~~~~~~~~~~~~~~~~~~
error: casting `main::E5::A` to `i8` will truncate the value
--> $DIR/cast.rs:196:21
--> $DIR/cast.rs:211:21
|
LL | let _ = Self::A as i8;
| ^^^^^^^^^^^^^
error: casting `main::E6` to `i16` may truncate the value
--> $DIR/cast.rs:210:21
--> $DIR/cast.rs:227:21
|
LL | let _ = self as i16;
| ^^^^^^^^^^^
@ -392,7 +392,7 @@ LL | let _ = i16::try_from(self);
| ~~~~~~~~~~~~~~~~~~~
error: casting `main::E7` to `usize` may truncate the value on targets with 32-bit wide pointers
--> $DIR/cast.rs:225:21
--> $DIR/cast.rs:245:21
|
LL | let _ = self as usize;
| ^^^^^^^^^^^^^
@ -404,7 +404,7 @@ LL | let _ = usize::try_from(self);
| ~~~~~~~~~~~~~~~~~~~~~
error: casting `main::E10` to `u16` may truncate the value
--> $DIR/cast.rs:266:21
--> $DIR/cast.rs:291:21
|
LL | let _ = self as u16;
| ^^^^^^^^^^^
@ -416,7 +416,7 @@ LL | let _ = u16::try_from(self);
| ~~~~~~~~~~~~~~~~~~~
error: casting `u32` to `u8` may truncate the value
--> $DIR/cast.rs:274:13
--> $DIR/cast.rs:301:13
|
LL | let c = (q >> 16) as u8;
| ^^^^^^^^^^^^^^^
@ -428,7 +428,7 @@ LL | let c = u8::try_from(q >> 16);
| ~~~~~~~~~~~~~~~~~~~~~
error: casting `u32` to `u8` may truncate the value
--> $DIR/cast.rs:277:13
--> $DIR/cast.rs:304:13
|
LL | let c = (q / 1000) as u8;
| ^^^^^^^^^^^^^^^^

View file

@ -1,5 +1,6 @@
#![warn(clippy::char_lit_as_u8)]
fn main() {
let _ = '❤' as u8; // no suggestion, since a byte literal won't work.
// no suggestion, since a byte literal won't work.
let _ = '❤' as u8;
}

View file

@ -1,7 +1,7 @@
error: casting a character literal to `u8` truncates
--> $DIR/char_lit_as_u8.rs:4:13
--> $DIR/char_lit_as_u8.rs:5:13
|
LL | let _ = '❤' as u8; // no suggestion, since a byte literal won't work.
LL | let _ = '❤' as u8;
| ^^^^^^^^^
|
= note: `char` is four bytes wide, but `u8` is a single byte

View file

@ -9,10 +9,14 @@ fn test_complex_conditions() {
let x: Result<(), ()> = Ok(());
let y: Result<(), ()> = Ok(());
if x.is_ok() && y.is_err() {
x.unwrap(); // unnecessary
x.unwrap_err(); // will panic
y.unwrap(); // will panic
y.unwrap_err(); // unnecessary
// unnecessary
x.unwrap();
// will panic
x.unwrap_err();
// will panic
y.unwrap();
// unnecessary
y.unwrap_err();
} else {
// not statically determinable whether any of the following will always succeed or always fail:
x.unwrap();
@ -26,19 +30,29 @@ fn test_complex_conditions() {
x.unwrap();
y.unwrap();
} else {
x.unwrap(); // will panic
x.unwrap_err(); // unnecessary
y.unwrap(); // will panic
y.unwrap_err(); // unnecessary
// will panic
x.unwrap();
// unnecessary
x.unwrap_err();
// will panic
y.unwrap();
// unnecessary
y.unwrap_err();
}
let z: Result<(), ()> = Ok(());
if x.is_ok() && !(y.is_ok() || z.is_err()) {
x.unwrap(); // unnecessary
x.unwrap_err(); // will panic
y.unwrap(); // will panic
y.unwrap_err(); // unnecessary
z.unwrap(); // unnecessary
z.unwrap_err(); // will panic
// unnecessary
x.unwrap();
// will panic
x.unwrap_err();
// will panic
y.unwrap();
// unnecessary
y.unwrap_err();
// unnecessary
z.unwrap();
// will panic
z.unwrap_err();
}
if x.is_ok() || !(y.is_ok() && z.is_err()) {
// not statically determinable whether any of the following will always succeed or always fail:
@ -46,12 +60,18 @@ fn test_complex_conditions() {
y.unwrap();
z.unwrap();
} else {
x.unwrap(); // will panic
x.unwrap_err(); // unnecessary
y.unwrap(); // unnecessary
y.unwrap_err(); // will panic
z.unwrap(); // will panic
z.unwrap_err(); // unnecessary
// will panic
x.unwrap();
// unnecessary
x.unwrap_err();
// unnecessary
y.unwrap();
// will panic
y.unwrap_err();
// will panic
z.unwrap();
// unnecessary
z.unwrap_err();
}
}

View file

@ -1,9 +1,10 @@
error: called `unwrap` on `x` after checking its variant with `is_ok`
--> $DIR/complex_conditionals.rs:12:9
--> $DIR/complex_conditionals.rs:13:9
|
LL | if x.is_ok() && y.is_err() {
| --------- the check is happening here
LL | x.unwrap(); // unnecessary
LL | // unnecessary
LL | x.unwrap();
| ^^^^^^^^^^
|
= help: try using `if let` or `match`
@ -14,12 +15,12 @@ LL | #![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
error: this call to `unwrap_err()` will always panic
--> $DIR/complex_conditionals.rs:13:9
--> $DIR/complex_conditionals.rs:15:9
|
LL | if x.is_ok() && y.is_err() {
| --------- because of this check
LL | x.unwrap(); // unnecessary
LL | x.unwrap_err(); // will panic
...
LL | x.unwrap_err();
| ^^^^^^^^^^^^^^
|
note: the lint level is defined here
@ -29,180 +30,181 @@ LL | #![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)]
| ^^^^^^^^^^^^^^^^^^^^^^^^
error: this call to `unwrap()` will always panic
--> $DIR/complex_conditionals.rs:14:9
--> $DIR/complex_conditionals.rs:17:9
|
LL | if x.is_ok() && y.is_err() {
| ---------- because of this check
...
LL | y.unwrap(); // will panic
LL | y.unwrap();
| ^^^^^^^^^^
error: called `unwrap_err` on `y` after checking its variant with `is_err`
--> $DIR/complex_conditionals.rs:15:9
--> $DIR/complex_conditionals.rs:19:9
|
LL | if x.is_ok() && y.is_err() {
| ---------- the check is happening here
...
LL | y.unwrap_err(); // unnecessary
LL | y.unwrap_err();
| ^^^^^^^^^^^^^^
|
= help: try using `if let` or `match`
error: this call to `unwrap()` will always panic
--> $DIR/complex_conditionals.rs:29:9
--> $DIR/complex_conditionals.rs:34:9
|
LL | if x.is_ok() || y.is_ok() {
| --------- because of this check
...
LL | x.unwrap(); // will panic
LL | x.unwrap();
| ^^^^^^^^^^
error: called `unwrap_err` on `x` after checking its variant with `is_ok`
--> $DIR/complex_conditionals.rs:30:9
--> $DIR/complex_conditionals.rs:36:9
|
LL | if x.is_ok() || y.is_ok() {
| --------- the check is happening here
...
LL | x.unwrap_err(); // unnecessary
LL | x.unwrap_err();
| ^^^^^^^^^^^^^^
|
= help: try using `if let` or `match`
error: this call to `unwrap()` will always panic
--> $DIR/complex_conditionals.rs:31:9
--> $DIR/complex_conditionals.rs:38:9
|
LL | if x.is_ok() || y.is_ok() {
| --------- because of this check
...
LL | y.unwrap(); // will panic
LL | y.unwrap();
| ^^^^^^^^^^
error: called `unwrap_err` on `y` after checking its variant with `is_ok`
--> $DIR/complex_conditionals.rs:32:9
--> $DIR/complex_conditionals.rs:40:9
|
LL | if x.is_ok() || y.is_ok() {
| --------- the check is happening here
...
LL | y.unwrap_err(); // unnecessary
LL | y.unwrap_err();
| ^^^^^^^^^^^^^^
|
= help: try using `if let` or `match`
error: called `unwrap` on `x` after checking its variant with `is_ok`
--> $DIR/complex_conditionals.rs:36:9
--> $DIR/complex_conditionals.rs:45:9
|
LL | if x.is_ok() && !(y.is_ok() || z.is_err()) {
| --------- the check is happening here
LL | x.unwrap(); // unnecessary
LL | // unnecessary
LL | x.unwrap();
| ^^^^^^^^^^
|
= help: try using `if let` or `match`
error: this call to `unwrap_err()` will always panic
--> $DIR/complex_conditionals.rs:37:9
--> $DIR/complex_conditionals.rs:47:9
|
LL | if x.is_ok() && !(y.is_ok() || z.is_err()) {
| --------- because of this check
LL | x.unwrap(); // unnecessary
LL | x.unwrap_err(); // will panic
| ^^^^^^^^^^^^^^
error: this call to `unwrap()` will always panic
--> $DIR/complex_conditionals.rs:38:9
|
LL | if x.is_ok() && !(y.is_ok() || z.is_err()) {
| --------- because of this check
...
LL | y.unwrap(); // will panic
| ^^^^^^^^^^
error: called `unwrap_err` on `y` after checking its variant with `is_ok`
--> $DIR/complex_conditionals.rs:39:9
|
LL | if x.is_ok() && !(y.is_ok() || z.is_err()) {
| --------- the check is happening here
...
LL | y.unwrap_err(); // unnecessary
| ^^^^^^^^^^^^^^
|
= help: try using `if let` or `match`
error: called `unwrap` on `z` after checking its variant with `is_err`
--> $DIR/complex_conditionals.rs:40:9
|
LL | if x.is_ok() && !(y.is_ok() || z.is_err()) {
| ---------- the check is happening here
...
LL | z.unwrap(); // unnecessary
| ^^^^^^^^^^
|
= help: try using `if let` or `match`
error: this call to `unwrap_err()` will always panic
--> $DIR/complex_conditionals.rs:41:9
|
LL | if x.is_ok() && !(y.is_ok() || z.is_err()) {
| ---------- because of this check
...
LL | z.unwrap_err(); // will panic
LL | x.unwrap_err();
| ^^^^^^^^^^^^^^
error: this call to `unwrap()` will always panic
--> $DIR/complex_conditionals.rs:49:9
|
LL | if x.is_ok() || !(y.is_ok() && z.is_err()) {
| --------- because of this check
LL | if x.is_ok() && !(y.is_ok() || z.is_err()) {
| --------- because of this check
...
LL | x.unwrap(); // will panic
LL | y.unwrap();
| ^^^^^^^^^^
error: called `unwrap_err` on `x` after checking its variant with `is_ok`
--> $DIR/complex_conditionals.rs:50:9
error: called `unwrap_err` on `y` after checking its variant with `is_ok`
--> $DIR/complex_conditionals.rs:51:9
|
LL | if x.is_ok() || !(y.is_ok() && z.is_err()) {
| --------- the check is happening here
LL | if x.is_ok() && !(y.is_ok() || z.is_err()) {
| --------- the check is happening here
...
LL | x.unwrap_err(); // unnecessary
LL | y.unwrap_err();
| ^^^^^^^^^^^^^^
|
= help: try using `if let` or `match`
error: called `unwrap` on `y` after checking its variant with `is_ok`
--> $DIR/complex_conditionals.rs:51:9
error: called `unwrap` on `z` after checking its variant with `is_err`
--> $DIR/complex_conditionals.rs:53:9
|
LL | if x.is_ok() || !(y.is_ok() && z.is_err()) {
| --------- the check is happening here
LL | if x.is_ok() && !(y.is_ok() || z.is_err()) {
| ---------- the check is happening here
...
LL | y.unwrap(); // unnecessary
LL | z.unwrap();
| ^^^^^^^^^^
|
= help: try using `if let` or `match`
error: this call to `unwrap_err()` will always panic
--> $DIR/complex_conditionals.rs:52:9
--> $DIR/complex_conditionals.rs:55:9
|
LL | if x.is_ok() && !(y.is_ok() || z.is_err()) {
| ---------- because of this check
...
LL | z.unwrap_err();
| ^^^^^^^^^^^^^^
error: this call to `unwrap()` will always panic
--> $DIR/complex_conditionals.rs:64:9
|
LL | if x.is_ok() || !(y.is_ok() && z.is_err()) {
| --------- because of this check
...
LL | x.unwrap();
| ^^^^^^^^^^
error: called `unwrap_err` on `x` after checking its variant with `is_ok`
--> $DIR/complex_conditionals.rs:66:9
|
LL | if x.is_ok() || !(y.is_ok() && z.is_err()) {
| --------- the check is happening here
...
LL | x.unwrap_err();
| ^^^^^^^^^^^^^^
|
= help: try using `if let` or `match`
error: called `unwrap` on `y` after checking its variant with `is_ok`
--> $DIR/complex_conditionals.rs:68:9
|
LL | if x.is_ok() || !(y.is_ok() && z.is_err()) {
| --------- the check is happening here
...
LL | y.unwrap();
| ^^^^^^^^^^
|
= help: try using `if let` or `match`
error: this call to `unwrap_err()` will always panic
--> $DIR/complex_conditionals.rs:70:9
|
LL | if x.is_ok() || !(y.is_ok() && z.is_err()) {
| --------- because of this check
...
LL | y.unwrap_err(); // will panic
LL | y.unwrap_err();
| ^^^^^^^^^^^^^^
error: this call to `unwrap()` will always panic
--> $DIR/complex_conditionals.rs:53:9
--> $DIR/complex_conditionals.rs:72:9
|
LL | if x.is_ok() || !(y.is_ok() && z.is_err()) {
| ---------- because of this check
...
LL | z.unwrap(); // will panic
LL | z.unwrap();
| ^^^^^^^^^^
error: called `unwrap_err` on `z` after checking its variant with `is_err`
--> $DIR/complex_conditionals.rs:54:9
--> $DIR/complex_conditionals.rs:74:9
|
LL | if x.is_ok() || !(y.is_ok() && z.is_err()) {
| ---------- the check is happening here
...
LL | z.unwrap_err(); // unnecessary
LL | z.unwrap_err();
| ^^^^^^^^^^^^^^
|
= help: try using `if let` or `match`

View file

@ -9,9 +9,11 @@ fn test_nested() {
fn nested() {
let x = Some(());
if x.is_some() {
x.unwrap(); // unnecessary
// unnecessary
x.unwrap();
} else {
x.unwrap(); // will panic
// will panic
x.unwrap();
}
}
}

View file

@ -1,9 +1,10 @@
error: called `unwrap` on `x` after checking its variant with `is_some`
--> $DIR/complex_conditionals_nested.rs:12:13
--> $DIR/complex_conditionals_nested.rs:13:13
|
LL | if x.is_some() {
| -------------- help: try: `if let Some(..) = x`
LL | x.unwrap(); // unnecessary
LL | // unnecessary
LL | x.unwrap();
| ^^^^^^^^^^
|
note: the lint level is defined here
@ -13,12 +14,12 @@ LL | #![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
error: this call to `unwrap()` will always panic
--> $DIR/complex_conditionals_nested.rs:14:13
--> $DIR/complex_conditionals_nested.rs:16:13
|
LL | if x.is_some() {
| ----------- because of this check
...
LL | x.unwrap(); // will panic
LL | x.unwrap();
| ^^^^^^^^^^
|
note: the lint level is defined here

View file

@ -10,7 +10,8 @@
macro_rules! m {
($a:expr) => {
if $a.is_some() {
$a.unwrap(); // unnecessary
// unnecessary
$a.unwrap();
}
};
}
@ -42,37 +43,56 @@ macro_rules! checks_some {
fn main() {
let x = Some(());
if x.is_some() {
x.unwrap(); // unnecessary
x.expect("an error message"); // unnecessary
// unnecessary
x.unwrap();
// unnecessary
x.expect("an error message");
} else {
x.unwrap(); // will panic
x.expect("an error message"); // will panic
// will panic
x.unwrap();
// will panic
x.expect("an error message");
}
if x.is_none() {
x.unwrap(); // will panic
// will panic
x.unwrap();
} else {
x.unwrap(); // unnecessary
// unnecessary
x.unwrap();
}
m!(x);
checks_in_param!(x.is_some(), x.unwrap()); // ok
checks_unwrap!(x, x.unwrap()); // ok
checks_some!(x.is_some(), x); // ok
// ok
checks_in_param!(x.is_some(), x.unwrap());
// ok
checks_unwrap!(x, x.unwrap());
// ok
checks_some!(x.is_some(), x);
let mut x: Result<(), ()> = Ok(());
if x.is_ok() {
x.unwrap(); // unnecessary
x.expect("an error message"); // unnecessary
x.unwrap_err(); // will panic
// unnecessary
x.unwrap();
// unnecessary
x.expect("an error message");
// will panic
x.unwrap_err();
} else {
x.unwrap(); // will panic
x.expect("an error message"); // will panic
x.unwrap_err(); // unnecessary
// will panic
x.unwrap();
// will panic
x.expect("an error message");
// unnecessary
x.unwrap_err();
}
if x.is_err() {
x.unwrap(); // will panic
x.unwrap_err(); // unnecessary
// will panic
x.unwrap();
// unnecessary
x.unwrap_err();
} else {
x.unwrap(); // unnecessary
x.unwrap_err(); // will panic
// unnecessary
x.unwrap();
// will panic
x.unwrap_err();
}
if x.is_ok() {
x = Err(());
@ -88,20 +108,25 @@ fn main() {
x.unwrap_err();
}
assert!(x.is_ok(), "{:?}", x.unwrap_err()); // ok, it's a common test pattern
// ok, it's a common test pattern
assert!(x.is_ok(), "{:?}", x.unwrap_err());
}
fn check_expect() {
let x = Some(());
if x.is_some() {
#[expect(clippy::unnecessary_unwrap)]
x.unwrap(); // unnecessary
// unnecessary
x.unwrap();
#[expect(clippy::unnecessary_unwrap)]
x.expect("an error message"); // unnecessary
// unnecessary
x.expect("an error message");
} else {
#[expect(clippy::panicking_unwrap)]
x.unwrap(); // will panic
// will panic
x.unwrap();
#[expect(clippy::panicking_unwrap)]
x.expect("an error message"); // will panic
// will panic
x.expect("an error message");
}
}

View file

@ -1,9 +1,10 @@
error: called `unwrap` on `x` after checking its variant with `is_some`
--> $DIR/simple_conditionals.rs:45:9
--> $DIR/simple_conditionals.rs:47:9
|
LL | if x.is_some() {
| -------------- help: try: `if let Some(..) = x`
LL | x.unwrap(); // unnecessary
LL | // unnecessary
LL | x.unwrap();
| ^^^^^^^^^^
|
note: the lint level is defined here
@ -13,21 +14,21 @@ LL | #![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
error: called `expect` on `x` after checking its variant with `is_some`
--> $DIR/simple_conditionals.rs:46:9
--> $DIR/simple_conditionals.rs:49:9
|
LL | if x.is_some() {
| -------------- help: try: `if let Some(..) = x`
LL | x.unwrap(); // unnecessary
LL | x.expect("an error message"); // unnecessary
...
LL | x.expect("an error message");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: this call to `unwrap()` will always panic
--> $DIR/simple_conditionals.rs:48:9
--> $DIR/simple_conditionals.rs:52:9
|
LL | if x.is_some() {
| ----------- because of this check
...
LL | x.unwrap(); // will panic
LL | x.unwrap();
| ^^^^^^^^^^
|
note: the lint level is defined here
@ -37,37 +38,39 @@ LL | #![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)]
| ^^^^^^^^^^^^^^^^^^^^^^^^
error: this call to `expect()` will always panic
--> $DIR/simple_conditionals.rs:49:9
--> $DIR/simple_conditionals.rs:54:9
|
LL | if x.is_some() {
| ----------- because of this check
...
LL | x.expect("an error message"); // will panic
LL | x.expect("an error message");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: this call to `unwrap()` will always panic
--> $DIR/simple_conditionals.rs:52:9
--> $DIR/simple_conditionals.rs:58:9
|
LL | if x.is_none() {
| ----------- because of this check
LL | x.unwrap(); // will panic
LL | // will panic
LL | x.unwrap();
| ^^^^^^^^^^
error: called `unwrap` on `x` after checking its variant with `is_none`
--> $DIR/simple_conditionals.rs:54:9
--> $DIR/simple_conditionals.rs:61:9
|
LL | if x.is_none() {
| -------------- help: try: `if let Some(..) = x`
...
LL | x.unwrap(); // unnecessary
LL | x.unwrap();
| ^^^^^^^^^^
error: called `unwrap` on `x` after checking its variant with `is_some`
--> $DIR/simple_conditionals.rs:13:13
--> $DIR/simple_conditionals.rs:14:13
|
LL | if $a.is_some() {
| --------------- help: try: `if let Some(..) = x`
LL | $a.unwrap(); // unnecessary
LL | // unnecessary
LL | $a.unwrap();
| ^^^^^^^^^^^
...
LL | m!(x);
@ -76,91 +79,93 @@ LL | m!(x);
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
error: called `unwrap` on `x` after checking its variant with `is_ok`
--> $DIR/simple_conditionals.rs:62:9
--> $DIR/simple_conditionals.rs:73:9
|
LL | if x.is_ok() {
| ------------ help: try: `if let Ok(..) = x`
LL | x.unwrap(); // unnecessary
LL | // unnecessary
LL | x.unwrap();
| ^^^^^^^^^^
error: called `expect` on `x` after checking its variant with `is_ok`
--> $DIR/simple_conditionals.rs:63:9
--> $DIR/simple_conditionals.rs:75:9
|
LL | if x.is_ok() {
| ------------ help: try: `if let Ok(..) = x`
LL | x.unwrap(); // unnecessary
LL | x.expect("an error message"); // unnecessary
...
LL | x.expect("an error message");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: this call to `unwrap_err()` will always panic
--> $DIR/simple_conditionals.rs:64:9
--> $DIR/simple_conditionals.rs:77:9
|
LL | if x.is_ok() {
| --------- because of this check
...
LL | x.unwrap_err(); // will panic
LL | x.unwrap_err();
| ^^^^^^^^^^^^^^
error: this call to `unwrap()` will always panic
--> $DIR/simple_conditionals.rs:66:9
--> $DIR/simple_conditionals.rs:80:9
|
LL | if x.is_ok() {
| --------- because of this check
...
LL | x.unwrap(); // will panic
LL | x.unwrap();
| ^^^^^^^^^^
error: this call to `expect()` will always panic
--> $DIR/simple_conditionals.rs:67:9
--> $DIR/simple_conditionals.rs:82:9
|
LL | if x.is_ok() {
| --------- because of this check
...
LL | x.expect("an error message"); // will panic
LL | x.expect("an error message");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: called `unwrap_err` on `x` after checking its variant with `is_ok`
--> $DIR/simple_conditionals.rs:68:9
--> $DIR/simple_conditionals.rs:84:9
|
LL | if x.is_ok() {
| ------------ help: try: `if let Err(..) = x`
...
LL | x.unwrap_err(); // unnecessary
LL | x.unwrap_err();
| ^^^^^^^^^^^^^^
error: this call to `unwrap()` will always panic
--> $DIR/simple_conditionals.rs:71:9
--> $DIR/simple_conditionals.rs:88:9
|
LL | if x.is_err() {
| ---------- because of this check
LL | x.unwrap(); // will panic
LL | // will panic
LL | x.unwrap();
| ^^^^^^^^^^
error: called `unwrap_err` on `x` after checking its variant with `is_err`
--> $DIR/simple_conditionals.rs:72:9
--> $DIR/simple_conditionals.rs:90:9
|
LL | if x.is_err() {
| ------------- help: try: `if let Err(..) = x`
LL | x.unwrap(); // will panic
LL | x.unwrap_err(); // unnecessary
...
LL | x.unwrap_err();
| ^^^^^^^^^^^^^^
error: called `unwrap` on `x` after checking its variant with `is_err`
--> $DIR/simple_conditionals.rs:74:9
--> $DIR/simple_conditionals.rs:93:9
|
LL | if x.is_err() {
| ------------- help: try: `if let Ok(..) = x`
...
LL | x.unwrap(); // unnecessary
LL | x.unwrap();
| ^^^^^^^^^^
error: this call to `unwrap_err()` will always panic
--> $DIR/simple_conditionals.rs:75:9
--> $DIR/simple_conditionals.rs:95:9
|
LL | if x.is_err() {
| ---------- because of this check
...
LL | x.unwrap_err(); // will panic
LL | x.unwrap_err();
| ^^^^^^^^^^^^^^
error: aborting due to 17 previous errors

View file

@ -18,7 +18,7 @@ fn no_access_at_all() {
fn write_without_read() {
// The main use case for `collection_is_never_read`.
let mut x = HashMap::new(); // WARNING
let mut x = HashMap::new();
x.insert(1, 2);
}
@ -57,7 +57,7 @@ fn read_in_closure() {
}
fn write_in_closure() {
let mut x = vec![1, 2, 3]; // WARNING
let mut x = vec![1, 2, 3];
let _ = || {
x.push(4);
};
@ -72,12 +72,12 @@ fn read_in_format() {
fn shadowing_1() {
let x = HashMap::<usize, usize>::new(); // Ok
let _ = x.len();
let mut x = HashMap::new(); // WARNING
let mut x = HashMap::new();
x.insert(1, 2);
}
fn shadowing_2() {
let mut x = HashMap::new(); // WARNING
let mut x = HashMap::new();
x.insert(1, 2);
let x = HashMap::<usize, usize>::new(); // Ok
let _ = x.len();
@ -85,26 +85,26 @@ fn shadowing_2() {
#[allow(clippy::let_unit_value)]
fn fake_read_1() {
let mut x = vec![1, 2, 3]; // WARNING
let mut x = vec![1, 2, 3];
x.reverse();
let _: () = x.clear();
}
fn fake_read_2() {
let mut x = vec![1, 2, 3]; // WARNING
let mut x = vec![1, 2, 3];
x.reverse();
println!("{:?}", x.push(5));
}
fn assignment() {
let mut x = vec![1, 2, 3]; // WARNING
let mut x = vec![1, 2, 3];
let y = vec![4, 5, 6]; // Ok
x = y;
}
#[allow(clippy::self_assignment)]
fn self_assignment() {
let mut x = vec![1, 2, 3]; // WARNING
let mut x = vec![1, 2, 3];
x = x;
}
@ -121,7 +121,7 @@ fn method_argument_but_not_target() {
}
fn insert_is_not_a_read() {
let mut x = HashSet::new(); // WARNING
let mut x = HashSet::new();
x.insert(5);
}
@ -135,7 +135,7 @@ fn insert_is_a_read() {
fn not_read_if_return_value_not_used() {
// `is_empty` does not modify the set, so it's a query. But since the return value is not used, the
// lint does not consider it a read here.
let x = vec![1, 2, 3]; // WARNING
let x = vec![1, 2, 3];
x.is_empty();
}
@ -170,34 +170,34 @@ fn function_argument() {
}
fn supported_types() {
let mut x = std::collections::BTreeMap::new(); // WARNING
let mut x = std::collections::BTreeMap::new();
x.insert(true, 1);
let mut x = std::collections::BTreeSet::new(); // WARNING
let mut x = std::collections::BTreeSet::new();
x.insert(1);
let mut x = std::collections::BinaryHeap::new(); // WARNING
let mut x = std::collections::BinaryHeap::new();
x.push(1);
let mut x = std::collections::HashMap::new(); // WARNING
let mut x = std::collections::HashMap::new();
x.insert(1, 2);
let mut x = std::collections::HashSet::new(); // WARNING
let mut x = std::collections::HashSet::new();
x.insert(1);
let mut x = std::collections::LinkedList::new(); // WARNING
let mut x = std::collections::LinkedList::new();
x.push_front(1);
let mut x = Some(true); // WARNING
let mut x = Some(true);
x.insert(false);
let mut x = String::from("hello"); // WARNING
let mut x = String::from("hello");
x.push('!');
let mut x = Vec::new(); // WARNING
let mut x = Vec::new();
x.clear();
x.push(1);
let mut x = std::collections::VecDeque::new(); // WARNING
let mut x = std::collections::VecDeque::new();
x.push_front(1);
}

View file

@ -1,7 +1,7 @@
error: collection is never read
--> $DIR/collection_is_never_read.rs:21:5
|
LL | let mut x = HashMap::new(); // WARNING
LL | let mut x = HashMap::new();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::collection-is-never-read` implied by `-D warnings`
@ -9,115 +9,115 @@ LL | let mut x = HashMap::new(); // WARNING
error: collection is never read
--> $DIR/collection_is_never_read.rs:60:5
|
LL | let mut x = vec![1, 2, 3]; // WARNING
LL | let mut x = vec![1, 2, 3];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
error: collection is never read
--> $DIR/collection_is_never_read.rs:75:5
|
LL | let mut x = HashMap::new(); // WARNING
LL | let mut x = HashMap::new();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: collection is never read
--> $DIR/collection_is_never_read.rs:80:5
|
LL | let mut x = HashMap::new(); // WARNING
LL | let mut x = HashMap::new();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: collection is never read
--> $DIR/collection_is_never_read.rs:88:5
|
LL | let mut x = vec![1, 2, 3]; // WARNING
LL | let mut x = vec![1, 2, 3];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
error: collection is never read
--> $DIR/collection_is_never_read.rs:94:5
|
LL | let mut x = vec![1, 2, 3]; // WARNING
LL | let mut x = vec![1, 2, 3];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
error: collection is never read
--> $DIR/collection_is_never_read.rs:100:5
|
LL | let mut x = vec![1, 2, 3]; // WARNING
LL | let mut x = vec![1, 2, 3];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
error: collection is never read
--> $DIR/collection_is_never_read.rs:107:5
|
LL | let mut x = vec![1, 2, 3]; // WARNING
LL | let mut x = vec![1, 2, 3];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
error: collection is never read
--> $DIR/collection_is_never_read.rs:124:5
|
LL | let mut x = HashSet::new(); // WARNING
LL | let mut x = HashSet::new();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: collection is never read
--> $DIR/collection_is_never_read.rs:138:5
|
LL | let x = vec![1, 2, 3]; // WARNING
LL | let x = vec![1, 2, 3];
| ^^^^^^^^^^^^^^^^^^^^^^
error: collection is never read
--> $DIR/collection_is_never_read.rs:173:5
|
LL | let mut x = std::collections::BTreeMap::new(); // WARNING
LL | let mut x = std::collections::BTreeMap::new();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: collection is never read
--> $DIR/collection_is_never_read.rs:176:5
|
LL | let mut x = std::collections::BTreeSet::new(); // WARNING
LL | let mut x = std::collections::BTreeSet::new();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: collection is never read
--> $DIR/collection_is_never_read.rs:179:5
|
LL | let mut x = std::collections::BinaryHeap::new(); // WARNING
LL | let mut x = std::collections::BinaryHeap::new();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: collection is never read
--> $DIR/collection_is_never_read.rs:182:5
|
LL | let mut x = std::collections::HashMap::new(); // WARNING
LL | let mut x = std::collections::HashMap::new();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: collection is never read
--> $DIR/collection_is_never_read.rs:185:5
|
LL | let mut x = std::collections::HashSet::new(); // WARNING
LL | let mut x = std::collections::HashSet::new();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: collection is never read
--> $DIR/collection_is_never_read.rs:188:5
|
LL | let mut x = std::collections::LinkedList::new(); // WARNING
LL | let mut x = std::collections::LinkedList::new();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: collection is never read
--> $DIR/collection_is_never_read.rs:191:5
|
LL | let mut x = Some(true); // WARNING
LL | let mut x = Some(true);
| ^^^^^^^^^^^^^^^^^^^^^^^
error: collection is never read
--> $DIR/collection_is_never_read.rs:194:5
|
LL | let mut x = String::from("hello"); // WARNING
LL | let mut x = String::from("hello");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: collection is never read
--> $DIR/collection_is_never_read.rs:197:5
|
LL | let mut x = Vec::new(); // WARNING
LL | let mut x = Vec::new();
| ^^^^^^^^^^^^^^^^^^^^^^^
error: collection is never read
--> $DIR/collection_is_never_read.rs:201:5
|
LL | let mut x = std::collections::VecDeque::new(); // WARNING
LL | let mut x = std::collections::VecDeque::new();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 20 previous errors

View file

@ -2,9 +2,13 @@
#![allow(dead_code)]
fn main() {
let counter = 10; // OK, latin is allowed.
let zähler = 10; // OK, it's still latin.
// OK, latin is allowed.
let counter = 10;
// OK, it's still latin.
let zähler = 10;
let счётчик = 10; // Cyrillic is not allowed by default.
let = 10; // Same for japanese.
// Cyrillic is not allowed by default.
let счётчик = 10;
// Same for japanese.
let = 10;
}

View file

@ -1,7 +1,7 @@
error: identifier `счётчик` has a Unicode script that is not allowed by configuration: Cyrillic
--> $DIR/disallowed_script_idents.rs:8:9
--> $DIR/disallowed_script_idents.rs:11:9
|
LL | let счётчик = 10; // Cyrillic is not allowed by default.
LL | let счётчик = 10;
| ^^^^^^^
|
note: the lint level is defined here
@ -11,9 +11,9 @@ LL | #![deny(clippy::disallowed_script_idents)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: identifier `カウンタ` has a Unicode script that is not allowed by configuration: Katakana
--> $DIR/disallowed_script_idents.rs:9:9
--> $DIR/disallowed_script_idents.rs:13:9
|
LL | let カウンタ = 10; // Same for japanese.
LL | let カウンタ = 10;
| ^^^^^^^^
error: aborting due to 2 previous errors

View file

@ -20,16 +20,20 @@ fn main() {
eprint!("\n\n");
eprint!("like eof\n\n");
eprint!("Hello {} {}\n\n", "world", "#2");
eprintln!("\ndon't\nwarn\nfor\nmultiple\nnewlines\n"); // #3126
eprintln!("\nbla\n\n"); // #3126
// #3126
eprintln!("\ndon't\nwarn\nfor\nmultiple\nnewlines\n");
// #3126
eprintln!("\nbla\n\n");
// Escaping
eprint!("\\n"); // #3514
eprintln!("\\"); // should fail
// #3514
eprint!("\\n");
eprintln!("\\");
eprint!("\\\\n");
// Raw strings
eprint!(r"\n"); // #3778
// #3778
eprint!(r"\n");
// Literal newlines should also fail
eprintln!(

View file

@ -20,16 +20,20 @@ fn main() {
eprint!("\n\n");
eprint!("like eof\n\n");
eprint!("Hello {} {}\n\n", "world", "#2");
eprintln!("\ndon't\nwarn\nfor\nmultiple\nnewlines\n"); // #3126
eprintln!("\nbla\n\n"); // #3126
// #3126
eprintln!("\ndon't\nwarn\nfor\nmultiple\nnewlines\n");
// #3126
eprintln!("\nbla\n\n");
// Escaping
eprint!("\\n"); // #3514
eprint!("\\\n"); // should fail
// #3514
eprint!("\\n");
eprint!("\\\n");
eprint!("\\\\n");
// Raw strings
eprint!(r"\n"); // #3778
// #3778
eprint!(r"\n");
// Literal newlines should also fail
eprint!(

View file

@ -60,19 +60,19 @@ LL + eprintln!();
|
error: using `eprint!()` with a format string that ends in a single newline
--> $DIR/eprint_with_newline.rs:28:5
--> $DIR/eprint_with_newline.rs:31:5
|
LL | eprint!("\\\n"); // should fail
LL | eprint!("\\\n");
| ^^^^^^^^^^^^^^^
|
help: use `eprintln!` instead
|
LL - eprint!("\\\n"); // should fail
LL + eprintln!("\\"); // should fail
LL - eprint!("\\\n");
LL + eprintln!("\\");
|
error: using `eprint!()` with a format string that ends in a single newline
--> $DIR/eprint_with_newline.rs:35:5
--> $DIR/eprint_with_newline.rs:39:5
|
LL | / eprint!(
LL | | "
@ -87,7 +87,7 @@ LL ~
|
error: using `eprint!()` with a format string that ends in a single newline
--> $DIR/eprint_with_newline.rs:39:5
--> $DIR/eprint_with_newline.rs:43:5
|
LL | / eprint!(
LL | | r"
@ -102,7 +102,7 @@ LL ~
|
error: using `eprint!()` with a format string that ends in a single newline
--> $DIR/eprint_with_newline.rs:47:5
--> $DIR/eprint_with_newline.rs:51:5
|
LL | eprint!("\\r\n");
| ^^^^^^^^^^^^^^^^

View file

@ -25,29 +25,43 @@ fn main() {
let x = [1, 2, 3, 4];
let index: usize = 1;
x[index];
x[4]; // Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays.
x[1 << 3]; // Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays.
// Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays.
x[4];
// Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays.
x[1 << 3];
x[0]; // Ok, should not produce stderr.
x[3]; // Ok, should not produce stderr.
x[const { idx() }]; // Ok, should not produce stderr.
x[const { idx4() }]; // Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays.
const { &ARR[idx()] }; // This should be linted, since `suppress-restriction-lint-in-const` default is false.
const { &ARR[idx4()] }; // This should be linted, since `suppress-restriction-lint-in-const` default is false.
// Ok, should not produce stderr.
x[0];
// Ok, should not produce stderr.
x[3];
// Ok, should not produce stderr.
x[const { idx() }];
// Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays.
x[const { idx4() }];
// This should be linted, since `suppress-restriction-lint-in-const` default is false.
const { &ARR[idx()] };
// This should be linted, since `suppress-restriction-lint-in-const` default is false.
const { &ARR[idx4()] };
let y = &x;
y[0]; // Ok, referencing shouldn't affect this lint. See the issue 6021
y[4]; // Ok, rustc will handle references too.
// Ok, referencing shouldn't affect this lint. See the issue 6021
y[0];
// Ok, rustc will handle references too.
y[4];
let v = vec![0; 5];
v[0];
v[10];
v[1 << 3];
const N: usize = 15; // Out of bounds
const M: usize = 3; // In bounds
x[N]; // Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays.
x[M]; // Ok, should not produce stderr.
// Out of bounds
const N: usize = 15;
// In bounds
const M: usize = 3;
// Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays.
x[N];
// Ok, should not produce stderr.
x[M];
v[N];
v[M];
}

View file

@ -18,15 +18,15 @@ LL | const REF_ERR: &i32 = &ARR[idx4()]; // Ok, let rustc handle const contexts.
= note: the suggestion might not be applicable in constant blocks
error[E0080]: evaluation of `main::{constant#3}` failed
--> $DIR/indexing_slicing_index.rs:36:14
--> $DIR/indexing_slicing_index.rs:44:14
|
LL | const { &ARR[idx4()] }; // This should be linted, since `suppress-restriction-lint-in-const` default is false.
LL | const { &ARR[idx4()] };
| ^^^^^^^^^^^ index out of bounds: the length is 2 but the index is 4
note: erroneous constant used
--> $DIR/indexing_slicing_index.rs:36:5
--> $DIR/indexing_slicing_index.rs:44:5
|
LL | const { &ARR[idx4()] }; // This should be linted, since `suppress-restriction-lint-in-const` default is false.
LL | const { &ARR[idx4()] };
| ^^^^^^^^^^^^^^^^^^^^^^
error: indexing may panic
@ -38,25 +38,25 @@ LL | x[index];
= help: consider using `.get(n)` or `.get_mut(n)` instead
error: indexing may panic
--> $DIR/indexing_slicing_index.rs:35:14
--> $DIR/indexing_slicing_index.rs:42:14
|
LL | const { &ARR[idx()] }; // This should be linted, since `suppress-restriction-lint-in-const` default is false.
LL | const { &ARR[idx()] };
| ^^^^^^^^^^
|
= help: consider using `.get(n)` or `.get_mut(n)` instead
= note: the suggestion might not be applicable in constant blocks
error: indexing may panic
--> $DIR/indexing_slicing_index.rs:36:14
--> $DIR/indexing_slicing_index.rs:44:14
|
LL | const { &ARR[idx4()] }; // This should be linted, since `suppress-restriction-lint-in-const` default is false.
LL | const { &ARR[idx4()] };
| ^^^^^^^^^^^
|
= help: consider using `.get(n)` or `.get_mut(n)` instead
= note: the suggestion might not be applicable in constant blocks
error: indexing may panic
--> $DIR/indexing_slicing_index.rs:43:5
--> $DIR/indexing_slicing_index.rs:53:5
|
LL | v[0];
| ^^^^
@ -64,7 +64,7 @@ LL | v[0];
= help: consider using `.get(n)` or `.get_mut(n)` instead
error: indexing may panic
--> $DIR/indexing_slicing_index.rs:44:5
--> $DIR/indexing_slicing_index.rs:54:5
|
LL | v[10];
| ^^^^^
@ -72,7 +72,7 @@ LL | v[10];
= help: consider using `.get(n)` or `.get_mut(n)` instead
error: indexing may panic
--> $DIR/indexing_slicing_index.rs:45:5
--> $DIR/indexing_slicing_index.rs:55:5
|
LL | v[1 << 3];
| ^^^^^^^^^
@ -80,7 +80,7 @@ LL | v[1 << 3];
= help: consider using `.get(n)` or `.get_mut(n)` instead
error: indexing may panic
--> $DIR/indexing_slicing_index.rs:51:5
--> $DIR/indexing_slicing_index.rs:65:5
|
LL | v[N];
| ^^^^
@ -88,7 +88,7 @@ LL | v[N];
= help: consider using `.get(n)` or `.get_mut(n)` instead
error: indexing may panic
--> $DIR/indexing_slicing_index.rs:52:5
--> $DIR/indexing_slicing_index.rs:66:5
|
LL | v[M];
| ^^^^

View file

@ -12,8 +12,8 @@ fn main() {
&x[index..];
&x[..index];
&x[index_from..index_to];
&x[index_from..][..index_to]; // Two lint reports, one for [index_from..] and another for [..index_to].
&x[5..][..10]; // Two lint reports, one for out of bounds [5..] and another for slicing [..10].
&x[index_from..][..index_to];
&x[5..][..10];
&x[0..][..3];
&x[1..][..5];
@ -29,7 +29,7 @@ fn main() {
let v = vec![0; 5];
&v[10..100];
&x[10..][..100]; // Two lint reports, one for [10..] and another for [..100].
&x[10..][..100];
&v[10..];
&v[..100];

View file

@ -26,7 +26,7 @@ LL | &x[index_from..index_to];
error: slicing may panic
--> $DIR/indexing_slicing_slice.rs:15:6
|
LL | &x[index_from..][..index_to]; // Two lint reports, one for [index_from..] and another for [..index_to].
LL | &x[index_from..][..index_to];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider using `.get(..n)`or `.get_mut(..n)` instead
@ -34,7 +34,7 @@ LL | &x[index_from..][..index_to]; // Two lint reports, one for [index_from.
error: slicing may panic
--> $DIR/indexing_slicing_slice.rs:15:6
|
LL | &x[index_from..][..index_to]; // Two lint reports, one for [index_from..] and another for [..index_to].
LL | &x[index_from..][..index_to];
| ^^^^^^^^^^^^^^^
|
= help: consider using `.get(n..)` or .get_mut(n..)` instead
@ -42,7 +42,7 @@ LL | &x[index_from..][..index_to]; // Two lint reports, one for [index_from.
error: slicing may panic
--> $DIR/indexing_slicing_slice.rs:16:6
|
LL | &x[5..][..10]; // Two lint reports, one for out of bounds [5..] and another for slicing [..10].
LL | &x[5..][..10];
| ^^^^^^^^^^^^
|
= help: consider using `.get(..n)`or `.get_mut(..n)` instead
@ -50,7 +50,7 @@ LL | &x[5..][..10]; // Two lint reports, one for out of bounds [5..] and ano
error: range is out of bounds
--> $DIR/indexing_slicing_slice.rs:16:8
|
LL | &x[5..][..10]; // Two lint reports, one for out of bounds [5..] and another for slicing [..10].
LL | &x[5..][..10];
| ^
|
= note: `-D clippy::out-of-bounds-indexing` implied by `-D warnings`
@ -94,7 +94,7 @@ LL | &v[10..100];
error: slicing may panic
--> $DIR/indexing_slicing_slice.rs:32:6
|
LL | &x[10..][..100]; // Two lint reports, one for [10..] and another for [..100].
LL | &x[10..][..100];
| ^^^^^^^^^^^^^^
|
= help: consider using `.get(..n)`or `.get_mut(..n)` instead
@ -102,7 +102,7 @@ LL | &x[10..][..100]; // Two lint reports, one for [10..] and another for [.
error: range is out of bounds
--> $DIR/indexing_slicing_slice.rs:32:8
|
LL | &x[10..][..100]; // Two lint reports, one for [10..] and another for [..100].
LL | &x[10..][..100];
| ^^
error: slicing may panic

View file

@ -8,42 +8,60 @@ fn square_is_lower_64(x: &u32) -> bool {
#[allow(clippy::maybe_infinite_iter)]
#[deny(clippy::infinite_iter)]
fn infinite_iters() {
repeat(0_u8).collect::<Vec<_>>(); // infinite iter
(0..8_u32).take_while(square_is_lower_64).cycle().count(); // infinite iter
(0..8_u64).chain(0..).max(); // infinite iter
repeat(0_u8).collect::<Vec<_>>();
// infinite iter
(0..8_u32).take_while(square_is_lower_64).cycle().count();
// infinite iter
(0..8_u64).chain(0..).max();
// infinite iter
(0_usize..)
.chain([0usize, 1, 2].iter().cloned())
.skip_while(|x| *x != 42)
.min(); // infinite iter
.min();
// infinite iter
(0..8_u32)
.rev()
.cycle()
.map(|x| x + 1_u32)
.for_each(|x| println!("{}", x)); // infinite iter
(0..3_u32).flat_map(|x| x..).sum::<u32>(); // infinite iter
(0_usize..).flat_map(|x| 0..x).product::<usize>(); // infinite iter
(0_u64..).filter(|x| x % 2 == 0).last(); // infinite iter
(0..42_u64).by_ref().last(); // not an infinite, because ranges are double-ended
(0..).next(); // iterator is not exhausted
.for_each(|x| println!("{}", x));
// infinite iter
(0..3_u32).flat_map(|x| x..).sum::<u32>();
// infinite iter
(0_usize..).flat_map(|x| 0..x).product::<usize>();
// infinite iter
(0_u64..).filter(|x| x % 2 == 0).last();
// not an infinite, because ranges are double-ended
(0..42_u64).by_ref().last();
// iterator is not exhausted
(0..).next();
}
#[deny(clippy::maybe_infinite_iter)]
fn potential_infinite_iters() {
(0..).zip((0..).take_while(square_is_lower_64)).count(); // maybe infinite iter
repeat(42).take_while(|x| *x == 42).chain(0..42).max(); // maybe infinite iter
// maybe infinite iter
(0..).zip((0..).take_while(square_is_lower_64)).count();
// maybe infinite iter
repeat(42).take_while(|x| *x == 42).chain(0..42).max();
// maybe infinite iter
(1..)
.scan(0, |state, x| {
*state += x;
Some(*state)
})
.min(); // maybe infinite iter
(0..).find(|x| *x == 24); // maybe infinite iter
(0..).position(|x| x == 24); // maybe infinite iter
(0..).any(|x| x == 24); // maybe infinite iter
(0..).all(|x| x == 24); // maybe infinite iter
.min();
// maybe infinite iter
(0..).find(|x| *x == 24);
// maybe infinite iter
(0..).position(|x| x == 24);
// maybe infinite iter
(0..).any(|x| x == 24);
// maybe infinite iter
(0..).all(|x| x == 24);
(0..).zip(0..42).take_while(|&(x, _)| x != 42).count(); // not infinite
repeat(42).take_while(|x| *x == 42).next(); // iterator is not exhausted
// not infinite
(0..).zip(0..42).take_while(|&(x, _)| x != 42).count();
// iterator is not exhausted
repeat(42).take_while(|x| *x == 42).next();
}
fn main() {
@ -62,7 +80,8 @@ mod finite_collect {
}
fn check_collect() {
let _: HashSet<i32> = (0..).collect(); // Infinite iter
// Infinite iter
let _: HashSet<i32> = (0..).collect();
// Some data structures don't collect infinitely, such as `ArrayVec`
let _: C = (0..).collect();

View file

@ -1,7 +1,7 @@
error: infinite iteration detected
--> $DIR/infinite_iter.rs:11:5
|
LL | repeat(0_u8).collect::<Vec<_>>(); // infinite iter
LL | repeat(0_u8).collect::<Vec<_>>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: the lint level is defined here
@ -11,96 +11,96 @@ LL | #[deny(clippy::infinite_iter)]
| ^^^^^^^^^^^^^^^^^^^^^
error: infinite iteration detected
--> $DIR/infinite_iter.rs:12:5
--> $DIR/infinite_iter.rs:13:5
|
LL | (0..8_u32).take_while(square_is_lower_64).cycle().count(); // infinite iter
LL | (0..8_u32).take_while(square_is_lower_64).cycle().count();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: infinite iteration detected
--> $DIR/infinite_iter.rs:13:5
--> $DIR/infinite_iter.rs:15:5
|
LL | (0..8_u64).chain(0..).max(); // infinite iter
LL | (0..8_u64).chain(0..).max();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: infinite iteration detected
--> $DIR/infinite_iter.rs:18:5
--> $DIR/infinite_iter.rs:22:5
|
LL | / (0..8_u32)
LL | | .rev()
LL | | .cycle()
LL | | .map(|x| x + 1_u32)
LL | | .for_each(|x| println!("{}", x)); // infinite iter
LL | | .for_each(|x| println!("{}", x));
| |________________________________________^
error: infinite iteration detected
--> $DIR/infinite_iter.rs:24:5
--> $DIR/infinite_iter.rs:30:5
|
LL | (0_usize..).flat_map(|x| 0..x).product::<usize>(); // infinite iter
LL | (0_usize..).flat_map(|x| 0..x).product::<usize>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: infinite iteration detected
--> $DIR/infinite_iter.rs:25:5
--> $DIR/infinite_iter.rs:32:5
|
LL | (0_u64..).filter(|x| x % 2 == 0).last(); // infinite iter
LL | (0_u64..).filter(|x| x % 2 == 0).last();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: possible infinite iteration detected
--> $DIR/infinite_iter.rs:32:5
--> $DIR/infinite_iter.rs:42:5
|
LL | (0..).zip((0..).take_while(square_is_lower_64)).count(); // maybe infinite iter
LL | (0..).zip((0..).take_while(square_is_lower_64)).count();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: the lint level is defined here
--> $DIR/infinite_iter.rs:30:8
--> $DIR/infinite_iter.rs:39:8
|
LL | #[deny(clippy::maybe_infinite_iter)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: possible infinite iteration detected
--> $DIR/infinite_iter.rs:33:5
--> $DIR/infinite_iter.rs:44:5
|
LL | repeat(42).take_while(|x| *x == 42).chain(0..42).max(); // maybe infinite iter
LL | repeat(42).take_while(|x| *x == 42).chain(0..42).max();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: possible infinite iteration detected
--> $DIR/infinite_iter.rs:34:5
--> $DIR/infinite_iter.rs:46:5
|
LL | / (1..)
LL | | .scan(0, |state, x| {
LL | | *state += x;
LL | | Some(*state)
LL | | })
LL | | .min(); // maybe infinite iter
LL | | .min();
| |______________^
error: possible infinite iteration detected
--> $DIR/infinite_iter.rs:40:5
--> $DIR/infinite_iter.rs:53:5
|
LL | (0..).find(|x| *x == 24); // maybe infinite iter
LL | (0..).find(|x| *x == 24);
| ^^^^^^^^^^^^^^^^^^^^^^^^
error: possible infinite iteration detected
--> $DIR/infinite_iter.rs:41:5
--> $DIR/infinite_iter.rs:55:5
|
LL | (0..).position(|x| x == 24); // maybe infinite iter
LL | (0..).position(|x| x == 24);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: possible infinite iteration detected
--> $DIR/infinite_iter.rs:42:5
--> $DIR/infinite_iter.rs:57:5
|
LL | (0..).any(|x| x == 24); // maybe infinite iter
LL | (0..).any(|x| x == 24);
| ^^^^^^^^^^^^^^^^^^^^^^
error: possible infinite iteration detected
--> $DIR/infinite_iter.rs:43:5
--> $DIR/infinite_iter.rs:59:5
|
LL | (0..).all(|x| x == 24); // maybe infinite iter
LL | (0..).all(|x| x == 24);
| ^^^^^^^^^^^^^^^^^^^^^^
error: infinite iteration detected
--> $DIR/infinite_iter.rs:65:31
--> $DIR/infinite_iter.rs:84:31
|
LL | let _: HashSet<i32> = (0..).collect(); // Infinite iter
LL | let _: HashSet<i32> = (0..).collect();
| ^^^^^^^^^^^^^^^
|
= note: `#[deny(clippy::infinite_iter)]` on by default

View file

@ -16,7 +16,8 @@ mod module {} // ok
pub mod pub_module {} // ok
fn foo() {}
pub fn pub_foo() {} // missing #[inline]
// missing #[inline]
pub fn pub_foo() {}
#[inline]
pub fn pub_foo_inline() {} // ok
#[inline(always)]
@ -32,7 +33,8 @@ trait Bar {
pub trait PubBar {
fn PubBar_a(); // ok
fn PubBar_b() {} // missing #[inline]
// missing #[inline]
fn PubBar_b() {}
#[inline]
fn PubBar_c() {} // ok
}
@ -46,9 +48,12 @@ impl PubBar for Foo {
// all of these need inline because PubFoo is exported
impl PubBar for PubFoo {
fn PubBar_a() {} // missing #[inline]
fn PubBar_b() {} // missing #[inline]
fn PubBar_c() {} // missing #[inline]
// missing #[inline]
fn PubBar_a() {}
// missing #[inline]
fn PubBar_b() {}
// missing #[inline]
fn PubBar_c() {}
}
// do not need inline because Foo is not exported
@ -58,7 +63,8 @@ impl Foo {
// need inline because PubFoo is exported
impl PubFoo {
pub fn PubFooImpl() {} // missing #[inline]
// missing #[inline]
pub fn PubFooImpl() {}
}
// do not lint this since users cannot control the external code

View file

@ -1,39 +1,39 @@
error: missing `#[inline]` for a function
--> $DIR/missing_inline.rs:19:1
--> $DIR/missing_inline.rs:20:1
|
LL | pub fn pub_foo() {} // missing #[inline]
LL | pub fn pub_foo() {}
| ^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::missing-inline-in-public-items` implied by `-D warnings`
error: missing `#[inline]` for a default trait method
--> $DIR/missing_inline.rs:35:5
--> $DIR/missing_inline.rs:37:5
|
LL | fn PubBar_b() {} // missing #[inline]
LL | fn PubBar_b() {}
| ^^^^^^^^^^^^^^^^
error: missing `#[inline]` for a method
--> $DIR/missing_inline.rs:49:5
--> $DIR/missing_inline.rs:52:5
|
LL | fn PubBar_a() {} // missing #[inline]
LL | fn PubBar_a() {}
| ^^^^^^^^^^^^^^^^
error: missing `#[inline]` for a method
--> $DIR/missing_inline.rs:50:5
--> $DIR/missing_inline.rs:54:5
|
LL | fn PubBar_b() {} // missing #[inline]
LL | fn PubBar_b() {}
| ^^^^^^^^^^^^^^^^
error: missing `#[inline]` for a method
--> $DIR/missing_inline.rs:51:5
--> $DIR/missing_inline.rs:56:5
|
LL | fn PubBar_c() {} // missing #[inline]
LL | fn PubBar_c() {}
| ^^^^^^^^^^^^^^^^
error: missing `#[inline]` for a method
--> $DIR/missing_inline.rs:61:5
--> $DIR/missing_inline.rs:67:5
|
LL | pub fn PubFooImpl() {} // missing #[inline]
LL | pub fn PubFooImpl() {}
| ^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 6 previous errors

View file

@ -8,16 +8,21 @@ fn main() {
10 % 1;
10 % -1;
10 % 2;
i32::MIN % (-1); // also caught by rustc
// also caught by rustc
i32::MIN % (-1);
const ONE: u32 = 1 * 1;
const NEG_ONE: i64 = 1 - 2;
const INT_MIN: i64 = i64::MIN;
2 % ONE;
5 % STATIC_ONE; // NOT caught by lint
// NOT caught by lint
5 % STATIC_ONE;
2 % NEG_ONE;
5 % STATIC_NEG_ONE; // NOT caught by lint
INT_MIN % NEG_ONE; // also caught by rustc
INT_MIN % STATIC_NEG_ONE; // ONLY caught by rustc
// NOT caught by lint
5 % STATIC_NEG_ONE;
// also caught by rustc
INT_MIN % NEG_ONE;
// ONLY caught by rustc
INT_MIN % STATIC_NEG_ONE;
}

View file

@ -1,21 +1,21 @@
error: this operation will panic at runtime
--> $DIR/modulo_one.rs:11:5
--> $DIR/modulo_one.rs:12:5
|
LL | i32::MIN % (-1); // also caught by rustc
LL | i32::MIN % (-1);
| ^^^^^^^^^^^^^^^ attempt to compute `i32::MIN % -1_i32`, which would overflow
|
= note: `#[deny(unconditional_panic)]` on by default
error: this operation will panic at runtime
--> $DIR/modulo_one.rs:21:5
--> $DIR/modulo_one.rs:25:5
|
LL | INT_MIN % NEG_ONE; // also caught by rustc
LL | INT_MIN % NEG_ONE;
| ^^^^^^^^^^^^^^^^^ attempt to compute `i64::MIN % -1_i64`, which would overflow
error: this operation will panic at runtime
--> $DIR/modulo_one.rs:22:5
--> $DIR/modulo_one.rs:27:5
|
LL | INT_MIN % STATIC_NEG_ONE; // ONLY caught by rustc
LL | INT_MIN % STATIC_NEG_ONE;
| ^^^^^^^^^^^^^^^^^^^^^^^^ attempt to compute `i64::MIN % -1_i64`, which would overflow
error: any number modulo 1 will be 0
@ -33,27 +33,27 @@ LL | 10 % -1;
| ^^^^^^^
error: any number modulo -1 will panic/overflow or result in 0
--> $DIR/modulo_one.rs:11:5
--> $DIR/modulo_one.rs:12:5
|
LL | i32::MIN % (-1); // also caught by rustc
LL | i32::MIN % (-1);
| ^^^^^^^^^^^^^^^
error: any number modulo 1 will be 0
--> $DIR/modulo_one.rs:17:5
--> $DIR/modulo_one.rs:18:5
|
LL | 2 % ONE;
| ^^^^^^^
error: any number modulo -1 will panic/overflow or result in 0
--> $DIR/modulo_one.rs:19:5
--> $DIR/modulo_one.rs:21:5
|
LL | 2 % NEG_ONE;
| ^^^^^^^^^^^
error: any number modulo -1 will panic/overflow or result in 0
--> $DIR/modulo_one.rs:21:5
--> $DIR/modulo_one.rs:25:5
|
LL | INT_MIN % NEG_ONE; // also caught by rustc
LL | INT_MIN % NEG_ONE;
| ^^^^^^^^^^^^^^^^^
error: aborting due to 9 previous errors

View file

@ -6,14 +6,14 @@ fn mut_range_bound_upper() {
let mut m = 4;
for i in 0..m {
m = 5;
} // warning
}
}
fn mut_range_bound_lower() {
let mut m = 4;
for i in m..10 {
m *= 2;
} // warning
}
}
fn mut_range_bound_both() {
@ -22,7 +22,7 @@ fn mut_range_bound_both() {
for i in m..n {
m = 5;
n = 7;
} // warning (1 for each mutated bound)
}
}
fn mut_range_bound_no_mutation() {
@ -35,7 +35,7 @@ fn mut_range_bound_no_mutation() {
fn mut_borrow_range_bound() {
let mut m = 4;
for i in 0..m {
let n = &mut m; // warning
let n = &mut m;
*n += 1;
}
}
@ -43,7 +43,7 @@ fn mut_borrow_range_bound() {
fn immut_borrow_range_bound() {
let mut m = 4;
for i in 0..m {
let n = &m; // should be no warning?
let n = &m;
}
}
@ -67,7 +67,8 @@ fn mut_range_bound_break() {
fn mut_range_bound_no_immediate_break() {
let mut m = 4;
for i in 0..m {
m = 2; // warning because it is not immediately followed by break
// warning because it is not immediately followed by break
m = 2;
if m == 4 {
break;
}
@ -76,7 +77,8 @@ fn mut_range_bound_no_immediate_break() {
let mut n = 3;
for i in n..10 {
if n == 4 {
n = 1; // FIXME: warning because it is not immediately followed by break
// FIXME: warning because it is not immediately followed by break
n = 1;
let _ = 2;
break;
}

View file

@ -34,23 +34,23 @@ LL | n = 7;
error: attempt to mutate range bound within loop
--> $DIR/mut_range_bound.rs:38:22
|
LL | let n = &mut m; // warning
LL | let n = &mut m;
| ^
|
= note: the range of the loop is unchanged
error: attempt to mutate range bound within loop
--> $DIR/mut_range_bound.rs:70:9
--> $DIR/mut_range_bound.rs:71:9
|
LL | m = 2; // warning because it is not immediately followed by break
LL | m = 2;
| ^
|
= note: the range of the loop is unchanged
error: attempt to mutate range bound within loop
--> $DIR/mut_range_bound.rs:79:13
--> $DIR/mut_range_bound.rs:81:13
|
LL | n = 1; // FIXME: warning because it is not immediately followed by break
LL | n = 1;
| ^
|
= note: the range of the loop is unchanged

View file

@ -55,21 +55,21 @@ fn main() {
fn simple_loop() {
loop {
continue; // should lint here
continue;
}
}
fn simple_loop2() {
loop {
println!("bleh");
continue; // should lint here
continue;
}
}
#[rustfmt::skip]
fn simple_loop3() {
loop {
continue // should lint here
continue
}
}
@ -77,7 +77,7 @@ fn simple_loop3() {
fn simple_loop4() {
loop {
println!("bleh");
continue // should lint here
continue
}
}
@ -128,13 +128,13 @@ mod issue_2329 {
if condition() {
println!("bar-3");
} else {
continue 'inner; // should lint here
continue 'inner;
}
println!("bar-4");
update_condition();
if condition() {
continue; // should lint here
continue;
} else {
println!("bar-5");
}

View file

@ -57,7 +57,7 @@ LL | | }
error: this `continue` expression is redundant
--> $DIR/needless_continue.rs:58:9
|
LL | continue; // should lint here
LL | continue;
| ^^^^^^^^^
|
= help: consider dropping the `continue` expression
@ -65,7 +65,7 @@ LL | continue; // should lint here
error: this `continue` expression is redundant
--> $DIR/needless_continue.rs:65:9
|
LL | continue; // should lint here
LL | continue;
| ^^^^^^^^^
|
= help: consider dropping the `continue` expression
@ -73,7 +73,7 @@ LL | continue; // should lint here
error: this `continue` expression is redundant
--> $DIR/needless_continue.rs:72:9
|
LL | continue // should lint here
LL | continue
| ^^^^^^^^
|
= help: consider dropping the `continue` expression
@ -81,7 +81,7 @@ LL | continue // should lint here
error: this `continue` expression is redundant
--> $DIR/needless_continue.rs:80:9
|
LL | continue // should lint here
LL | continue
| ^^^^^^^^
|
= help: consider dropping the `continue` expression
@ -91,7 +91,7 @@ error: this `else` block is redundant
|
LL | } else {
| ________________________^
LL | | continue 'inner; // should lint here
LL | | continue 'inner;
LL | | }
| |_________________^
|
@ -102,7 +102,7 @@ LL | | }
println!("bar-4");
update_condition();
if condition() {
continue; // should lint here
continue;
} else {
println!("bar-5");
}
@ -113,7 +113,7 @@ error: there is no need for an explicit `else` block for this `if` expression
--> $DIR/needless_continue.rs:136:17
|
LL | / if condition() {
LL | | continue; // should lint here
LL | | continue;
LL | | } else {
LL | | println!("bar-5");
LL | | }
@ -121,7 +121,7 @@ LL | | }
|
= help: consider dropping the `else` clause
if condition() {
continue; // should lint here
continue;
}
{
println!("bar-5");

View file

@ -6,6 +6,6 @@ fn main() {
// issue 3102
let num = 1;
&x[num..10]; // should trigger out of bounds error
&x[10..num]; // should trigger out of bounds error
&x[num..10];
&x[10..num];
}

View file

@ -1,7 +1,7 @@
error: range is out of bounds
--> $DIR/issue-3102.rs:9:13
|
LL | &x[num..10]; // should trigger out of bounds error
LL | &x[num..10];
| ^^
|
= note: `-D clippy::out-of-bounds-indexing` implied by `-D warnings`
@ -9,7 +9,7 @@ LL | &x[num..10]; // should trigger out of bounds error
error: range is out of bounds
--> $DIR/issue-3102.rs:10:8
|
LL | &x[10..num]; // should trigger out of bounds error
LL | &x[10..num];
| ^^
error: aborting due to 2 previous errors

View file

@ -22,16 +22,20 @@ fn main() {
print!("\n\n");
print!("like eof\n\n");
print!("Hello {} {}\n\n", "world", "#2");
println!("\ndon't\nwarn\nfor\nmultiple\nnewlines\n"); // #3126
println!("\nbla\n\n"); // #3126
// #3126
println!("\ndon't\nwarn\nfor\nmultiple\nnewlines\n");
// #3126
println!("\nbla\n\n");
// Escaping
print!("\\n"); // #3514
println!("\\"); // should fail
// #3514
print!("\\n");
println!("\\");
print!("\\\\n");
// Raw strings
print!(r"\n"); // #3778
// #3778
print!(r"\n");
// Literal newlines should also fail
println!(
@ -44,7 +48,8 @@ fn main() {
// Don't warn on CRLF (#4208)
print!("\r\n");
print!("foo\r\n");
println!("\\r"); // should fail
// should fail
println!("\\r");
print!("foo\rbar\n");
// Ignore expanded format strings

View file

@ -22,16 +22,20 @@ fn main() {
print!("\n\n");
print!("like eof\n\n");
print!("Hello {} {}\n\n", "world", "#2");
println!("\ndon't\nwarn\nfor\nmultiple\nnewlines\n"); // #3126
println!("\nbla\n\n"); // #3126
// #3126
println!("\ndon't\nwarn\nfor\nmultiple\nnewlines\n");
// #3126
println!("\nbla\n\n");
// Escaping
print!("\\n"); // #3514
print!("\\\n"); // should fail
// #3514
print!("\\n");
print!("\\\n");
print!("\\\\n");
// Raw strings
print!(r"\n"); // #3778
// #3778
print!(r"\n");
// Literal newlines should also fail
print!(
@ -46,7 +50,8 @@ fn main() {
// Don't warn on CRLF (#4208)
print!("\r\n");
print!("foo\r\n");
print!("\\r\n"); // should fail
// should fail
print!("\\r\n");
print!("foo\rbar\n");
// Ignore expanded format strings

View file

@ -60,19 +60,19 @@ LL + println!();
|
error: using `print!()` with a format string that ends in a single newline
--> $DIR/print_with_newline.rs:30:5
--> $DIR/print_with_newline.rs:33:5
|
LL | print!("\\\n"); // should fail
LL | print!("\\\n");
| ^^^^^^^^^^^^^^
|
help: use `println!` instead
|
LL - print!("\\\n"); // should fail
LL + println!("\\"); // should fail
LL - print!("\\\n");
LL + println!("\\");
|
error: using `print!()` with a format string that ends in a single newline
--> $DIR/print_with_newline.rs:37:5
--> $DIR/print_with_newline.rs:41:5
|
LL | / print!(
LL | | "
@ -87,7 +87,7 @@ LL ~
|
error: using `print!()` with a format string that ends in a single newline
--> $DIR/print_with_newline.rs:41:5
--> $DIR/print_with_newline.rs:45:5
|
LL | / print!(
LL | | r"
@ -102,15 +102,15 @@ LL ~
|
error: using `print!()` with a format string that ends in a single newline
--> $DIR/print_with_newline.rs:49:5
--> $DIR/print_with_newline.rs:54:5
|
LL | print!("\\r\n"); // should fail
LL | print!("\\r\n");
| ^^^^^^^^^^^^^^^
|
help: use `println!` instead
|
LL - print!("\\r\n"); // should fail
LL + println!("\\r"); // should fail
LL - print!("\\r\n");
LL + println!("\\r");
|
error: aborting due to 9 previous errors

View file

@ -3,8 +3,8 @@
fn main() {
let x: i32 = 42;
let _ = x.trailing_zeros() >= 4; // suggest trailing_zeros
let _ = x.trailing_zeros() >= 5; // suggest trailing_zeros
let _ = x.trailing_zeros() >= 4;
let _ = x.trailing_zeros() >= 5;
let _ = x & 0b1_1010 == 0; // do not lint
let _ = x & 1 == 0; // do not lint
}

View file

@ -3,8 +3,8 @@
fn main() {
let x: i32 = 42;
let _ = (x & 0b1111 == 0); // suggest trailing_zeros
let _ = x & 0b1_1111 == 0; // suggest trailing_zeros
let _ = (x & 0b1111 == 0);
let _ = x & 0b1_1111 == 0;
let _ = x & 0b1_1010 == 0; // do not lint
let _ = x & 1 == 0; // do not lint
}

View file

@ -1,7 +1,7 @@
error: bit mask could be simplified with a call to `trailing_zeros`
--> $DIR/trailing_zeros.rs:6:13
|
LL | let _ = (x & 0b1111 == 0); // suggest trailing_zeros
LL | let _ = (x & 0b1111 == 0);
| ^^^^^^^^^^^^^^^^^ help: try: `x.trailing_zeros() >= 4`
|
= note: `-D clippy::verbose-bit-mask` implied by `-D warnings`
@ -9,7 +9,7 @@ LL | let _ = (x & 0b1111 == 0); // suggest trailing_zeros
error: bit mask could be simplified with a call to `trailing_zeros`
--> $DIR/trailing_zeros.rs:7:13
|
LL | let _ = x & 0b1_1111 == 0; // suggest trailing_zeros
LL | let _ = x & 0b1_1111 == 0;
| ^^^^^^^^^^^^^^^^^ help: try: `x.trailing_zeros() >= 5`
error: aborting due to 2 previous errors

View file

@ -25,102 +25,160 @@ fn main() {
let _: Ty<u32> = transmute(value::<u32>());
let _: Ty<u32> = transmute(value::<u32>());
let _: Ty2C<u32, i32> = transmute(value::<Ty2<u32, i32>>()); // Lint, Ty2 is unordered
let _: Ty2<u32, i32> = transmute(value::<Ty2C<u32, i32>>()); // Lint, Ty2 is unordered
// Lint, Ty2 is unordered
let _: Ty2C<u32, i32> = transmute(value::<Ty2<u32, i32>>());
// Lint, Ty2 is unordered
let _: Ty2<u32, i32> = transmute(value::<Ty2C<u32, i32>>());
let _: Ty2<u32, i32> = transmute(value::<Ty<Ty2<u32, i32>>>()); // Ok, Ty2 types are the same
let _: Ty<Ty2<u32, i32>> = transmute(value::<Ty2<u32, i32>>()); // Ok, Ty2 types are the same
// Ok, Ty2 types are the same
let _: Ty2<u32, i32> = transmute(value::<Ty<Ty2<u32, i32>>>());
// Ok, Ty2 types are the same
let _: Ty<Ty2<u32, i32>> = transmute(value::<Ty2<u32, i32>>());
let _: Ty2<u32, f32> = transmute(value::<Ty<Ty2<u32, i32>>>()); // Lint, different Ty2 instances
let _: Ty<Ty2<u32, i32>> = transmute(value::<Ty2<u32, f32>>()); // Lint, different Ty2 instances
// Lint, different Ty2 instances
let _: Ty2<u32, f32> = transmute(value::<Ty<Ty2<u32, i32>>>());
// Lint, different Ty2 instances
let _: Ty<Ty2<u32, i32>> = transmute(value::<Ty2<u32, f32>>());
let _: Ty<&()> = transmute(value::<&()>());
let _: &() = transmute(value::<Ty<&()>>());
let _: &Ty2<u32, f32> = transmute(value::<Ty<&Ty2<u32, i32>>>()); // Lint, different Ty2 instances
let _: Ty<&Ty2<u32, i32>> = transmute(value::<&Ty2<u32, f32>>()); // Lint, different Ty2 instances
// Lint, different Ty2 instances
let _: &Ty2<u32, f32> = transmute(value::<Ty<&Ty2<u32, i32>>>());
// Lint, different Ty2 instances
let _: Ty<&Ty2<u32, i32>> = transmute(value::<&Ty2<u32, f32>>());
let _: Ty<usize> = transmute(value::<&Ty2<u32, i32>>()); // Ok, pointer to usize conversion
let _: &Ty2<u32, i32> = transmute(value::<Ty<usize>>()); // Ok, pointer to usize conversion
// Ok, pointer to usize conversion
let _: Ty<usize> = transmute(value::<&Ty2<u32, i32>>());
// Ok, pointer to usize conversion
let _: &Ty2<u32, i32> = transmute(value::<Ty<usize>>());
let _: Ty<[u8; 8]> = transmute(value::<Ty2<u32, i32>>()); // Ok, transmute to byte array
let _: Ty2<u32, i32> = transmute(value::<Ty<[u8; 8]>>()); // Ok, transmute from byte array
// Ok, transmute to byte array
let _: Ty<[u8; 8]> = transmute(value::<Ty2<u32, i32>>());
// Ok, transmute from byte array
let _: Ty2<u32, i32> = transmute(value::<Ty<[u8; 8]>>());
// issue #8417
let _: Ty2C<Ty2<u32, i32>, ()> = transmute(value::<Ty2<u32, i32>>()); // Ok, Ty2 types are the same
let _: Ty2<u32, i32> = transmute(value::<Ty2C<Ty2<u32, i32>, ()>>()); // Ok, Ty2 types are the same
// Ok, Ty2 types are the same
let _: Ty2C<Ty2<u32, i32>, ()> = transmute(value::<Ty2<u32, i32>>());
// Ok, Ty2 types are the same
let _: Ty2<u32, i32> = transmute(value::<Ty2C<Ty2<u32, i32>, ()>>());
let _: &'static mut Ty2<u32, u32> = transmute(value::<Box<Ty2<u32, u32>>>()); // Ok, Ty2 types are the same
let _: Box<Ty2<u32, u32>> = transmute(value::<&'static mut Ty2<u32, u32>>()); // Ok, Ty2 types are the same
let _: *mut Ty2<u32, u32> = transmute(value::<Box<Ty2<u32, u32>>>()); // Ok, Ty2 types are the same
let _: Box<Ty2<u32, u32>> = transmute(value::<*mut Ty2<u32, u32>>()); // Ok, Ty2 types are the same
// Ok, Ty2 types are the same
let _: &'static mut Ty2<u32, u32> = transmute(value::<Box<Ty2<u32, u32>>>());
// Ok, Ty2 types are the same
let _: Box<Ty2<u32, u32>> = transmute(value::<&'static mut Ty2<u32, u32>>());
// Ok, Ty2 types are the same
let _: *mut Ty2<u32, u32> = transmute(value::<Box<Ty2<u32, u32>>>());
// Ok, Ty2 types are the same
let _: Box<Ty2<u32, u32>> = transmute(value::<*mut Ty2<u32, u32>>());
let _: &'static mut Ty2<u32, f32> = transmute(value::<Box<Ty2<u32, u32>>>()); // Lint, different Ty2 instances
let _: Box<Ty2<u32, u32>> = transmute(value::<&'static mut Ty2<u32, f32>>()); // Lint, different Ty2 instances
// Lint, different Ty2 instances
let _: &'static mut Ty2<u32, f32> = transmute(value::<Box<Ty2<u32, u32>>>());
// Lint, different Ty2 instances
let _: Box<Ty2<u32, u32>> = transmute(value::<&'static mut Ty2<u32, f32>>());
let _: *const () = transmute(value::<Ty<&Ty2<u32, f32>>>()); // Ok, type erasure
let _: Ty<&Ty2<u32, f32>> = transmute(value::<*const ()>()); // Ok, reverse type erasure
// Ok, type erasure
let _: *const () = transmute(value::<Ty<&Ty2<u32, f32>>>());
// Ok, reverse type erasure
let _: Ty<&Ty2<u32, f32>> = transmute(value::<*const ()>());
let _: *const c_void = transmute(value::<Ty<&Ty2<u32, f32>>>()); // Ok, type erasure
let _: Ty<&Ty2<u32, f32>> = transmute(value::<*const c_void>()); // Ok, reverse type erasure
// Ok, type erasure
let _: *const c_void = transmute(value::<Ty<&Ty2<u32, f32>>>());
// Ok, reverse type erasure
let _: Ty<&Ty2<u32, f32>> = transmute(value::<*const c_void>());
enum Erase {}
let _: *const Erase = transmute(value::<Ty<&Ty2<u32, f32>>>()); // Ok, type erasure
let _: Ty<&Ty2<u32, f32>> = transmute(value::<*const Erase>()); // Ok, reverse type erasure
// Ok, type erasure
let _: *const Erase = transmute(value::<Ty<&Ty2<u32, f32>>>());
// Ok, reverse type erasure
let _: Ty<&Ty2<u32, f32>> = transmute(value::<*const Erase>());
struct Erase2(
[u8; 0],
core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>,
);
let _: *const Erase2 = transmute(value::<Ty<&Ty2<u32, f32>>>()); // Ok, type erasure
let _: Ty<&Ty2<u32, f32>> = transmute(value::<*const Erase2>()); // Ok, reverse type erasure
// Ok, type erasure
let _: *const Erase2 = transmute(value::<Ty<&Ty2<u32, f32>>>());
// Ok, reverse type erasure
let _: Ty<&Ty2<u32, f32>> = transmute(value::<*const Erase2>());
let _: *const () = transmute(value::<&&[u8]>()); // Ok, type erasure
let _: &&[u8] = transmute(value::<*const ()>()); // Ok, reverse type erasure
// Ok, type erasure
let _: *const () = transmute(value::<&&[u8]>());
// Ok, reverse type erasure
let _: &&[u8] = transmute(value::<*const ()>());
let _: *mut c_void = transmute(value::<&mut &[u8]>()); // Ok, type erasure
let _: &mut &[u8] = transmute(value::<*mut c_void>()); // Ok, reverse type erasure
// Ok, type erasure
let _: *mut c_void = transmute(value::<&mut &[u8]>());
// Ok, reverse type erasure
let _: &mut &[u8] = transmute(value::<*mut c_void>());
let _: [u8; size_of::<&[u8]>()] = transmute(value::<&[u8]>()); // Ok, transmute to byte array
let _: &[u8] = transmute(value::<[u8; size_of::<&[u8]>()]>()); // Ok, transmute from byte array
// Ok, transmute to byte array
let _: [u8; size_of::<&[u8]>()] = transmute(value::<&[u8]>());
// Ok, transmute from byte array
let _: &[u8] = transmute(value::<[u8; size_of::<&[u8]>()]>());
let _: [usize; 2] = transmute(value::<&[u8]>()); // Ok, transmute to int array
let _: &[u8] = transmute(value::<[usize; 2]>()); // Ok, transmute from int array
// Ok, transmute to int array
let _: [usize; 2] = transmute(value::<&[u8]>());
// Ok, transmute from int array
let _: &[u8] = transmute(value::<[usize; 2]>());
let _: *const [u8] = transmute(value::<Box<[u8]>>()); // Ok
let _: Box<[u8]> = transmute(value::<*mut [u8]>()); // Ok
// Ok
let _: *const [u8] = transmute(value::<Box<[u8]>>());
// Ok
let _: Box<[u8]> = transmute(value::<*mut [u8]>());
let _: Ty2<u32, u32> = transmute(value::<(Ty2<u32, u32>,)>()); // Ok
let _: (Ty2<u32, u32>,) = transmute(value::<Ty2<u32, u32>>()); // Ok
// Ok
let _: Ty2<u32, u32> = transmute(value::<(Ty2<u32, u32>,)>());
// Ok
let _: (Ty2<u32, u32>,) = transmute(value::<Ty2<u32, u32>>());
let _: Ty2<u32, u32> = transmute(value::<(Ty2<u32, u32>, ())>()); // Ok
let _: (Ty2<u32, u32>, ()) = transmute(value::<Ty2<u32, u32>>()); // Ok
// Ok
let _: Ty2<u32, u32> = transmute(value::<(Ty2<u32, u32>, ())>());
// Ok
let _: (Ty2<u32, u32>, ()) = transmute(value::<Ty2<u32, u32>>());
let _: Ty2<u32, u32> = transmute(value::<((), Ty2<u32, u32>)>()); // Ok
let _: ((), Ty2<u32, u32>) = transmute(value::<Ty2<u32, u32>>()); // Ok
// Ok
let _: Ty2<u32, u32> = transmute(value::<((), Ty2<u32, u32>)>());
// Ok
let _: ((), Ty2<u32, u32>) = transmute(value::<Ty2<u32, u32>>());
let _: (usize, usize) = transmute(value::<&[u8]>()); // Ok
let _: &[u8] = transmute(value::<(usize, usize)>()); // Ok
// Ok
let _: (usize, usize) = transmute(value::<&[u8]>());
// Ok
let _: &[u8] = transmute(value::<(usize, usize)>());
trait Trait {}
let _: (isize, isize) = transmute(value::<&dyn Trait>()); // Ok
let _: &dyn Trait = transmute(value::<(isize, isize)>()); // Ok
// Ok
let _: (isize, isize) = transmute(value::<&dyn Trait>());
let _: &dyn Trait = transmute(value::<(isize, isize)>());
let _: MaybeUninit<Ty2<u32, u32>> = transmute(value::<Ty2<u32, u32>>()); // Ok
let _: Ty2<u32, u32> = transmute(value::<MaybeUninit<Ty2<u32, u32>>>()); // Ok
// Ok
let _: MaybeUninit<Ty2<u32, u32>> = transmute(value::<Ty2<u32, u32>>());
// Ok
let _: Ty2<u32, u32> = transmute(value::<MaybeUninit<Ty2<u32, u32>>>());
let _: Ty<&[u32]> = transmute::<&[u32], _>(value::<&Vec<u32>>()); // Ok
// Ok
let _: Ty<&[u32]> = transmute::<&[u32], _>(value::<&Vec<u32>>());
let _: *const Ty2<u32, u32> = transmute(value::<*const Ty2C<Ty2<u32, u32>, u32>>()); // Ok
let _: *const Ty2C<Ty2<u32, u32>, u32> = transmute(value::<*const Ty2<u32, u32>>()); // Ok
let _: *const Ty2<u32, u32> = transmute(value::<*const Ty2C<(), Ty2<u32, u32>>>()); // Ok
let _: *const Ty2C<(), Ty2<u32, u32>> = transmute(value::<*const Ty2<u32, u32>>()); // Ok
// Ok
let _: *const Ty2<u32, u32> = transmute(value::<*const Ty2C<Ty2<u32, u32>, u32>>());
// Ok
let _: *const Ty2C<Ty2<u32, u32>, u32> = transmute(value::<*const Ty2<u32, u32>>());
// Ok
let _: *const Ty2<u32, u32> = transmute(value::<*const Ty2C<(), Ty2<u32, u32>>>());
// Ok
let _: *const Ty2C<(), Ty2<u32, u32>> = transmute(value::<*const Ty2<u32, u32>>());
let _: *const Ty2<u32, u32> = transmute(value::<*const Ty2C<u32, Ty2<u32, u32>>>()); // Err
let _: *const Ty2C<u32, Ty2<u32, u32>> = transmute(value::<*const Ty2<u32, u32>>()); // Err
// Err
let _: *const Ty2<u32, u32> = transmute(value::<*const Ty2C<u32, Ty2<u32, u32>>>());
// Err
let _: *const Ty2C<u32, Ty2<u32, u32>> = transmute(value::<*const Ty2<u32, u32>>());
let _: NonNull<u8> = transmute(value::<NonNull<(String, String)>>()); // Ok
let _: NonNull<(String, String)> = transmute(value::<NonNull<u8>>()); // Ok
// Ok
let _: NonNull<u8> = transmute(value::<NonNull<(String, String)>>());
// Ok
let _: NonNull<(String, String)> = transmute(value::<NonNull<u8>>());
}
}
@ -129,28 +187,44 @@ fn _with_generics<T: 'static, U: 'static>() {
return;
}
unsafe {
let _: &u32 = transmute(value::<&T>()); // Ok
let _: &T = transmute(value::<&u32>()); // Ok
// Ok
let _: &u32 = transmute(value::<&T>());
// Ok
let _: &T = transmute(value::<&u32>());
let _: Vec<U> = transmute(value::<Vec<T>>()); // Ok
let _: Vec<T> = transmute(value::<Vec<U>>()); // Ok
// Ok
let _: Vec<U> = transmute(value::<Vec<T>>());
// Ok
let _: Vec<T> = transmute(value::<Vec<U>>());
let _: Ty<&u32> = transmute(value::<&T>()); // Ok
let _: Ty<&T> = transmute(value::<&u32>()); // Ok
// Ok
let _: Ty<&u32> = transmute(value::<&T>());
// Ok
let _: Ty<&T> = transmute(value::<&u32>());
let _: Vec<u32> = transmute(value::<Vec<T>>()); // Ok
let _: Vec<T> = transmute(value::<Vec<u32>>()); // Ok
// Ok
let _: Vec<u32> = transmute(value::<Vec<T>>());
// Ok
let _: Vec<T> = transmute(value::<Vec<u32>>());
let _: &Ty2<u32, u32> = transmute(value::<&Ty2<T, U>>()); // Ok
let _: &Ty2<T, U> = transmute(value::<&Ty2<u32, u32>>()); // Ok
// Ok
let _: &Ty2<u32, u32> = transmute(value::<&Ty2<T, U>>());
// Ok
let _: &Ty2<T, U> = transmute(value::<&Ty2<u32, u32>>());
let _: Vec<Vec<u32>> = transmute(value::<Vec<Vec<T>>>()); // Ok
let _: Vec<Vec<T>> = transmute(value::<Vec<Vec<u32>>>()); // Ok
// Ok
let _: Vec<Vec<u32>> = transmute(value::<Vec<Vec<T>>>());
// Ok
let _: Vec<Vec<T>> = transmute(value::<Vec<Vec<u32>>>());
let _: Vec<Ty2<T, u32>> = transmute(value::<Vec<Ty2<U, i32>>>()); // Err
let _: Vec<Ty2<U, i32>> = transmute(value::<Vec<Ty2<T, u32>>>()); // Err
// Err
let _: Vec<Ty2<T, u32>> = transmute(value::<Vec<Ty2<U, i32>>>());
// Err
let _: Vec<Ty2<U, i32>> = transmute(value::<Vec<Ty2<T, u32>>>());
let _: *const u32 = transmute(value::<Box<T>>()); // Ok
let _: Box<T> = transmute(value::<*const u32>()); // Ok
// Ok
let _: *const u32 = transmute(value::<Box<T>>());
// Ok
let _: Box<T> = transmute(value::<*const u32>());
}
}

View file

@ -1,93 +1,93 @@
error: transmute from `Ty2<u32, i32>` which has an undefined layout
--> $DIR/transmute_undefined_repr.rs:28:33
--> $DIR/transmute_undefined_repr.rs:29:33
|
LL | let _: Ty2C<u32, i32> = transmute(value::<Ty2<u32, i32>>()); // Lint, Ty2 is unordered
LL | let _: Ty2C<u32, i32> = transmute(value::<Ty2<u32, i32>>());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::transmute-undefined-repr` implied by `-D warnings`
error: transmute into `Ty2<u32, i32>` which has an undefined layout
--> $DIR/transmute_undefined_repr.rs:29:32
--> $DIR/transmute_undefined_repr.rs:31:32
|
LL | let _: Ty2<u32, i32> = transmute(value::<Ty2C<u32, i32>>()); // Lint, Ty2 is unordered
LL | let _: Ty2<u32, i32> = transmute(value::<Ty2C<u32, i32>>());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: transmute from `Ty<Ty2<u32, i32>>` to `Ty2<u32, f32>`, both of which have an undefined layout
--> $DIR/transmute_undefined_repr.rs:34:32
--> $DIR/transmute_undefined_repr.rs:39:32
|
LL | let _: Ty2<u32, f32> = transmute(value::<Ty<Ty2<u32, i32>>>()); // Lint, different Ty2 instances
LL | let _: Ty2<u32, f32> = transmute(value::<Ty<Ty2<u32, i32>>>());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: two instances of the same generic type (`Ty2`) may have different layouts
error: transmute from `Ty2<u32, f32>` to `Ty<Ty2<u32, i32>>`, both of which have an undefined layout
--> $DIR/transmute_undefined_repr.rs:35:36
--> $DIR/transmute_undefined_repr.rs:41:36
|
LL | let _: Ty<Ty2<u32, i32>> = transmute(value::<Ty2<u32, f32>>()); // Lint, different Ty2 instances
LL | let _: Ty<Ty2<u32, i32>> = transmute(value::<Ty2<u32, f32>>());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: two instances of the same generic type (`Ty2`) may have different layouts
error: transmute from `Ty<&Ty2<u32, i32>>` to `&Ty2<u32, f32>`, both of which have an undefined layout
--> $DIR/transmute_undefined_repr.rs:40:33
--> $DIR/transmute_undefined_repr.rs:47:33
|
LL | let _: &Ty2<u32, f32> = transmute(value::<Ty<&Ty2<u32, i32>>>()); // Lint, different Ty2 instances
LL | let _: &Ty2<u32, f32> = transmute(value::<Ty<&Ty2<u32, i32>>>());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: two instances of the same generic type (`Ty2`) may have different layouts
error: transmute from `&Ty2<u32, f32>` to `Ty<&Ty2<u32, i32>>`, both of which have an undefined layout
--> $DIR/transmute_undefined_repr.rs:41:37
--> $DIR/transmute_undefined_repr.rs:49:37
|
LL | let _: Ty<&Ty2<u32, i32>> = transmute(value::<&Ty2<u32, f32>>()); // Lint, different Ty2 instances
LL | let _: Ty<&Ty2<u32, i32>> = transmute(value::<&Ty2<u32, f32>>());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: two instances of the same generic type (`Ty2`) may have different layouts
error: transmute from `std::boxed::Box<Ty2<u32, u32>>` to `&mut Ty2<u32, f32>`, both of which have an undefined layout
--> $DIR/transmute_undefined_repr.rs:58:45
--> $DIR/transmute_undefined_repr.rs:77:45
|
LL | let _: &'static mut Ty2<u32, f32> = transmute(value::<Box<Ty2<u32, u32>>>()); // Lint, different Ty2 instances
LL | let _: &'static mut Ty2<u32, f32> = transmute(value::<Box<Ty2<u32, u32>>>());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: two instances of the same generic type (`Ty2`) may have different layouts
error: transmute from `&mut Ty2<u32, f32>` to `std::boxed::Box<Ty2<u32, u32>>`, both of which have an undefined layout
--> $DIR/transmute_undefined_repr.rs:59:37
--> $DIR/transmute_undefined_repr.rs:79:37
|
LL | let _: Box<Ty2<u32, u32>> = transmute(value::<&'static mut Ty2<u32, f32>>()); // Lint, different Ty2 instances
LL | let _: Box<Ty2<u32, u32>> = transmute(value::<&'static mut Ty2<u32, f32>>());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: two instances of the same generic type (`Ty2`) may have different layouts
error: transmute into `*const Ty2<u32, u32>` which has an undefined layout
--> $DIR/transmute_undefined_repr.rs:119:39
--> $DIR/transmute_undefined_repr.rs:174:39
|
LL | let _: *const Ty2<u32, u32> = transmute(value::<*const Ty2C<u32, Ty2<u32, u32>>>()); // Err
LL | let _: *const Ty2<u32, u32> = transmute(value::<*const Ty2C<u32, Ty2<u32, u32>>>());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: the contained type `Ty2<u32, u32>` has an undefined layout
error: transmute from `*const Ty2<u32, u32>` which has an undefined layout
--> $DIR/transmute_undefined_repr.rs:120:50
--> $DIR/transmute_undefined_repr.rs:176:50
|
LL | let _: *const Ty2C<u32, Ty2<u32, u32>> = transmute(value::<*const Ty2<u32, u32>>()); // Err
LL | let _: *const Ty2C<u32, Ty2<u32, u32>> = transmute(value::<*const Ty2<u32, u32>>());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: the contained type `Ty2<u32, u32>` has an undefined layout
error: transmute from `std::vec::Vec<Ty2<U, i32>>` to `std::vec::Vec<Ty2<T, u32>>`, both of which have an undefined layout
--> $DIR/transmute_undefined_repr.rs:150:35
--> $DIR/transmute_undefined_repr.rs:221:35
|
LL | let _: Vec<Ty2<T, u32>> = transmute(value::<Vec<Ty2<U, i32>>>()); // Err
LL | let _: Vec<Ty2<T, u32>> = transmute(value::<Vec<Ty2<U, i32>>>());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: two instances of the same generic type (`Vec`) may have different layouts
error: transmute from `std::vec::Vec<Ty2<T, u32>>` to `std::vec::Vec<Ty2<U, i32>>`, both of which have an undefined layout
--> $DIR/transmute_undefined_repr.rs:151:35
--> $DIR/transmute_undefined_repr.rs:223:35
|
LL | let _: Vec<Ty2<U, i32>> = transmute(value::<Vec<Ty2<T, u32>>>()); // Err
LL | let _: Vec<Ty2<U, i32>> = transmute(value::<Vec<Ty2<T, u32>>>());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: two instances of the same generic type (`Vec`) may have different layouts

View file

@ -1,4 +1,4 @@
error: this argument (N byte) is passed by reference, but would be more efficient if passed by value (limit: N byte)
error: this argument (N byte) is passed by reference, but would be more efficient if passed by value (limit: 8 byte)
--> $DIR/trivially_copy_pass_by_ref.rs:52:11
|
LL | fn bad(x: &u32, y: &Foo, z: &Baz) {}
@ -10,13 +10,13 @@ note: the lint level is defined here
LL | #![deny(clippy::trivially_copy_pass_by_ref)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: this argument (N byte) is passed by reference, but would be more efficient if passed by value (limit: N byte)
error: this argument (N byte) is passed by reference, but would be more efficient if passed by value (limit: 8 byte)
--> $DIR/trivially_copy_pass_by_ref.rs:52:20
|
LL | fn bad(x: &u32, y: &Foo, z: &Baz) {}
| ^^^^ help: consider passing by value instead: `Foo`
error: this argument (N byte) is passed by reference, but would be more efficient if passed by value (limit: N byte)
error: this argument (N byte) is passed by reference, but would be more efficient if passed by value (limit: 8 byte)
--> $DIR/trivially_copy_pass_by_ref.rs:52:29
|
LL | fn bad(x: &u32, y: &Foo, z: &Baz) {}

View file

@ -30,6 +30,8 @@ fn main() {
writeln!(v, r#"{}"#, "\\");
writeln!(v, "{}", r"\");
writeln!(v, "{}", "\r");
writeln!(v, r#"{}{}"#, '#', '"'); // hard mode
writeln!(v, r"{}", "\r"); // should not lint
// hard mode
writeln!(v, r#"{}{}"#, '#', '"');
// should not lint
writeln!(v, r"{}", "\r");
}

View file

@ -183,15 +183,15 @@ LL + writeln!(v, "\r");
|
error: literal with an empty format string
--> $DIR/write_literal_2.rs:33:28
--> $DIR/write_literal_2.rs:34:28
|
LL | writeln!(v, r#"{}{}"#, '#', '"'); // hard mode
LL | writeln!(v, r#"{}{}"#, '#', '"');
| ^^^
error: literal with an empty format string
--> $DIR/write_literal_2.rs:33:33
--> $DIR/write_literal_2.rs:34:33
|
LL | writeln!(v, r#"{}{}"#, '#', '"'); // hard mode
LL | writeln!(v, r#"{}{}"#, '#', '"');
| ^^^
error: aborting due to 18 previous errors

View file

@ -27,16 +27,20 @@ fn main() {
write!(v, "\n\n");
write!(v, "like eof\n\n");
write!(v, "Hello {} {}\n\n", "world", "#2");
writeln!(v, "\ndon't\nwarn\nfor\nmultiple\nnewlines\n"); // #3126
writeln!(v, "\nbla\n\n"); // #3126
// #3126
writeln!(v, "\ndon't\nwarn\nfor\nmultiple\nnewlines\n");
// #3126
writeln!(v, "\nbla\n\n");
// Escaping
write!(v, "\\n"); // #3514
writeln!(v, "\\"); // should fail
// #3514
write!(v, "\\n");
writeln!(v, "\\");
write!(v, "\\\\n");
// Raw strings
write!(v, r"\n"); // #3778
// #3778
write!(v, r"\n");
// Literal newlines should also fail
writeln!(

View file

@ -27,16 +27,20 @@ fn main() {
write!(v, "\n\n");
write!(v, "like eof\n\n");
write!(v, "Hello {} {}\n\n", "world", "#2");
writeln!(v, "\ndon't\nwarn\nfor\nmultiple\nnewlines\n"); // #3126
writeln!(v, "\nbla\n\n"); // #3126
// #3126
writeln!(v, "\ndon't\nwarn\nfor\nmultiple\nnewlines\n");
// #3126
writeln!(v, "\nbla\n\n");
// Escaping
write!(v, "\\n"); // #3514
write!(v, "\\\n"); // should fail
// #3514
write!(v, "\\n");
write!(v, "\\\n");
write!(v, "\\\\n");
// Raw strings
write!(v, r"\n"); // #3778
// #3778
write!(v, r"\n");
// Literal newlines should also fail
write!(

View file

@ -60,19 +60,19 @@ LL + writeln!(v);
|
error: using `write!()` with a format string that ends in a single newline
--> $DIR/write_with_newline.rs:35:5
--> $DIR/write_with_newline.rs:38:5
|
LL | write!(v, "\\\n"); // should fail
LL | write!(v, "\\\n");
| ^^^^^^^^^^^^^^^^^
|
help: use `writeln!` instead
|
LL - write!(v, "\\\n"); // should fail
LL + writeln!(v, "\\"); // should fail
LL - write!(v, "\\\n");
LL + writeln!(v, "\\");
|
error: using `write!()` with a format string that ends in a single newline
--> $DIR/write_with_newline.rs:42:5
--> $DIR/write_with_newline.rs:46:5
|
LL | / write!(
LL | | v,
@ -88,7 +88,7 @@ LL ~ v
|
error: using `write!()` with a format string that ends in a single newline
--> $DIR/write_with_newline.rs:47:5
--> $DIR/write_with_newline.rs:51:5
|
LL | / write!(
LL | | v,
@ -104,7 +104,7 @@ LL ~ v
|
error: using `write!()` with a format string that ends in a single newline
--> $DIR/write_with_newline.rs:56:5
--> $DIR/write_with_newline.rs:60:5
|
LL | write!(v, "\\r\n");
| ^^^^^^^^^^^^^^^^^^