@@ -31,8 +31,8 @@ use crate::{
3131 BuiltinMutablesTransmutes , BuiltinNoMangleGeneric , BuiltinNonShorthandFieldPatterns ,
3232 BuiltinSpecialModuleNameUsed , BuiltinTrivialBounds , BuiltinUnexpectedCliConfigName ,
3333 BuiltinUnexpectedCliConfigValue , BuiltinUnnameableTestItems , BuiltinUnreachablePub ,
34- BuiltinUnstableFeatures , BuiltinUnusedDocComment , BuiltinUnusedDocCommentSub ,
35- BuiltinWhileTrue ,
34+ BuiltinUnsafe , BuiltinUnstableFeatures , BuiltinUnusedDocComment ,
35+ BuiltinUnusedDocCommentSub , BuiltinWhileTrue ,
3636 } ,
3737 types:: { transparent_newtype_field, CItemKind } ,
3838 EarlyContext , EarlyLintPass , LateContext , LateLintPass , LintContext ,
@@ -46,8 +46,7 @@ use rustc_ast_pretty::pprust::{self, expr_to_string};
4646use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
4747use rustc_data_structures:: stack:: ensure_sufficient_stack;
4848use rustc_errors:: {
49- fluent, Applicability , DelayDm , Diagnostic , DiagnosticBuilder , DiagnosticMessage ,
50- DiagnosticStyledString , MultiSpan ,
49+ fluent, Applicability , DecorateLint , DelayDm , Diagnostic , DiagnosticStyledString , MultiSpan ,
5150} ;
5251use rustc_feature:: { deprecated_attributes, AttributeGate , BuiltinAttribute , GateIssue , Stability } ;
5352use rustc_hir as hir;
@@ -314,48 +313,21 @@ impl UnsafeCode {
314313 & self ,
315314 cx : & EarlyContext < ' _ > ,
316315 span : Span ,
317- msg : impl Into < DiagnosticMessage > ,
318- decorate : impl for <' a , ' b > FnOnce (
319- & ' b mut DiagnosticBuilder < ' a , ( ) > ,
320- ) -> & ' b mut DiagnosticBuilder < ' a , ( ) > ,
316+ decorate : impl for < ' a > DecorateLint < ' a , ( ) > ,
321317 ) {
322318 // This comes from a macro that has `#[allow_internal_unsafe]`.
323319 if span. allows_unsafe ( ) {
324320 return ;
325321 }
326322
327- cx. struct_span_lint ( UNSAFE_CODE , span, msg, decorate) ;
328- }
329-
330- fn report_overridden_symbol_name (
331- & self ,
332- cx : & EarlyContext < ' _ > ,
333- span : Span ,
334- msg : DiagnosticMessage ,
335- ) {
336- self . report_unsafe ( cx, span, msg, |lint| {
337- lint. note ( fluent:: lint_builtin_overridden_symbol_name)
338- } )
339- }
340-
341- fn report_overridden_symbol_section (
342- & self ,
343- cx : & EarlyContext < ' _ > ,
344- span : Span ,
345- msg : DiagnosticMessage ,
346- ) {
347- self . report_unsafe ( cx, span, msg, |lint| {
348- lint. note ( fluent:: lint_builtin_overridden_symbol_section)
349- } )
323+ cx. emit_spanned_lint ( UNSAFE_CODE , span, decorate) ;
350324 }
351325}
352326
353327impl EarlyLintPass for UnsafeCode {
354328 fn check_attribute ( & mut self , cx : & EarlyContext < ' _ > , attr : & ast:: Attribute ) {
355329 if attr. has_name ( sym:: allow_internal_unsafe) {
356- self . report_unsafe ( cx, attr. span , fluent:: lint_builtin_allow_internal_unsafe, |lint| {
357- lint
358- } ) ;
330+ self . report_unsafe ( cx, attr. span , BuiltinUnsafe :: AllowInternalUnsafe ) ;
359331 }
360332 }
361333
@@ -364,70 +336,46 @@ impl EarlyLintPass for UnsafeCode {
364336 if let ast:: ExprKind :: Block ( ref blk, _) = e. kind {
365337 // Don't warn about generated blocks; that'll just pollute the output.
366338 if blk. rules == ast:: BlockCheckMode :: Unsafe ( ast:: UserProvided ) {
367- self . report_unsafe ( cx, blk. span , fluent :: lint_builtin_unsafe_block , |lint| lint ) ;
339+ self . report_unsafe ( cx, blk. span , BuiltinUnsafe :: UnsafeBlock ) ;
368340 }
369341 }
370342 }
371343
372344 fn check_item ( & mut self , cx : & EarlyContext < ' _ > , it : & ast:: Item ) {
373345 match it. kind {
374346 ast:: ItemKind :: Trait ( box ast:: Trait { unsafety : ast:: Unsafe :: Yes ( _) , .. } ) => {
375- self . report_unsafe ( cx, it. span , fluent :: lint_builtin_unsafe_trait , |lint| lint )
347+ self . report_unsafe ( cx, it. span , BuiltinUnsafe :: UnsafeTrait ) ;
376348 }
377349
378350 ast:: ItemKind :: Impl ( box ast:: Impl { unsafety : ast:: Unsafe :: Yes ( _) , .. } ) => {
379- self . report_unsafe ( cx, it. span , fluent :: lint_builtin_unsafe_impl , |lint| lint )
351+ self . report_unsafe ( cx, it. span , BuiltinUnsafe :: UnsafeImpl ) ;
380352 }
381353
382354 ast:: ItemKind :: Fn ( ..) => {
383355 if let Some ( attr) = cx. sess ( ) . find_by_name ( & it. attrs , sym:: no_mangle) {
384- self . report_overridden_symbol_name (
385- cx,
386- attr. span ,
387- fluent:: lint_builtin_no_mangle_fn,
388- ) ;
356+ self . report_unsafe ( cx, attr. span , BuiltinUnsafe :: NoMangleFn ) ;
389357 }
390358
391359 if let Some ( attr) = cx. sess ( ) . find_by_name ( & it. attrs , sym:: export_name) {
392- self . report_overridden_symbol_name (
393- cx,
394- attr. span ,
395- fluent:: lint_builtin_export_name_fn,
396- ) ;
360+ self . report_unsafe ( cx, attr. span , BuiltinUnsafe :: ExportNameFn ) ;
397361 }
398362
399363 if let Some ( attr) = cx. sess ( ) . find_by_name ( & it. attrs , sym:: link_section) {
400- self . report_overridden_symbol_section (
401- cx,
402- attr. span ,
403- fluent:: lint_builtin_link_section_fn,
404- ) ;
364+ self . report_unsafe ( cx, attr. span , BuiltinUnsafe :: LinkSectionFn ) ;
405365 }
406366 }
407367
408368 ast:: ItemKind :: Static ( ..) => {
409369 if let Some ( attr) = cx. sess ( ) . find_by_name ( & it. attrs , sym:: no_mangle) {
410- self . report_overridden_symbol_name (
411- cx,
412- attr. span ,
413- fluent:: lint_builtin_no_mangle_static,
414- ) ;
370+ self . report_unsafe ( cx, attr. span , BuiltinUnsafe :: NoMangleStatic ) ;
415371 }
416372
417373 if let Some ( attr) = cx. sess ( ) . find_by_name ( & it. attrs , sym:: export_name) {
418- self . report_overridden_symbol_name (
419- cx,
420- attr. span ,
421- fluent:: lint_builtin_export_name_static,
422- ) ;
374+ self . report_unsafe ( cx, attr. span , BuiltinUnsafe :: ExportNameStatic ) ;
423375 }
424376
425377 if let Some ( attr) = cx. sess ( ) . find_by_name ( & it. attrs , sym:: link_section) {
426- self . report_overridden_symbol_section (
427- cx,
428- attr. span ,
429- fluent:: lint_builtin_link_section_static,
430- ) ;
378+ self . report_unsafe ( cx, attr. span , BuiltinUnsafe :: LinkSectionStatic ) ;
431379 }
432380 }
433381
@@ -438,18 +386,10 @@ impl EarlyLintPass for UnsafeCode {
438386 fn check_impl_item ( & mut self , cx : & EarlyContext < ' _ > , it : & ast:: AssocItem ) {
439387 if let ast:: AssocItemKind :: Fn ( ..) = it. kind {
440388 if let Some ( attr) = cx. sess ( ) . find_by_name ( & it. attrs , sym:: no_mangle) {
441- self . report_overridden_symbol_name (
442- cx,
443- attr. span ,
444- fluent:: lint_builtin_no_mangle_method,
445- ) ;
389+ self . report_unsafe ( cx, attr. span , BuiltinUnsafe :: NoMangleMethod ) ;
446390 }
447391 if let Some ( attr) = cx. sess ( ) . find_by_name ( & it. attrs , sym:: export_name) {
448- self . report_overridden_symbol_name (
449- cx,
450- attr. span ,
451- fluent:: lint_builtin_export_name_method,
452- ) ;
392+ self . report_unsafe ( cx, attr. span , BuiltinUnsafe :: ExportNameMethod ) ;
453393 }
454394 }
455395 }
@@ -464,13 +404,13 @@ impl EarlyLintPass for UnsafeCode {
464404 body,
465405 ) = fk
466406 {
467- let msg = match ctxt {
407+ let decorator = match ctxt {
468408 FnCtxt :: Foreign => return ,
469- FnCtxt :: Free => fluent :: lint_builtin_decl_unsafe_fn ,
470- FnCtxt :: Assoc ( _) if body. is_none ( ) => fluent :: lint_builtin_decl_unsafe_method ,
471- FnCtxt :: Assoc ( _) => fluent :: lint_builtin_impl_unsafe_method ,
409+ FnCtxt :: Free => BuiltinUnsafe :: DeclUnsafeFn ,
410+ FnCtxt :: Assoc ( _) if body. is_none ( ) => BuiltinUnsafe :: DeclUnsafeMethod ,
411+ FnCtxt :: Assoc ( _) => BuiltinUnsafe :: ImplUnsafeMethod ,
472412 } ;
473- self . report_unsafe ( cx, span, msg , |lint| lint ) ;
413+ self . report_unsafe ( cx, span, decorator ) ;
474414 }
475415 }
476416}
0 commit comments