@@ -40,9 +40,9 @@ use syntax_pos::{BytePos, Span, SyntaxContext};
40
40
use syntax:: symbol:: keywords;
41
41
use syntax:: errors:: { Applicability , DiagnosticBuilder } ;
42
42
use syntax:: print:: pprust:: expr_to_string;
43
+ use syntax:: visit:: FnKind ;
43
44
44
45
use rustc:: hir:: { self , GenericParamKind , PatKind } ;
45
- use rustc:: hir:: intravisit:: FnKind ;
46
46
47
47
use nonstandard_style:: { MethodLateContext , method_context} ;
48
48
@@ -216,7 +216,7 @@ impl LintPass for UnsafeCode {
216
216
}
217
217
218
218
impl UnsafeCode {
219
- fn report_unsafe ( & self , cx : & LateContext , span : Span , desc : & ' static str ) {
219
+ fn report_unsafe ( & self , cx : & EarlyContext , span : Span , desc : & ' static str ) {
220
220
// This comes from a macro that has #[allow_internal_unsafe].
221
221
if span. allows_unsafe ( ) {
222
222
return ;
@@ -226,23 +226,31 @@ impl UnsafeCode {
226
226
}
227
227
}
228
228
229
- impl < ' a , ' tcx > LateLintPass < ' a , ' tcx > for UnsafeCode {
230
- fn check_expr ( & mut self , cx : & LateContext , e : & hir:: Expr ) {
231
- if let hir:: ExprKind :: Block ( ref blk, _) = e. node {
229
+ impl EarlyLintPass for UnsafeCode {
230
+ fn check_attribute ( & mut self , cx : & EarlyContext , attr : & ast:: Attribute ) {
231
+ if attr. check_name ( "allow_internal_unsafe" ) {
232
+ self . report_unsafe ( cx, attr. span , "`allow_internal_unsafe` allows defining \
233
+ macros using unsafe without triggering \
234
+ the `unsafe_code` lint at their call site") ;
235
+ }
236
+ }
237
+
238
+ fn check_expr ( & mut self , cx : & EarlyContext , e : & ast:: Expr ) {
239
+ if let ast:: ExprKind :: Block ( ref blk, _) = e. node {
232
240
// Don't warn about generated blocks, that'll just pollute the output.
233
- if blk. rules == hir :: UnsafeBlock ( hir :: UserProvided ) {
241
+ if blk. rules == ast :: BlockCheckMode :: Unsafe ( ast :: UserProvided ) {
234
242
self . report_unsafe ( cx, blk. span , "usage of an `unsafe` block" ) ;
235
243
}
236
244
}
237
245
}
238
246
239
- fn check_item ( & mut self , cx : & LateContext , it : & hir :: Item ) {
247
+ fn check_item ( & mut self , cx : & EarlyContext , it : & ast :: Item ) {
240
248
match it. node {
241
- hir :: ItemKind :: Trait ( _, hir :: Unsafety :: Unsafe , ..) => {
249
+ ast :: ItemKind :: Trait ( _, ast :: Unsafety :: Unsafe , ..) => {
242
250
self . report_unsafe ( cx, it. span , "declaration of an `unsafe` trait" )
243
251
}
244
252
245
- hir :: ItemKind :: Impl ( hir :: Unsafety :: Unsafe , ..) => {
253
+ ast :: ItemKind :: Impl ( ast :: Unsafety :: Unsafe , ..) => {
246
254
self . report_unsafe ( cx, it. span , "implementation of an `unsafe` trait" )
247
255
}
248
256
@@ -251,19 +259,18 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnsafeCode {
251
259
}
252
260
253
261
fn check_fn ( & mut self ,
254
- cx : & LateContext ,
255
- fk : FnKind < ' tcx > ,
256
- _: & hir:: FnDecl ,
257
- _: & hir:: Body ,
262
+ cx : & EarlyContext ,
263
+ fk : FnKind ,
264
+ _: & ast:: FnDecl ,
258
265
span : Span ,
259
266
_: ast:: NodeId ) {
260
267
match fk {
261
- FnKind :: ItemFn ( _, _ , hir :: FnHeader { unsafety : hir :: Unsafety :: Unsafe , .. } , ..) => {
268
+ FnKind :: ItemFn ( _, ast :: FnHeader { unsafety : ast :: Unsafety :: Unsafe , .. } , ..) => {
262
269
self . report_unsafe ( cx, span, "declaration of an `unsafe` function" )
263
270
}
264
271
265
272
FnKind :: Method ( _, sig, ..) => {
266
- if sig. header . unsafety == hir :: Unsafety :: Unsafe {
273
+ if sig. header . unsafety == ast :: Unsafety :: Unsafe {
267
274
self . report_unsafe ( cx, span, "implementation of an `unsafe` method" )
268
275
}
269
276
}
@@ -272,9 +279,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnsafeCode {
272
279
}
273
280
}
274
281
275
- fn check_trait_item ( & mut self , cx : & LateContext , item : & hir :: TraitItem ) {
276
- if let hir :: TraitItemKind :: Method ( ref sig, hir :: TraitMethod :: Required ( _ ) ) = item. node {
277
- if sig. header . unsafety == hir :: Unsafety :: Unsafe {
282
+ fn check_trait_item ( & mut self , cx : & EarlyContext , item : & ast :: TraitItem ) {
283
+ if let ast :: TraitItemKind :: Method ( ref sig, None ) = item. node {
284
+ if sig. header . unsafety == ast :: Unsafety :: Unsafe {
278
285
self . report_unsafe ( cx, item. span , "declaration of an `unsafe` method" )
279
286
}
280
287
}
0 commit comments