@@ -7,6 +7,7 @@ use rustc_hir::{
7
7
StmtKind , TyKind , UnOp ,
8
8
} ;
9
9
use rustc_lint:: { LateContext , LateLintPass } ;
10
+ use rustc_middle:: lint:: in_external_macro;
10
11
use rustc_middle:: ty:: { self , Ty } ;
11
12
use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
12
13
use rustc_span:: hygiene:: DesugaringKind ;
@@ -271,13 +272,16 @@ impl<'tcx> LateLintPass<'tcx> for MiscLints {
271
272
k : FnKind < ' tcx > ,
272
273
decl : & ' tcx FnDecl < ' _ > ,
273
274
body : & ' tcx Body < ' _ > ,
274
- _ : Span ,
275
+ span : Span ,
275
276
_: HirId ,
276
277
) {
277
278
if let FnKind :: Closure ( _) = k {
278
279
// Does not apply to closures
279
280
return ;
280
281
}
282
+ if in_external_macro ( cx. tcx . sess , span) {
283
+ return ;
284
+ }
281
285
for arg in iter_input_pats ( decl, body) {
282
286
if let PatKind :: Binding ( BindingAnnotation :: Ref | BindingAnnotation :: RefMut , ..) = arg. pat . kind {
283
287
span_lint (
@@ -293,13 +297,16 @@ impl<'tcx> LateLintPass<'tcx> for MiscLints {
293
297
294
298
fn check_stmt ( & mut self , cx : & LateContext < ' tcx > , stmt : & ' tcx Stmt < ' _ > ) {
295
299
if_chain ! {
300
+ if !in_external_macro( cx. tcx. sess, stmt. span) ;
296
301
if let StmtKind :: Local ( ref local) = stmt. kind;
297
302
if let PatKind :: Binding ( an, .., name, None ) = local. pat. kind;
298
303
if let Some ( ref init) = local. init;
299
304
if !higher:: is_from_for_desugar( local) ;
300
305
then {
301
306
if an == BindingAnnotation :: Ref || an == BindingAnnotation :: RefMut {
302
- let sugg_init = if init. span. from_expansion( ) {
307
+ // use the macro callsite when the init span (but not the whole local span)
308
+ // comes from an expansion like `vec![1, 2, 3]` in `let ref _ = vec![1, 2, 3];`
309
+ let sugg_init = if init. span. from_expansion( ) && !local. span. from_expansion( ) {
303
310
Sugg :: hir_with_macro_callsite( cx, init, ".." )
304
311
} else {
305
312
Sugg :: hir( cx, init, ".." )
@@ -310,7 +317,7 @@ impl<'tcx> LateLintPass<'tcx> for MiscLints {
310
317
( "" , sugg_init. addr( ) )
311
318
} ;
312
319
let tyopt = if let Some ( ref ty) = local. ty {
313
- format!( ": &{mutopt}{ty}" , mutopt=mutopt, ty=snippet( cx, ty. span, "_ " ) )
320
+ format!( ": &{mutopt}{ty}" , mutopt=mutopt, ty=snippet( cx, ty. span, ".. " ) )
314
321
} else {
315
322
String :: new( )
316
323
} ;
@@ -326,7 +333,7 @@ impl<'tcx> LateLintPass<'tcx> for MiscLints {
326
333
"try" ,
327
334
format!(
328
335
"let {name}{tyopt} = {initref};" ,
329
- name=snippet( cx, name. span, "_ " ) ,
336
+ name=snippet( cx, name. span, ".. " ) ,
330
337
tyopt=tyopt,
331
338
initref=initref,
332
339
) ,
0 commit comments