Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Move MSRV tests into the lint specific test files #9688

Merged
merged 1 commit into from
Oct 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions book/src/development/adding_lints.md
Original file line number Diff line number Diff line change
Expand Up @@ -478,8 +478,27 @@ impl<'tcx> LateLintPass<'tcx> for ManualStrip {
```

Once the `msrv` is added to the lint, a relevant test case should be added to
`tests/ui/min_rust_version_attr.rs` which verifies that the lint isn't emitted
if the project's MSRV is lower.
the lint's test file, `tests/ui/manual_strip.rs` in this example. It should
have a case for the version below the MSRV and one with the same contents but
for the MSRV version itself.

```rust
#![feature(custom_inner_attributes)]

...

fn msrv_1_44() {
#![clippy::msrv = "1.44"]

/* something that would trigger the lint */
}

fn msrv_1_45() {
#![clippy::msrv = "1.45"]

/* something that would trigger the lint */
}
```

As a last step, the lint should be added to the lint documentation. This is done
in `clippy_lints/src/utils/conf.rs`:
Expand Down
18 changes: 17 additions & 1 deletion tests/ui/cast_abs_to_unsigned.fixed
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// run-rustfix

#![feature(custom_inner_attributes)]
#![warn(clippy::cast_abs_to_unsigned)]
#![allow(clippy::uninlined_format_args)]
#![allow(clippy::uninlined_format_args, unused)]

fn main() {
let x: i32 = -42;
Expand Down Expand Up @@ -30,3 +32,17 @@ fn main() {

let _ = (x as i64 - y as i64).unsigned_abs() as u32;
}

fn msrv_1_50() {
#![clippy::msrv = "1.50"]

let x: i32 = 10;
assert_eq!(10u32, x.abs() as u32);
}

fn msrv_1_51() {
#![clippy::msrv = "1.51"]

let x: i32 = 10;
assert_eq!(10u32, x.unsigned_abs());
}
18 changes: 17 additions & 1 deletion tests/ui/cast_abs_to_unsigned.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// run-rustfix

#![feature(custom_inner_attributes)]
#![warn(clippy::cast_abs_to_unsigned)]
#![allow(clippy::uninlined_format_args)]
#![allow(clippy::uninlined_format_args, unused)]

fn main() {
let x: i32 = -42;
Expand Down Expand Up @@ -30,3 +32,17 @@ fn main() {

let _ = (x as i64 - y as i64).abs() as u32;
}

fn msrv_1_50() {
#![clippy::msrv = "1.50"]

let x: i32 = 10;
assert_eq!(10u32, x.abs() as u32);
}

fn msrv_1_51() {
#![clippy::msrv = "1.51"]

let x: i32 = 10;
assert_eq!(10u32, x.abs() as u32);
}
42 changes: 24 additions & 18 deletions tests/ui/cast_abs_to_unsigned.stderr
Original file line number Diff line number Diff line change
@@ -1,106 +1,112 @@
error: casting the result of `i32::abs()` to u32
--> $DIR/cast_abs_to_unsigned.rs:7:18
--> $DIR/cast_abs_to_unsigned.rs:9:18
|
LL | let y: u32 = x.abs() as u32;
| ^^^^^^^^^^^^^^ help: replace with: `x.unsigned_abs()`
|
= note: `-D clippy::cast-abs-to-unsigned` implied by `-D warnings`

error: casting the result of `i32::abs()` to usize
--> $DIR/cast_abs_to_unsigned.rs:11:20
--> $DIR/cast_abs_to_unsigned.rs:13:20
|
LL | let _: usize = a.abs() as usize;
| ^^^^^^^ help: replace with: `a.unsigned_abs()`

error: casting the result of `i32::abs()` to usize
--> $DIR/cast_abs_to_unsigned.rs:12:20
--> $DIR/cast_abs_to_unsigned.rs:14:20
|
LL | let _: usize = a.abs() as _;
| ^^^^^^^ help: replace with: `a.unsigned_abs()`

error: casting the result of `i32::abs()` to usize
--> $DIR/cast_abs_to_unsigned.rs:13:13
--> $DIR/cast_abs_to_unsigned.rs:15:13
|
LL | let _ = a.abs() as usize;
| ^^^^^^^ help: replace with: `a.unsigned_abs()`

error: casting the result of `i64::abs()` to usize
--> $DIR/cast_abs_to_unsigned.rs:16:13
--> $DIR/cast_abs_to_unsigned.rs:18:13
|
LL | let _ = a.abs() as usize;
| ^^^^^^^ help: replace with: `a.unsigned_abs()`

error: casting the result of `i64::abs()` to u8
--> $DIR/cast_abs_to_unsigned.rs:17:13
--> $DIR/cast_abs_to_unsigned.rs:19:13
|
LL | let _ = a.abs() as u8;
| ^^^^^^^ help: replace with: `a.unsigned_abs()`

error: casting the result of `i64::abs()` to u16
--> $DIR/cast_abs_to_unsigned.rs:18:13
--> $DIR/cast_abs_to_unsigned.rs:20:13
|
LL | let _ = a.abs() as u16;
| ^^^^^^^ help: replace with: `a.unsigned_abs()`

error: casting the result of `i64::abs()` to u32
--> $DIR/cast_abs_to_unsigned.rs:19:13
--> $DIR/cast_abs_to_unsigned.rs:21:13
|
LL | let _ = a.abs() as u32;
| ^^^^^^^ help: replace with: `a.unsigned_abs()`

error: casting the result of `i64::abs()` to u64
--> $DIR/cast_abs_to_unsigned.rs:20:13
--> $DIR/cast_abs_to_unsigned.rs:22:13
|
LL | let _ = a.abs() as u64;
| ^^^^^^^^^^^^^^ help: replace with: `a.unsigned_abs()`

error: casting the result of `i64::abs()` to u128
--> $DIR/cast_abs_to_unsigned.rs:21:13
--> $DIR/cast_abs_to_unsigned.rs:23:13
|
LL | let _ = a.abs() as u128;
| ^^^^^^^ help: replace with: `a.unsigned_abs()`

error: casting the result of `isize::abs()` to usize
--> $DIR/cast_abs_to_unsigned.rs:24:13
--> $DIR/cast_abs_to_unsigned.rs:26:13
|
LL | let _ = a.abs() as usize;
| ^^^^^^^^^^^^^^^^ help: replace with: `a.unsigned_abs()`

error: casting the result of `isize::abs()` to u8
--> $DIR/cast_abs_to_unsigned.rs:25:13
--> $DIR/cast_abs_to_unsigned.rs:27:13
|
LL | let _ = a.abs() as u8;
| ^^^^^^^ help: replace with: `a.unsigned_abs()`

error: casting the result of `isize::abs()` to u16
--> $DIR/cast_abs_to_unsigned.rs:26:13
--> $DIR/cast_abs_to_unsigned.rs:28:13
|
LL | let _ = a.abs() as u16;
| ^^^^^^^ help: replace with: `a.unsigned_abs()`

error: casting the result of `isize::abs()` to u32
--> $DIR/cast_abs_to_unsigned.rs:27:13
--> $DIR/cast_abs_to_unsigned.rs:29:13
|
LL | let _ = a.abs() as u32;
| ^^^^^^^ help: replace with: `a.unsigned_abs()`

error: casting the result of `isize::abs()` to u64
--> $DIR/cast_abs_to_unsigned.rs:28:13
--> $DIR/cast_abs_to_unsigned.rs:30:13
|
LL | let _ = a.abs() as u64;
| ^^^^^^^ help: replace with: `a.unsigned_abs()`

error: casting the result of `isize::abs()` to u128
--> $DIR/cast_abs_to_unsigned.rs:29:13
--> $DIR/cast_abs_to_unsigned.rs:31:13
|
LL | let _ = a.abs() as u128;
| ^^^^^^^ help: replace with: `a.unsigned_abs()`

error: casting the result of `i64::abs()` to u32
--> $DIR/cast_abs_to_unsigned.rs:31:13
--> $DIR/cast_abs_to_unsigned.rs:33:13
|
LL | let _ = (x as i64 - y as i64).abs() as u32;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `(x as i64 - y as i64).unsigned_abs()`

error: aborting due to 17 previous errors
error: casting the result of `i32::abs()` to u32
--> $DIR/cast_abs_to_unsigned.rs:47:23
|
LL | assert_eq!(10u32, x.abs() as u32);
| ^^^^^^^^^^^^^^ help: replace with: `x.unsigned_abs()`

error: aborting due to 18 previous errors

13 changes: 13 additions & 0 deletions tests/ui/cast_lossless_bool.fixed
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// run-rustfix

#![feature(custom_inner_attributes)]
#![allow(dead_code)]
#![warn(clippy::cast_lossless)]

Expand Down Expand Up @@ -40,3 +41,15 @@ mod cast_lossless_in_impl {
}
}
}

fn msrv_1_27() {
#![clippy::msrv = "1.27"]

let _ = true as u8;
}

fn msrv_1_28() {
#![clippy::msrv = "1.28"]

let _ = u8::from(true);
}
13 changes: 13 additions & 0 deletions tests/ui/cast_lossless_bool.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// run-rustfix

#![feature(custom_inner_attributes)]
#![allow(dead_code)]
#![warn(clippy::cast_lossless)]

Expand Down Expand Up @@ -40,3 +41,15 @@ mod cast_lossless_in_impl {
}
}
}

fn msrv_1_27() {
#![clippy::msrv = "1.27"]

let _ = true as u8;
}

fn msrv_1_28() {
#![clippy::msrv = "1.28"]

let _ = true as u8;
}
34 changes: 20 additions & 14 deletions tests/ui/cast_lossless_bool.stderr
Original file line number Diff line number Diff line change
@@ -1,82 +1,88 @@
error: casting `bool` to `u8` is more cleanly stated with `u8::from(_)`
--> $DIR/cast_lossless_bool.rs:8:13
--> $DIR/cast_lossless_bool.rs:9:13
|
LL | let _ = true as u8;
| ^^^^^^^^^^ help: try: `u8::from(true)`
|
= note: `-D clippy::cast-lossless` implied by `-D warnings`

error: casting `bool` to `u16` is more cleanly stated with `u16::from(_)`
--> $DIR/cast_lossless_bool.rs:9:13
--> $DIR/cast_lossless_bool.rs:10:13
|
LL | let _ = true as u16;
| ^^^^^^^^^^^ help: try: `u16::from(true)`

error: casting `bool` to `u32` is more cleanly stated with `u32::from(_)`
--> $DIR/cast_lossless_bool.rs:10:13
--> $DIR/cast_lossless_bool.rs:11:13
|
LL | let _ = true as u32;
| ^^^^^^^^^^^ help: try: `u32::from(true)`

error: casting `bool` to `u64` is more cleanly stated with `u64::from(_)`
--> $DIR/cast_lossless_bool.rs:11:13
--> $DIR/cast_lossless_bool.rs:12:13
|
LL | let _ = true as u64;
| ^^^^^^^^^^^ help: try: `u64::from(true)`

error: casting `bool` to `u128` is more cleanly stated with `u128::from(_)`
--> $DIR/cast_lossless_bool.rs:12:13
--> $DIR/cast_lossless_bool.rs:13:13
|
LL | let _ = true as u128;
| ^^^^^^^^^^^^ help: try: `u128::from(true)`

error: casting `bool` to `usize` is more cleanly stated with `usize::from(_)`
--> $DIR/cast_lossless_bool.rs:13:13
--> $DIR/cast_lossless_bool.rs:14:13
|
LL | let _ = true as usize;
| ^^^^^^^^^^^^^ help: try: `usize::from(true)`

error: casting `bool` to `i8` is more cleanly stated with `i8::from(_)`
--> $DIR/cast_lossless_bool.rs:15:13
--> $DIR/cast_lossless_bool.rs:16:13
|
LL | let _ = true as i8;
| ^^^^^^^^^^ help: try: `i8::from(true)`

error: casting `bool` to `i16` is more cleanly stated with `i16::from(_)`
--> $DIR/cast_lossless_bool.rs:16:13
--> $DIR/cast_lossless_bool.rs:17:13
|
LL | let _ = true as i16;
| ^^^^^^^^^^^ help: try: `i16::from(true)`

error: casting `bool` to `i32` is more cleanly stated with `i32::from(_)`
--> $DIR/cast_lossless_bool.rs:17:13
--> $DIR/cast_lossless_bool.rs:18:13
|
LL | let _ = true as i32;
| ^^^^^^^^^^^ help: try: `i32::from(true)`

error: casting `bool` to `i64` is more cleanly stated with `i64::from(_)`
--> $DIR/cast_lossless_bool.rs:18:13
--> $DIR/cast_lossless_bool.rs:19:13
|
LL | let _ = true as i64;
| ^^^^^^^^^^^ help: try: `i64::from(true)`

error: casting `bool` to `i128` is more cleanly stated with `i128::from(_)`
--> $DIR/cast_lossless_bool.rs:19:13
--> $DIR/cast_lossless_bool.rs:20:13
|
LL | let _ = true as i128;
| ^^^^^^^^^^^^ help: try: `i128::from(true)`

error: casting `bool` to `isize` is more cleanly stated with `isize::from(_)`
--> $DIR/cast_lossless_bool.rs:20:13
--> $DIR/cast_lossless_bool.rs:21:13
|
LL | let _ = true as isize;
| ^^^^^^^^^^^^^ help: try: `isize::from(true)`

error: casting `bool` to `u16` is more cleanly stated with `u16::from(_)`
--> $DIR/cast_lossless_bool.rs:23:13
--> $DIR/cast_lossless_bool.rs:24:13
|
LL | let _ = (true | false) as u16;
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `u16::from(true | false)`

error: aborting due to 13 previous errors
error: casting `bool` to `u8` is more cleanly stated with `u8::from(_)`
--> $DIR/cast_lossless_bool.rs:54:13
|
LL | let _ = true as u8;
| ^^^^^^^^^^ help: try: `u8::from(true)`

error: aborting due to 14 previous errors

16 changes: 15 additions & 1 deletion tests/ui/cfg_attr_rustfmt.fixed
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// run-rustfix
#![feature(stmt_expr_attributes)]
#![feature(stmt_expr_attributes, custom_inner_attributes)]

#![allow(unused, clippy::no_effect, clippy::unnecessary_operation)]
#![warn(clippy::deprecated_cfg_attr)]
Expand Down Expand Up @@ -29,3 +29,17 @@ mod foo {

pub fn f() {}
}

fn msrv_1_29() {
#![clippy::msrv = "1.29"]

#[cfg_attr(rustfmt, rustfmt::skip)]
1+29;
}

fn msrv_1_30() {
#![clippy::msrv = "1.30"]

#[rustfmt::skip]
1+30;
}
Loading