@@ -25,7 +25,7 @@ pub struct UselessVec {
25
25
/// Maps from a `vec![]` source callsite invocation span to the "state" (i.e., whether we can
26
26
/// emit a warning there or not).
27
27
///
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
29
29
/// lint while visiting, because a `vec![]` invocation span can appear multiple times when
30
30
/// it is passed as a macro argument, once in a context that doesn't require a `Vec<_>` and
31
31
/// another time that does. Consider:
@@ -187,7 +187,11 @@ impl<'tcx> LateLintPass<'tcx> for UselessVec {
187
187
. checked_mul ( length)
188
188
. is_some_and ( |size| size <= self . too_large_for_stack )
189
189
{
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
+ )
191
195
} else {
192
196
return ;
193
197
}
@@ -267,11 +271,17 @@ impl SuggestedType {
267
271
}
268
272
269
273
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
+
270
279
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" ) )
272
281
. map_or ( String :: new ( ) , |x| x. to_owned ( ) ) ;
273
282
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}" ) )
275
285
. unwrap_or_default ( ) ;
276
286
277
287
match self {
0 commit comments