Skip to content
This repository was archived by the owner on Nov 24, 2023. It is now read-only.

Commit f36c22a

Browse files
committed
Ignore duplicate replacements
Currently the compiler suffers from bugs like rust-lang/rust#51211 where it will emit duplicate suggestions for code that originates in a macro. This commit allows rustfix to accept and process these suggestions by ignoring exact duplicates of replacements. While not a great solution long term it should hopefully be a non-harmful band-aid until rustc is fixed!
1 parent c750e4d commit f36c22a

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

src/replace.rs

+26-1
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,23 @@ impl Data {
142142
})?;
143143

144144
let part_to_split = &self.parts[index_of_part_to_split];
145+
146+
// If this replacement matches exactly the part that we would
147+
// otherwise split then we ignore this for now. This means that you
148+
// can replace the exact same range with the exact same content
149+
// multiple times and we'll process and allow it.
150+
//
151+
// This is currently done to alleviate issues like
152+
// rust-lang/rust#51211 although this clause likely wants to be
153+
// removed if that's fixed deeper in the compiler.
154+
if part_to_split.start == from && part_to_split.end == up_to_and_including {
155+
if let State::Replaced(ref replacement) = part_to_split.data {
156+
if &**replacement == data {
157+
return Ok(())
158+
}
159+
}
160+
}
161+
145162
ensure!(
146163
part_to_split.data == State::Initial,
147164
"Cannot replace slice of data that was already replaced"
@@ -267,7 +284,7 @@ mod tests {
267284
d.replace_range(4, 6, b"lol").unwrap();
268285
assert_eq!("foo lol baz", str(&d.to_vec()));
269286

270-
d.replace_range(4, 6, b"lol").unwrap();
287+
d.replace_range(4, 6, b"lol2").unwrap();
271288
}
272289

273290
#[test]
@@ -277,6 +294,14 @@ mod tests {
277294
d.replace_range(4, 7, b"lol").unwrap();
278295
}
279296

297+
#[test]
298+
fn replace_same_twice() {
299+
let mut d = Data::new(b"foo");
300+
d.replace_range(0, 0, b"b").unwrap();
301+
d.replace_range(0, 0, b"b").unwrap();
302+
assert_eq!("boo", str(&d.to_vec()));
303+
}
304+
280305
proptest! {
281306
#[test]
282307
#[ignore]

0 commit comments

Comments
 (0)