Skip to content

Commit f53f3f6

Browse files
committed
Report a better error than a panic in case there are suggestions for other files than the main file
1 parent addbd2c commit f53f3f6

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616

1717
### Fixed
1818

19+
* report an error instead of panicking when encountering a suggestion that does not belong to the main file.
20+
1921
### Changed
2022

2123
* crate-private span handling was passed off to the `spanned` crate, improving some diagnostics along the way.

src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,14 @@ fn run_rustfix(
874874
if suggestions.is_empty() {
875875
None
876876
} else {
877+
let path_str = path.display().to_string();
878+
for sugg in &suggestions {
879+
for snip in &sugg.snippets {
880+
if snip.file_name != path_str {
881+
return Some(Err(anyhow::anyhow!("cannot apply suggestions for `{}` since main file is `{path_str}`. Please use `//@no-rustfix` to disable rustfix", snip.file_name)));
882+
}
883+
}
884+
}
877885
Some(rustfix::apply_suggestions(
878886
&std::fs::read_to_string(path).unwrap(),
879887
&suggestions,

0 commit comments

Comments
 (0)