Skip to content

Commit a025eaa

Browse files
committed
fix(cargo-fix): dont apply same suggestion twice
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.
1 parent 33f3814 commit a025eaa

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

src/cargo/ops/fix.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,17 @@ fn rustfix_and_fix(
738738
// As mentioned above in `rustfix_crate`, we don't immediately warn
739739
// about suggestions that fail to apply here, and instead we save them
740740
// off for later processing.
741+
let mut already_applied = HashSet::new();
741742
for suggestion in suggestions.iter().rev() {
743+
// Skip seemly duplicate suggestions.
744+
if suggestion
745+
.solutions
746+
.iter()
747+
.any(|sol| !already_applied.insert(sol))
748+
{
749+
// already applied
750+
break;
751+
}
742752
match fixed.apply(suggestion) {
743753
Ok(()) => fixed_file.fixes_applied += 1,
744754
Err(e) => fixed_file.errors_applying_fixes.push(e.to_string()),

tests/testsuite/fix.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1924,21 +1924,21 @@ fn fix_only_once_for_duplicates() {
19241924
.build();
19251925

19261926
p.cargo("fix --allow-no-vcs")
1927-
.with_stderr_contains(
1927+
.with_stderr(
19281928
"\
19291929
[CHECKING] foo v0.0.1 ([CWD])
1930-
[FIXED] src/lib.rs (2 fixes)
1930+
[FIXED] src/lib.rs (1 fix)
1931+
[FINISHED] `dev` profile [..]
19311932
",
19321933
)
1933-
.with_stderr_contains("[WARNING] unnecessary `unsafe` block[..]")
19341934
.run();
19351935

19361936
assert_eq!(
19371937
p.read_file("src/lib.rs").matches("unsafe").count(),
1938-
5,
1938+
4,
19391939
"unsafe keyword in src/lib.rs:\n\
19401940
2 in lint name;\n\
19411941
1 from original unsafe fn;\n\
1942-
2 from newly-applied unsafe blocks"
1942+
1 from newly-applied unsafe blocks"
19431943
);
19441944
}

0 commit comments

Comments
 (0)