@@ -7,6 +7,7 @@ use rustc_hir::{
77 StmtKind , TyKind , UnOp ,
88} ;
99use rustc_lint:: { LateContext , LateLintPass } ;
10+ use rustc_middle:: lint:: in_external_macro;
1011use rustc_middle:: ty:: { self , Ty } ;
1112use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
1213use rustc_span:: hygiene:: DesugaringKind ;
@@ -271,13 +272,16 @@ impl<'tcx> LateLintPass<'tcx> for MiscLints {
271272 k : FnKind < ' tcx > ,
272273 decl : & ' tcx FnDecl < ' _ > ,
273274 body : & ' tcx Body < ' _ > ,
274- _ : Span ,
275+ span : Span ,
275276 _: HirId ,
276277 ) {
277278 if let FnKind :: Closure ( _) = k {
278279 // Does not apply to closures
279280 return ;
280281 }
282+ if in_external_macro ( cx. tcx . sess , span) {
283+ return ;
284+ }
281285 for arg in iter_input_pats ( decl, body) {
282286 if let PatKind :: Binding ( BindingAnnotation :: Ref | BindingAnnotation :: RefMut , ..) = arg. pat . kind {
283287 span_lint (
@@ -293,13 +297,16 @@ impl<'tcx> LateLintPass<'tcx> for MiscLints {
293297
294298 fn check_stmt ( & mut self , cx : & LateContext < ' tcx > , stmt : & ' tcx Stmt < ' _ > ) {
295299 if_chain ! {
300+ if !in_external_macro( cx. tcx. sess, stmt. span) ;
296301 if let StmtKind :: Local ( ref local) = stmt. kind;
297302 if let PatKind :: Binding ( an, .., name, None ) = local. pat. kind;
298303 if let Some ( ref init) = local. init;
299304 if !higher:: is_from_for_desugar( local) ;
300305 then {
301306 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( ) {
303310 Sugg :: hir_with_macro_callsite( cx, init, ".." )
304311 } else {
305312 Sugg :: hir( cx, init, ".." )
@@ -310,7 +317,7 @@ impl<'tcx> LateLintPass<'tcx> for MiscLints {
310317 ( "" , sugg_init. addr( ) )
311318 } ;
312319 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, ".. " ) )
314321 } else {
315322 String :: new( )
316323 } ;
@@ -326,7 +333,7 @@ impl<'tcx> LateLintPass<'tcx> for MiscLints {
326333 "try" ,
327334 format!(
328335 "let {name}{tyopt} = {initref};" ,
329- name=snippet( cx, name. span, "_ " ) ,
336+ name=snippet( cx, name. span, ".. " ) ,
330337 tyopt=tyopt,
331338 initref=initref,
332339 ) ,
0 commit comments