Skip to content

Commit 90aee40

Browse files
committed
Auto merge of #14710 - weihanglo:rustfix, r=epage
refactor(rustfix): minor refactors
2 parents 45ad483 + c9102e5 commit 90aee40

File tree

4 files changed

+79
-90
lines changed

4 files changed

+79
-90
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/rustfix/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rustfix"
3-
version = "0.8.7"
3+
version = "0.8.8"
44
authors = [
55
"Pascal Hertleif <[email protected]>",
66
"Oliver Schneider <[email protected]>",

crates/rustfix/src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,6 @@ pub fn collect_suggestions<S: ::std::hash::BuildHasher>(
167167
}
168168
}
169169

170-
let snippets = diagnostic.spans.iter().map(span_to_snippet).collect();
171-
172170
let solutions: Vec<_> = diagnostic
173171
.children
174172
.iter()
@@ -204,7 +202,7 @@ pub fn collect_suggestions<S: ::std::hash::BuildHasher>(
204202
} else {
205203
Some(Suggestion {
206204
message: diagnostic.message.clone(),
207-
snippets,
205+
snippets: diagnostic.spans.iter().map(span_to_snippet).collect(),
208206
solutions,
209207
})
210208
}

crates/rustfix/src/replace.rs

Lines changed: 76 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ impl State {
2424
}
2525

