@@ -12,7 +12,7 @@ use rustc_ast::ptr::P;
1212use rustc_ast:: token;
1313use rustc_ast:: tokenstream:: TokenStream ;
1414use rustc_ast:: visit:: { self , AssocCtxt , Visitor } ;
15- use rustc_ast:: { AstLike , Block , Inline , ItemKind , Local , MacArgs } ;
15+ use rustc_ast:: { AstLike , Block , Inline , ItemKind , Local , MacArgs , MacCall } ;
1616use rustc_ast:: { MacCallStmt , MacStmtStyle , MetaItemKind , ModKind , NestedMetaItem } ;
1717use rustc_ast:: { NodeId , PatKind , Path , StmtKind , Unsafe } ;
1818use rustc_ast_pretty:: pprust;
@@ -26,7 +26,7 @@ use rustc_parse::parser::{
2626 AttemptLocalParseRecovery , ForceCollect , Parser , RecoverColon , RecoverComma ,
2727} ;
2828use rustc_parse:: validate_attr;
29- use rustc_session:: lint:: builtin:: UNUSED_DOC_COMMENTS ;
29+ use rustc_session:: lint:: builtin:: { UNUSED_ATTRIBUTES , UNUSED_DOC_COMMENTS } ;
3030use rustc_session:: lint:: BuiltinLintDiagnostics ;
3131use rustc_session:: parse:: { feature_err, ParseSess } ;
3232use rustc_session:: Limit ;
@@ -1070,7 +1070,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
10701070
10711071 // Detect use of feature-gated or invalid attributes on macro invocations
10721072 // since they will not be detected after macro expansion.
1073- fn check_attributes ( & mut self , attrs : & [ ast:: Attribute ] ) {
1073+ fn check_attributes ( & mut self , attrs : & [ ast:: Attribute ] , call : & MacCall ) {
10741074 let features = self . cx . ecfg . features . unwrap ( ) ;
10751075 let mut attrs = attrs. iter ( ) . peekable ( ) ;
10761076 let mut span: Option < Span > = None ;
@@ -1085,14 +1085,31 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
10851085 continue ;
10861086 }
10871087
1088- if attr. doc_str ( ) . is_some ( ) {
1088+ if attr. is_doc_comment ( ) {
10891089 self . cx . sess . parse_sess . buffer_lint_with_diagnostic (
10901090 & UNUSED_DOC_COMMENTS ,
10911091 current_span,
1092- ast :: CRATE_NODE_ID ,
1092+ self . cx . current_expansion . lint_node_id ,
10931093 "unused doc comment" ,
10941094 BuiltinLintDiagnostics :: UnusedDocComment ( attr. span ) ,
10951095 ) ;
1096+ } else if rustc_attr:: is_builtin_attr ( attr) {
1097+ let attr_name = attr. ident ( ) . unwrap ( ) . name ;
1098+ // `#[cfg]` and `#[cfg_attr]` are special - they are
1099+ // eagerly evaluated.
1100+ if attr_name != sym:: cfg && attr_name != sym:: cfg_attr {
1101+ self . cx . sess . parse_sess . buffer_lint_with_diagnostic (
1102+ & UNUSED_ATTRIBUTES ,
1103+ attr. span ,
1104+ self . cx . current_expansion . lint_node_id ,
1105+ & format ! ( "unused attribute `{}`" , attr_name) ,
1106+ BuiltinLintDiagnostics :: UnusedBuiltinAttribute {
1107+ attr_name,
1108+ macro_name : pprust:: path_to_string ( & call. path ) ,
1109+ invoc_span : call. path . span ,
1110+ } ,
1111+ ) ;
1112+ }
10961113 }
10971114 }
10981115 }
@@ -1152,7 +1169,7 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
11521169 }
11531170
11541171 if let ast:: ExprKind :: MacCall ( mac) = expr. kind {
1155- self . check_attributes ( & expr. attrs ) ;
1172+ self . check_attributes ( & expr. attrs , & mac ) ;
11561173 self . collect_bang ( mac, expr. span , AstFragmentKind :: Expr ) . make_expr ( ) . into_inner ( )
11571174 } else {
11581175 assign_id ! ( self , & mut expr. id, || {
@@ -1253,7 +1270,7 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
12531270 }
12541271
12551272 if let ast:: ExprKind :: MacCall ( mac) = expr. kind {
1256- self . check_attributes ( & expr. attrs ) ;
1273+ self . check_attributes ( & expr. attrs , & mac ) ;
12571274 self . collect_bang ( mac, expr. span , AstFragmentKind :: OptExpr )
12581275 . make_opt_expr ( )
12591276 . map ( |expr| expr. into_inner ( ) )
@@ -1296,7 +1313,7 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
12961313
12971314 if let StmtKind :: MacCall ( mac) = stmt. kind {
12981315 let MacCallStmt { mac, style, attrs, tokens : _ } = mac. into_inner ( ) ;
1299- self . check_attributes ( & attrs) ;
1316+ self . check_attributes ( & attrs, & mac ) ;
13001317 let mut placeholder =
13011318 self . collect_bang ( mac, stmt. span , AstFragmentKind :: Stmts ) . make_stmts ( ) ;
13021319
@@ -1344,9 +1361,9 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
13441361 let span = item. span ;
13451362
13461363 match item. kind {
1347- ast:: ItemKind :: MacCall ( ..) => {
1364+ ast:: ItemKind :: MacCall ( ref mac) => {
1365+ self . check_attributes ( & attrs, & mac) ;
13481366 item. attrs = attrs;
1349- self . check_attributes ( & item. attrs ) ;
13501367 item. and_then ( |item| match item. kind {
13511368 ItemKind :: MacCall ( mac) => {
13521369 self . collect_bang ( mac, span, AstFragmentKind :: Items ) . make_items ( )
@@ -1455,8 +1472,8 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
14551472 }
14561473
14571474 match item. kind {
1458- ast:: AssocItemKind :: MacCall ( .. ) => {
1459- self . check_attributes ( & item. attrs ) ;
1475+ ast:: AssocItemKind :: MacCall ( ref mac ) => {
1476+ self . check_attributes ( & item. attrs , & mac ) ;
14601477 item. and_then ( |item| match item. kind {
14611478 ast:: AssocItemKind :: MacCall ( mac) => self
14621479 . collect_bang ( mac, item. span , AstFragmentKind :: TraitItems )
@@ -1480,8 +1497,8 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
14801497 }
14811498
14821499 match item. kind {
1483- ast:: AssocItemKind :: MacCall ( .. ) => {
1484- self . check_attributes ( & item. attrs ) ;
1500+ ast:: AssocItemKind :: MacCall ( ref mac ) => {
1501+ self . check_attributes ( & item. attrs , & mac ) ;
14851502 item. and_then ( |item| match item. kind {
14861503 ast:: AssocItemKind :: MacCall ( mac) => self
14871504 . collect_bang ( mac, item. span , AstFragmentKind :: ImplItems )
@@ -1526,8 +1543,8 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
15261543 }
15271544
15281545 match foreign_item. kind {
1529- ast:: ForeignItemKind :: MacCall ( .. ) => {
1530- self . check_attributes ( & foreign_item. attrs ) ;
1546+ ast:: ForeignItemKind :: MacCall ( ref mac ) => {
1547+ self . check_attributes ( & foreign_item. attrs , & mac ) ;
15311548 foreign_item. and_then ( |item| match item. kind {
15321549 ast:: ForeignItemKind :: MacCall ( mac) => self
15331550 . collect_bang ( mac, item. span , AstFragmentKind :: ForeignItems )
0 commit comments