test(rustfix): demonstrate duplicate suggestions

This commit is contained in:
Weihang Lo 2024-04-09 18:07:47 -04:00
parent bd1cf584af
commit 5b05a3bb5a
No known key found for this signature in database
GPG Key ID: D7DBF189825E82E7
4 changed files with 84 additions and 0 deletions

View File

@ -0,0 +1,17 @@
// This fixes rust-lang/rust#123304.
// If that lint stops emitting duplicate suggestions,
// we might need to find a substitution.
#![warn(unsafe_op_in_unsafe_fn)]
macro_rules! foo {
($x:ident) => {
pub unsafe fn $x() { unsafe { unsafe {
let _ = String::new().as_mut_vec();
}}}
};
}
fn main() {
foo!(a);
foo!(b);
}

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,17 @@
// This fixes rust-lang/rust#123304.
// If that lint stops emitting duplicate suggestions,
// we might need to find a substitution.
#![warn(unsafe_op_in_unsafe_fn)]
macro_rules! foo {
($x:ident) => {
pub unsafe fn $x() {
let _ = String::new().as_mut_vec();
}
};
}
fn main() {
foo!(a);
foo!(b);
}

View File

@ -1897,3 +1897,48 @@ warning: `foo` (lib) generated 1 warning (run `cargo fix --lib -p foo` to apply
")
.run();
}
// This fixes rust-lang/rust#123304.
// If that lint stops emitting duplicate suggestions,
// we might need to find a substitution.
#[cargo_test]
fn fix_only_once_for_duplicates() {
let p = project()
.file(
"src/lib.rs",
r#"
#![warn(unsafe_op_in_unsafe_fn)]
macro_rules! foo {
($x:ident) => {
pub unsafe fn $x() {
let _ = String::new().as_mut_vec();
}
};
}
foo!(a);
foo!(b);
"#,
)
.build();
p.cargo("fix --allow-no-vcs")
.with_stderr_contains(
"\
[CHECKING] foo v0.0.1 ([CWD])
[FIXED] src/lib.rs (2 fixes)
",
)
.with_stderr_contains("[WARNING] unnecessary `unsafe` block[..]")
.run();
assert_eq!(
p.read_file("src/lib.rs").matches("unsafe").count(),
5,
"unsafe keyword in src/lib.rs:\n\
2 in lint name;\n\
1 from original unsafe fn;\n\
2 from newly-applied unsafe blocks"
);
}