Skip to content

Commit

Permalink
fix(cargo-fix): dont apply same suggestion twice
Browse files Browse the repository at this point in the history
This assumes that if any of the machine applicable fixes (`solution`)
in a diagnostic is a duplicate, then `cargo fix` should only apply
the entire suggestion once.
  • Loading branch information
weihanglo committed Apr 9, 2024
1 parent 3af94ad commit 4542746
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
11 changes: 11 additions & 0 deletions src/cargo/ops/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,18 @@ fn rustfix_and_fix(
// As mentioned above in `rustfix_crate`, we don't immediately warn
// about suggestions that fail to apply here, and instead we save them
// off for later processing.
let mut already_applied = HashSet::new();
for suggestion in suggestions.iter().rev() {
// Skip seemly duplicated suggestions.
if suggestion
.solutions
.iter()
.any(|sol| !already_applied.insert(sol))
{
// already applied
fixed_file.fixes_applied += 1;
break;
}
match fixed.apply(suggestion) {
Ok(()) => fixed_file.fixes_applied += 1,
Err(e) => fixed_file.errors_applying_fixes.push(e.to_string()),
Expand Down
6 changes: 3 additions & 3 deletions tests/testsuite/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1921,14 +1921,14 @@ fn fix_only_once_for_duplicates() {
.build();

p.cargo("fix --allow-no-vcs")
.with_stderr_contains(
.with_stderr(
"\
[CHECKING] foo v0.0.1 ([CWD])
[FIXED] src/lib.rs (2 fixes)
[FINISHED] `dev` profile [..]
",
)
.with_stderr_contains("[WARNING] unnecessary `unsafe` block[..]")
.run();

assert!(p.read_file("src/lib.rs").contains("unsafe { unsafe"));
assert!(!p.read_file("src/lib.rs").contains("unsafe { unsafe"));
}

0 comments on commit 4542746

Please sign in to comment.