2626
/// Span with a change [`State`].
27-
#[derive(Debug, Clone, PartialEq, Eq)]
27+
#[derive(Clone, PartialEq, Eq)]
2828
struct Span {
2929
/// Start of this span in parent data
3030
start: usize,
@@ -34,6 +34,17 @@ struct Span {
3434
data: State,
3535
}
3636

37+
impl std::fmt::Debug for Span {
38+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39+
let state = match self.data {
40+
State::Initial => "initial",
41+
State::Replaced(_) => "replaced",
42+
State::Inserted(_) => "inserted",
43+
};
44+
write!(f, "({}, {}: {state})", self.start, self.end)
45+
}
46+
}
47+
3748
/// A container that allows easily replacing chunks of its data
3849
#[derive(Debug, Clone, Default)]
3950
pub struct Data {
@@ -97,102 +108,82 @@ impl Data {
97108
// [^empty]: Leading and trailing ones might be empty if we replace
98109
// the whole chunk. As an optimization and without loss of generality we
99110
// don't add empty parts.
100-
let new_parts = {
101-
let Some(index_of_part_to_split) = self.parts.iter().position(|p| {
102-
!p.data.is_inserted() && p.start <= range.start && p.end >= range.end
103-
}) else {
104-
if tracing::enabled!(tracing::Level::DEBUG) {
105-
let slices = self
106-
.parts
107-
.iter()
108-
.map(|p| {
109-
(
110-
p.start,
111-
p.end,
112-
match p.data {
113-
State::Initial => "initial",
114-
State::Replaced(..) => "replaced",
115-
State::Inserted(..) => "inserted",
116-
},
117-
)
118-
})
119-
.collect::<Vec<_>>();
120-
tracing::debug!(
121-
"no single slice covering {}..{}, current slices: {:?}",
122-
range.start,
123-
range.end,
124-
slices,
125-
);
126-
}
111+
let Some(index_of_part_to_split) = self
112+
.parts
113+
.iter()
114+
.position(|p| !p.data.is_inserted() && p.start <= range.start && p.end >= range.end)
115+
else {
116+
tracing::debug!(
117+
"no single slice covering {}..{}, current slices: {:?}",
118+
range.start,
119+
range.end,
120+
self.parts,
121+
);
122+
return Err(Error::MaybeAlreadyReplaced(range));
123+
};
127124

128-
return Err(Error::MaybeAlreadyReplaced(range));
129-
};
125+
let part_to_split = &self.parts[index_of_part_to_split];
130126

131-
let part_to_split = &self.parts[index_of_part_to_split];
132-
133-
// If this replacement matches exactly the part that we would
134-
// otherwise split then we ignore this for now. This means that you
135-
// can replace the exact same range with the exact same content
136-
// multiple times and we'll process and allow it.
137-
//
138-
// This is currently done to alleviate issues like
139-
// rust-lang/rust#51211 although this clause likely wants to be
140-
// removed if that's fixed deeper in the compiler.
141-
if part_to_split.start == range.start && part_to_split.end == range.end {
142-
if let State::Replaced(ref replacement) = part_to_split.data {
143-
if &**replacement == data {
144-
return Ok(());
145-
}
127+
// If this replacement matches exactly the part that we would
128+
// otherwise split then we ignore this for now. This means that you
129+
// can replace the exact same range with the exact same content
130+
// multiple times and we'll process and allow it.
131+
//
132+
// This is currently done to alleviate issues like
133+
// rust-lang/rust#51211 although this clause likely wants to be
134+
// removed if that's fixed deeper in the compiler.
135+
if part_to_split.start == range.start && part_to_split.end == range.end {
136+
if let State::Replaced(ref replacement) = part_to_split.data {
137+
if &**replacement == data {
138+
return Ok(());
146139
}
147140
}
141+
}
148142

149-
if part_to_split.data != State::Initial {
150-
return Err(Error::AlreadyReplaced);
151-
}
152-
153-
let mut new_parts = Vec::with_capacity(self.parts.len() + 2);
143+
if part_to_split.data != State::Initial {
144+
return Err(Error::AlreadyReplaced);
145+
}
154146

155-
// Previous parts
156-
if let Some(ps) = self.parts.get(..index_of_part_to_split) {
157-
new_parts.extend_from_slice(ps);
158-
}
147+
let mut new_parts = Vec::with_capacity(self.parts.len() + 2);
159148

160-
// Keep initial data on left side of part
161-
if range.start > part_to_split.start {
162-
new_parts.push(Span {
163-
start: part_to_split.start,
164-
end: range.start,
165-
data: State::Initial,
166-
});
167-
}
149+
// Previous parts
150+
if let Some(ps) = self.parts.get(..index_of_part_to_split) {
151+
new_parts.extend_from_slice(ps);
152+
}
168153

169-
// New part
154+
// Keep initial data on left side of part
155+
if range.start > part_to_split.start {
170156
new_parts.push(Span {
171-
start: range.start,
172-
end: range.end,
173-
data: if insert_only {
174-
State::Inserted(data.into())
175-
} else {
176-
State::Replaced(data.into())
177-
},
157+
start: part_to_split.start,
158+
end: range.start,
159+
data: State::Initial,
178160
});
161+
}
179162

180-
// Keep initial data on right side of part
181-
if range.end < part_to_split.end {
182-
new_parts.push(Span {
183-
start: range.end,
184-
end: part_to_split.end,
185-
data: State::Initial,
186-
});
187-
}
188-
189-
// Following parts
190-
if let Some(ps) = self.parts.get(index_of_part_to_split + 1..) {
191-
new_parts.extend_from_slice(ps);
192-
}
163+
// New part
164+
new_parts.push(Span {
165+
start: range.start,
166+
end: range.end,
167+
data: if insert_only {
168+
State::Inserted(data.into())
169+
} else {
170+
State::Replaced(data.into())
171+
},
172+
});
173+
174+
// Keep initial data on right side of part
175+
if range.end < part_to_split.end {
176+
new_parts.push(Span {
177+
start: range.end,
178+
end: part_to_split.end,
179+
data: State::Initial,
180+
});
181+
}
193182

194-
new_parts
195-
};
183+
// Following parts
184+
if let Some(ps) = self.parts.get(index_of_part_to_split + 1..) {
185+
new_parts.extend_from_slice(ps);
186+
}
196187

197188
self.parts = new_parts;
198189

0 commit comments

Comments
 (0)