@@ -1016,11 +1016,9 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
1016
1016
1017
1017
fn collect_attr (
1018
1018
& mut self ,
1019
- attr : Option < ast:: Attribute > ,
1020
- derives : Vec < Path > ,
1019
+ ( attr, derives, after_derive) : ( Option < ast:: Attribute > , Vec < Path > , bool ) ,
1021
1020
item : Annotatable ,
1022
1021
kind : AstFragmentKind ,
1023
- after_derive : bool ,
1024
1022
) -> AstFragment {
1025
1023
self . collect (
1026
1024
kind,
@@ -1048,34 +1046,34 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
1048
1046
}
1049
1047
1050
1048
/// If `item` is an attr invocation, remove and return the macro attribute and derive traits.
1051
- fn classify_item (
1049
+ fn take_first_attr (
1052
1050
& mut self ,
1053
1051
item : & mut impl HasAttrs ,
1054
- ) -> ( Option < ast:: Attribute > , Vec < Path > , /* after_derive */ bool ) {
1052
+ ) -> Option < ( Option < ast:: Attribute > , Vec < Path > , /* after_derive */ bool ) > {
1055
1053
let ( mut attr, mut traits, mut after_derive) = ( None , Vec :: new ( ) , false ) ;
1056
1054
1057
1055
item. visit_attrs ( |mut attrs| {
1058
1056
attr = self . find_attr_invoc ( & mut attrs, & mut after_derive) ;
1059
1057
traits = collect_derives ( & mut self . cx , & mut attrs) ;
1060
1058
} ) ;
1061
1059
1062
- ( attr, traits, after_derive)
1060
+ if attr . is_some ( ) || !traits . is_empty ( ) { Some ( ( attr, traits, after_derive) ) } else { None }
1063
1061
}
1064
1062
1065
- /// Alternative to `classify_item ()` that ignores `#[derive]` so invocations fallthrough
1063
+ /// Alternative to `take_first_attr ()` that ignores `#[derive]` so invocations fallthrough
1066
1064
/// to the unused-attributes lint (making it an error on statements and expressions
1067
1065
/// is a breaking change)
1068
- fn classify_nonitem (
1066
+ fn take_first_attr_no_derive (
1069
1067
& mut self ,
1070
1068
nonitem : & mut impl HasAttrs ,
1071
- ) -> ( Option < ast:: Attribute > , /* after_derive */ bool ) {
1069
+ ) -> Option < ( Option < ast:: Attribute > , Vec < Path > , /* after_derive */ bool ) > {
1072
1070
let ( mut attr, mut after_derive) = ( None , false ) ;
1073
1071
1074
1072
nonitem. visit_attrs ( |mut attrs| {
1075
1073
attr = self . find_attr_invoc ( & mut attrs, & mut after_derive) ;
1076
1074
} ) ;
1077
1075
1078
- ( attr, after_derive)
1076
+ attr . map ( | attr| ( Some ( attr ) , Vec :: new ( ) , after_derive) )
1079
1077
}
1080
1078
1081
1079
fn configure < T : HasAttrs > ( & mut self , node : T ) -> Option < T > {
@@ -1119,23 +1117,14 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
1119
1117
visit_clobber ( expr. deref_mut ( ) , |mut expr| {
1120
1118
self . cfg . configure_expr_kind ( & mut expr. kind ) ;
1121
1119
1122
- // ignore derives so they remain unused
1123
- let ( attr, after_derive) = self . classify_nonitem ( & mut expr) ;
1124
-
1125
- if let Some ( ref attr_value) = attr {
1120
+ if let Some ( attr) = self . take_first_attr_no_derive ( & mut expr) {
1126
1121
// Collect the invoc regardless of whether or not attributes are permitted here
1127
1122
// expansion will eat the attribute so it won't error later.
1128
- self . cfg . maybe_emit_expr_attr_err ( attr_value ) ;
1123
+ attr . 0 . as_ref ( ) . map ( |attr| self . cfg . maybe_emit_expr_attr_err ( attr ) ) ;
1129
1124
1130
1125
// AstFragmentKind::Expr requires the macro to emit an expression.
1131
1126
return self
1132
- . collect_attr (
1133
- attr,
1134
- vec ! [ ] ,
1135
- Annotatable :: Expr ( P ( expr) ) ,
1136
- AstFragmentKind :: Expr ,
1137
- after_derive,
1138
- )
1127
+ . collect_attr ( attr, Annotatable :: Expr ( P ( expr) ) , AstFragmentKind :: Expr )
1139
1128
. make_expr ( )
1140
1129
. into_inner ( ) ;
1141
1130
}
@@ -1153,16 +1142,9 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
1153
1142
fn flat_map_arm ( & mut self , arm : ast:: Arm ) -> SmallVec < [ ast:: Arm ; 1 ] > {
1154
1143
let mut arm = configure ! ( self , arm) ;
1155
1144
1156
- let ( attr, traits, after_derive) = self . classify_item ( & mut arm) ;
1157
- if attr. is_some ( ) || !traits. is_empty ( ) {
1145
+ if let Some ( attr) = self . take_first_attr ( & mut arm) {
1158
1146
return self
1159
- . collect_attr (
1160
- attr,
1161
- traits,
1162
- Annotatable :: Arm ( arm) ,
1163
- AstFragmentKind :: Arms ,
1164
- after_derive,
1165
- )
1147
+ . collect_attr ( attr, Annotatable :: Arm ( arm) , AstFragmentKind :: Arms )
1166
1148
. make_arms ( ) ;
1167
1149
}
1168
1150
@@ -1172,16 +1154,9 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
1172
1154
fn flat_map_field ( & mut self , field : ast:: Field ) -> SmallVec < [ ast:: Field ; 1 ] > {
1173
1155
let mut field = configure ! ( self , field) ;
1174
1156
1175
- let ( attr, traits, after_derive) = self . classify_item ( & mut field) ;
1176
- if attr. is_some ( ) || !traits. is_empty ( ) {
1157
+ if let Some ( attr) = self . take_first_attr ( & mut field) {
1177
1158
return self
1178
- . collect_attr (
1179
- attr,
1180
- traits,
1181
- Annotatable :: Field ( field) ,
1182
- AstFragmentKind :: Fields ,
1183
- after_derive,
1184
- )
1159
+ . collect_attr ( attr, Annotatable :: Field ( field) , AstFragmentKind :: Fields )
1185
1160
. make_fields ( ) ;
1186
1161
}
1187
1162
@@ -1191,16 +1166,9 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
1191
1166
fn flat_map_field_pattern ( & mut self , fp : ast:: FieldPat ) -> SmallVec < [ ast:: FieldPat ; 1 ] > {
1192
1167
let mut fp = configure ! ( self , fp) ;
1193
1168
1194
- let ( attr, traits, after_derive) = self . classify_item ( & mut fp) ;
1195
- if attr. is_some ( ) || !traits. is_empty ( ) {
1169
+ if let Some ( attr) = self . take_first_attr ( & mut fp) {
1196
1170
return self
1197
- . collect_attr (
1198
- attr,
1199
- traits,
1200
- Annotatable :: FieldPat ( fp) ,
1201
- AstFragmentKind :: FieldPats ,
1202
- after_derive,
1203
- )
1171
+ . collect_attr ( attr, Annotatable :: FieldPat ( fp) , AstFragmentKind :: FieldPats )
1204
1172
. make_field_patterns ( ) ;
1205
1173
}
1206
1174
@@ -1210,16 +1178,9 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
1210
1178
fn flat_map_param ( & mut self , p : ast:: Param ) -> SmallVec < [ ast:: Param ; 1 ] > {
1211
1179
let mut p = configure ! ( self , p) ;
1212
1180
1213
- let ( attr, traits, after_derive) = self . classify_item ( & mut p) ;
1214
- if attr. is_some ( ) || !traits. is_empty ( ) {
1181
+ if let Some ( attr) = self . take_first_attr ( & mut p) {
1215
1182
return self
1216
- . collect_attr (
1217
- attr,
1218
- traits,
1219
- Annotatable :: Param ( p) ,
1220
- AstFragmentKind :: Params ,
1221
- after_derive,
1222
- )
1183
+ . collect_attr ( attr, Annotatable :: Param ( p) , AstFragmentKind :: Params )
1223
1184
. make_params ( ) ;
1224
1185
}
1225
1186
@@ -1229,16 +1190,9 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
1229
1190
fn flat_map_struct_field ( & mut self , sf : ast:: StructField ) -> SmallVec < [ ast:: StructField ; 1 ] > {
1230
1191
let mut sf = configure ! ( self , sf) ;
1231
1192
1232
- let ( attr, traits, after_derive) = self . classify_item ( & mut sf) ;
1233
- if attr. is_some ( ) || !traits. is_empty ( ) {
1193
+ if let Some ( attr) = self . take_first_attr ( & mut sf) {
1234
1194
return self
1235
- . collect_attr (
1236
- attr,
1237
- traits,
1238
- Annotatable :: StructField ( sf) ,
1239
- AstFragmentKind :: StructFields ,
1240
- after_derive,
1241
- )
1195
+ . collect_attr ( attr, Annotatable :: StructField ( sf) , AstFragmentKind :: StructFields )
1242
1196
. make_struct_fields ( ) ;
1243
1197
}
1244
1198
@@ -1248,16 +1202,9 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
1248
1202
fn flat_map_variant ( & mut self , variant : ast:: Variant ) -> SmallVec < [ ast:: Variant ; 1 ] > {
1249
1203
let mut variant = configure ! ( self , variant) ;
1250
1204
1251
- let ( attr, traits, after_derive) = self . classify_item ( & mut variant) ;
1252
- if attr. is_some ( ) || !traits. is_empty ( ) {
1205
+ if let Some ( attr) = self . take_first_attr ( & mut variant) {
1253
1206
return self
1254
- . collect_attr (
1255
- attr,
1256
- traits,
1257
- Annotatable :: Variant ( variant) ,
1258
- AstFragmentKind :: Variants ,
1259
- after_derive,
1260
- )
1207
+ . collect_attr ( attr, Annotatable :: Variant ( variant) , AstFragmentKind :: Variants )
1261
1208
. make_variants ( ) ;
1262
1209
}
1263
1210
@@ -1269,20 +1216,11 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
1269
1216
expr. filter_map ( |mut expr| {
1270
1217
self . cfg . configure_expr_kind ( & mut expr. kind ) ;
1271
1218
1272
- // Ignore derives so they remain unused.
1273
- let ( attr, after_derive) = self . classify_nonitem ( & mut expr) ;
1274
-
1275
- if let Some ( ref attr_value) = attr {
1276
- self . cfg . maybe_emit_expr_attr_err ( attr_value) ;
1219
+ if let Some ( attr) = self . take_first_attr_no_derive ( & mut expr) {
1220
+ attr. 0 . as_ref ( ) . map ( |attr| self . cfg . maybe_emit_expr_attr_err ( attr) ) ;
1277
1221
1278
1222
return self
1279
- . collect_attr (
1280
- attr,
1281
- vec ! [ ] ,
1282
- Annotatable :: Expr ( P ( expr) ) ,
1283
- AstFragmentKind :: OptExpr ,
1284
- after_derive,
1285
- )
1223
+ . collect_attr ( attr, Annotatable :: Expr ( P ( expr) ) , AstFragmentKind :: OptExpr )
1286
1224
. make_opt_expr ( )
1287
1225
. map ( |expr| expr. into_inner ( ) ) ;
1288
1226
}
@@ -1321,25 +1259,13 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
1321
1259
1322
1260
// we'll expand attributes on expressions separately
1323
1261
if !stmt. is_expr ( ) {
1324
- let ( attr, derives, after_derive) = if stmt. is_item ( ) {
1325
- // FIXME: Handle custom attributes on statements (#15701)
1326
- ( None , vec ! [ ] , false )
1327
- } else {
1328
- // ignore derives on non-item statements so it falls through
1329
- // to the unused-attributes lint
1330
- let ( attr, after_derive) = self . classify_nonitem ( & mut stmt) ;
1331
- ( attr, vec ! [ ] , after_derive)
1332
- } ;
1262
+ // FIXME: Handle custom attributes on statements (#15701).
1263
+ let attr =
1264
+ if stmt. is_item ( ) { None } else { self . take_first_attr_no_derive ( & mut stmt) } ;
1333
1265
1334
- if attr . is_some ( ) || !derives . is_empty ( ) {
1266
+ if let Some ( attr ) = attr {
1335
1267
return self
1336
- . collect_attr (
1337
- attr,
1338
- derives,
1339
- Annotatable :: Stmt ( P ( stmt) ) ,
1340
- AstFragmentKind :: Stmts ,
1341
- after_derive,
1342
- )
1268
+ . collect_attr ( attr, Annotatable :: Stmt ( P ( stmt) ) , AstFragmentKind :: Stmts )
1343
1269
. make_stmts ( ) ;
1344
1270
}
1345
1271
}
@@ -1379,16 +1305,9 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
1379
1305
fn flat_map_item ( & mut self , item : P < ast:: Item > ) -> SmallVec < [ P < ast:: Item > ; 1 ] > {
1380
1306
let mut item = configure ! ( self , item) ;
1381
1307
1382
- let ( attr, traits, after_derive) = self . classify_item ( & mut item) ;
1383
- if attr. is_some ( ) || !traits. is_empty ( ) {
1308
+ if let Some ( attr) = self . take_first_attr ( & mut item) {
1384
1309
return self
1385
- . collect_attr (
1386
- attr,
1387
- traits,
1388
- Annotatable :: Item ( item) ,
1389
- AstFragmentKind :: Items ,
1390
- after_derive,
1391
- )
1310
+ . collect_attr ( attr, Annotatable :: Item ( item) , AstFragmentKind :: Items )
1392
1311
. make_items ( ) ;
1393
1312
}
1394
1313
@@ -1482,16 +1401,9 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
1482
1401
fn flat_map_trait_item ( & mut self , item : P < ast:: AssocItem > ) -> SmallVec < [ P < ast:: AssocItem > ; 1 ] > {
1483
1402
let mut item = configure ! ( self , item) ;
1484
1403
1485
- let ( attr, traits, after_derive) = self . classify_item ( & mut item) ;
1486
- if attr. is_some ( ) || !traits. is_empty ( ) {
1404
+ if let Some ( attr) = self . take_first_attr ( & mut item) {
1487
1405
return self
1488
- . collect_attr (
1489
- attr,
1490
- traits,
1491
- Annotatable :: TraitItem ( item) ,
1492
- AstFragmentKind :: TraitItems ,
1493
- after_derive,
1494
- )
1406
+ . collect_attr ( attr, Annotatable :: TraitItem ( item) , AstFragmentKind :: TraitItems )
1495
1407
. make_trait_items ( ) ;
1496
1408
}
1497
1409
@@ -1512,16 +1424,9 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
1512
1424
fn flat_map_impl_item ( & mut self , item : P < ast:: AssocItem > ) -> SmallVec < [ P < ast:: AssocItem > ; 1 ] > {
1513
1425
let mut item = configure ! ( self , item) ;
1514
1426
1515
- let ( attr, traits, after_derive) = self . classify_item ( & mut item) ;
1516
- if attr. is_some ( ) || !traits. is_empty ( ) {
1427
+ if let Some ( attr) = self . take_first_attr ( & mut item) {
1517
1428
return self
1518
- . collect_attr (
1519
- attr,
1520
- traits,
1521
- Annotatable :: ImplItem ( item) ,
1522
- AstFragmentKind :: ImplItems ,
1523
- after_derive,
1524
- )
1429
+ . collect_attr ( attr, Annotatable :: ImplItem ( item) , AstFragmentKind :: ImplItems )
1525
1430
. make_impl_items ( ) ;
1526
1431
}
1527
1432
@@ -1562,16 +1467,12 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
1562
1467
& mut self ,
1563
1468
mut foreign_item : P < ast:: ForeignItem > ,
1564
1469
) -> SmallVec < [ P < ast:: ForeignItem > ; 1 ] > {
1565
- let ( attr, traits, after_derive) = self . classify_item ( & mut foreign_item) ;
1566
-
1567
- if attr. is_some ( ) || !traits. is_empty ( ) {
1470
+ if let Some ( attr) = self . take_first_attr ( & mut foreign_item) {
1568
1471
return self
1569
1472
. collect_attr (
1570
1473
attr,
1571
- traits,
1572
1474
Annotatable :: ForeignItem ( foreign_item) ,
1573
1475
AstFragmentKind :: ForeignItems ,
1574
- after_derive,
1575
1476
)
1576
1477
. make_foreign_items ( ) ;
1577
1478
}
@@ -1606,15 +1507,12 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
1606
1507
) -> SmallVec < [ ast:: GenericParam ; 1 ] > {
1607
1508
let mut param = configure ! ( self , param) ;
1608
1509
1609
- let ( attr, traits, after_derive) = self . classify_item ( & mut param) ;
1610
- if attr. is_some ( ) || !traits. is_empty ( ) {
1510
+ if let Some ( attr) = self . take_first_attr ( & mut param) {
1611
1511
return self
1612
1512
. collect_attr (
1613
1513
attr,
1614
- traits,
1615
1514
Annotatable :: GenericParam ( param) ,
1616
1515
AstFragmentKind :: GenericParams ,
1617
- after_derive,
1618
1516
)
1619
1517
. make_generic_params ( ) ;
1620
1518
}
0 commit comments