@@ -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) ,
@@ -432,22 +432,21 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
432
432
/// Checks that `#[optimize(..)]` is applied to a function/closure/method,
433
433
/// or to an impl block or module.
434
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 {
435
+ fn check_optimize ( & self , hir_id : HirId , attr : & Attribute , span : Span , target : Target ) {
436
+ let is_valid = matches ! (
437
+ target,
437
438
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
+ } ) ;
451
450
}
452
451
}
453
452
0 commit comments