Skip to content

Commit 47e8c31

Browse files
committed
Make sure spans are from the root
1 parent bfd73cc commit 47e8c31

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

clippy_lints/src/vec.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub struct UselessVec {
2525
/// Maps from a `vec![]` source callsite invocation span to the "state" (i.e., whether we can
2626
/// emit a warning there or not).
2727
///
28-
/// The purpose of this is to buffer lints up until `check_expr_post` so that we can cancel a
28+
/// The purpose of this is to buffer lints up until `check_crate_post` so that we can cancel a
2929
/// lint while visiting, because a `vec![]` invocation span can appear multiple times when
3030
/// it is passed as a macro argument, once in a context that doesn't require a `Vec<_>` and
3131
/// another time that does. Consider:
@@ -187,7 +187,11 @@ impl<'tcx> LateLintPass<'tcx> for UselessVec {
187187
.checked_mul(length)
188188
.is_some_and(|size| size <= self.too_large_for_stack)
189189
{
190-
suggest_ty.snippet(cx, Some(expr.span), Some(len.span))
190+
suggest_ty.snippet(
191+
cx,
192+
Some(expr.span.source_callsite()),
193+
Some(len.span.source_callsite()),
194+
)
191195
} else {
192196
return;
193197
}
@@ -267,11 +271,17 @@ impl SuggestedType {
267271
}
268272

269273
fn snippet(self, cx: &LateContext<'_>, args_span: Option<Span>, len_span: Option<Span>) -> String {
274+
// Invariant of the lint as implemented: all spans are from the root context (and as a result,
275+
// always trivially crate-local).
276+
assert!(args_span.is_none_or(|s| !s.from_expansion()));
277+
assert!(len_span.is_none_or(|s| !s.from_expansion()));
278+
270279
let maybe_args = args_span
271-
.and_then(|sp| sp.get_source_text(cx))
280+
.map(|sp| sp.get_source_text(cx).expect("spans are always crate-local"))
272281
.map_or(String::new(), |x| x.to_owned());
273282
let maybe_len = len_span
274-
.and_then(|sp| sp.get_source_text(cx).map(|s| format!("; {s}")))
283+
.map(|sp| sp.get_source_text(cx).expect("spans are always crate-local"))
284+
.map(|st| format!("; {st}"))
275285
.unwrap_or_default();
276286

277287
match self {

0 commit comments

Comments
 (0)