@@ -417,6 +417,25 @@ impl EarlyLintPass for UnsafeCode {
417
417
}
418
418
}
419
419
420
+ fn check_impl_item ( & mut self , cx : & EarlyContext < ' _ > , it : & ast:: AssocItem ) {
421
+ if let ast:: AssocItemKind :: Fn ( ..) = it. kind {
422
+ if let Some ( attr) = cx. sess ( ) . find_by_name ( & it. attrs , sym:: no_mangle) {
423
+ self . report_overriden_symbol_name (
424
+ cx,
425
+ attr. span ,
426
+ "declaration of a `no_mangle` method" ,
427
+ ) ;
428
+ }
429
+ if let Some ( attr) = cx. sess ( ) . find_by_name ( & it. attrs , sym:: export_name) {
430
+ self . report_overriden_symbol_name (
431
+ cx,
432
+ attr. span ,
433
+ "declaration of a method with `export_name`" ,
434
+ ) ;
435
+ }
436
+ }
437
+ }
438
+
420
439
fn check_fn ( & mut self , cx : & EarlyContext < ' _ > , fk : FnKind < ' _ > , span : Span , _: ast:: NodeId ) {
421
440
if let FnKind :: Fn (
422
441
ctxt,
@@ -1115,31 +1134,37 @@ declare_lint_pass!(InvalidNoMangleItems => [NO_MANGLE_CONST_ITEMS, NO_MANGLE_GEN
1115
1134
impl < ' tcx > LateLintPass < ' tcx > for InvalidNoMangleItems {
1116
1135
fn check_item ( & mut self , cx : & LateContext < ' _ > , it : & hir:: Item < ' _ > ) {
1117
1136
let attrs = cx. tcx . hir ( ) . attrs ( it. hir_id ( ) ) ;
1137
+ let check_no_mangle_on_generic_fn = |no_mangle_attr : & ast:: Attribute ,
1138
+ impl_generics : Option < & hir:: Generics < ' _ > > ,
1139
+ generics : & hir:: Generics < ' _ > ,
1140
+ span| {
1141
+ for param in
1142
+ generics. params . iter ( ) . chain ( impl_generics. map ( |g| g. params ) . into_iter ( ) . flatten ( ) )
1143
+ {
1144
+ match param. kind {
1145
+ GenericParamKind :: Lifetime { .. } => { }
1146
+ GenericParamKind :: Type { .. } | GenericParamKind :: Const { .. } => {
1147
+ cx. struct_span_lint ( NO_MANGLE_GENERIC_ITEMS , span, |lint| {
1148
+ lint. build ( "functions generic over types or consts must be mangled" )
1149
+ . span_suggestion_short (
1150
+ no_mangle_attr. span ,
1151
+ "remove this attribute" ,
1152
+ String :: new ( ) ,
1153
+ // Use of `#[no_mangle]` suggests FFI intent; correct
1154
+ // fix may be to monomorphize source by hand
1155
+ Applicability :: MaybeIncorrect ,
1156
+ )
1157
+ . emit ( ) ;
1158
+ } ) ;
1159
+ break ;
1160
+ }
1161
+ }
1162
+ }
1163
+ } ;
1118
1164
match it. kind {
1119
1165
hir:: ItemKind :: Fn ( .., ref generics, _) => {
1120
1166
if let Some ( no_mangle_attr) = cx. sess ( ) . find_by_name ( attrs, sym:: no_mangle) {
1121
- for param in generics. params {
1122
- match param. kind {
1123
- GenericParamKind :: Lifetime { .. } => { }
1124
- GenericParamKind :: Type { .. } | GenericParamKind :: Const { .. } => {
1125
- cx. struct_span_lint ( NO_MANGLE_GENERIC_ITEMS , it. span , |lint| {
1126
- lint. build (
1127
- "functions generic over types or consts must be mangled" ,
1128
- )
1129
- . span_suggestion_short (
1130
- no_mangle_attr. span ,
1131
- "remove this attribute" ,
1132
- String :: new ( ) ,
1133
- // Use of `#[no_mangle]` suggests FFI intent; correct
1134
- // fix may be to monomorphize source by hand
1135
- Applicability :: MaybeIncorrect ,
1136
- )
1137
- . emit ( ) ;
1138
- } ) ;
1139
- break ;
1140
- }
1141
- }
1142
- }
1167
+ check_no_mangle_on_generic_fn ( no_mangle_attr, None , generics, it. span ) ;
1143
1168
}
1144
1169
}
1145
1170
hir:: ItemKind :: Const ( ..) => {
@@ -1170,6 +1195,23 @@ impl<'tcx> LateLintPass<'tcx> for InvalidNoMangleItems {
1170
1195
} ) ;
1171
1196
}
1172
1197
}
1198
+ hir:: ItemKind :: Impl ( hir:: Impl { ref generics, items, .. } ) => {
1199
+ for it in items {
1200
+ if let hir:: AssocItemKind :: Fn { .. } = it. kind {
1201
+ if let Some ( no_mangle_attr) = cx
1202
+ . sess ( )
1203
+ . find_by_name ( cx. tcx . hir ( ) . attrs ( it. id . hir_id ( ) ) , sym:: no_mangle)
1204
+ {
1205
+ check_no_mangle_on_generic_fn (
1206
+ no_mangle_attr,
1207
+ Some ( generics) ,
1208
+ cx. tcx . hir ( ) . get_generics ( it. id . def_id . to_def_id ( ) ) . unwrap ( ) ,
1209
+ it. span ,
1210
+ ) ;
1211
+ }
1212
+ }
1213
+ }
1214
+ }
1173
1215
_ => { }
1174
1216
}
1175
1217
}
0 commit comments