@@ -124,7 +124,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
124
124
}
125
125
[ sym:: inline, ..] => self . check_inline ( hir_id, attr, span, target) ,
126
126
[ 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) ,
128
128
[ sym:: no_sanitize, ..] => self . check_no_sanitize ( hir_id, attr, span, target) ,
129
129
[ sym:: non_exhaustive, ..] => self . check_non_exhaustive ( hir_id, attr, span, target) ,
130
130
[ sym:: marker, ..] => self . check_marker ( hir_id, attr, span, target) ,
@@ -431,23 +431,21 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
431
431
432
432
/// Checks that `#[optimize(..)]` is applied to a function/closure/method,
433
433
/// or to an impl block or module.
434
- // 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 {
434
+ fn check_optimize ( & self , hir_id : HirId , attr : & Attribute , span : Span , target : Target ) {
435
+ let is_valid = matches ! (
436
+ target ,
437
437
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
- }
438
+ | Target :: Closure
439
+ | Target :: Method ( MethodKind :: Trait { body: true } | MethodKind :: Inherent )
440
+ | Target :: Impl
441
+ | Target :: Mod
442
+ ) ;
443
+ if !is_valid {
444
+ self . dcx ( ) . emit_err ( errors:: OptimizeNotFnOrClosure {
445
+ attr_span : attr. span ,
446
+ defn_span : span,
447
+ on_crate : hir_id == CRATE_HIR_ID ,
448
+ } ) ;
451
449
}
452
450
}
453
451
0 commit comments