@@ -124,7 +124,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
124124 }
125125 [ sym:: inline, ..] => self . check_inline ( hir_id, attr, span, target) ,
126126 [ sym:: coverage, ..] => self . check_coverage ( attr, span, target) ,
127- [ sym:: optimize, ..] => self . check_optimize ( hir_id, attr, target) ,
127+ [ sym:: optimize, ..] => self . check_optimize ( hir_id, attr, span , target) ,
128128 [ sym:: no_sanitize, ..] => self . check_no_sanitize ( hir_id, attr, span, target) ,
129129 [ sym:: non_exhaustive, ..] => self . check_non_exhaustive ( hir_id, attr, span, target) ,
130130 [ sym:: marker, ..] => self . check_marker ( hir_id, attr, span, target) ,
@@ -432,22 +432,21 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
432432 /// Checks that `#[optimize(..)]` is applied to a function/closure/method,
433433 /// or to an impl block or module.
434434 // FIXME(#128488): this should probably be elevated to an error?
435- fn check_optimize ( & self , hir_id : HirId , attr : & Attribute , target : Target ) {
436- match target {
435+ fn check_optimize ( & self , hir_id : HirId , attr : & Attribute , span : Span , target : Target ) {
436+ let is_valid = matches ! (
437+ target,
437438 Target :: Fn
438- | Target :: Closure
439- | Target :: Method ( MethodKind :: Trait { body : true } | MethodKind :: Inherent )
440- | Target :: Impl
441- | Target :: Mod => { }
442-
443- _ => {
444- self . tcx . emit_node_span_lint (
445- UNUSED_ATTRIBUTES ,
446- hir_id,
447- attr. span ,
448- errors:: OptimizeNotFnOrClosure ,
449- ) ;
450- }
439+ | Target :: Closure
440+ | Target :: Method ( MethodKind :: Trait { body: true } | MethodKind :: Inherent )
441+ | Target :: Impl
442+ | Target :: Mod
443+ ) ;
444+ if !is_valid {
445+ self . dcx ( ) . emit_err ( errors:: OptimizeNotFnOrClosure {
446+ attr_span : attr. span ,
447+ defn_span : span,
448+ on_crate : hir_id == CRATE_HIR_ID ,
449+ } ) ;
451450 }
452451 }
453452
0 commit comments