@@ -160,9 +160,11 @@ declare_clippy_lint! {
160160 /// are stable now, they should be used instead of the old `cfg_attr(rustfmt)` attributes.
161161 ///
162162 /// ### Known problems
163- /// This lint doesn't detect crate level inner attributes, because they get
164- /// processed before the PreExpansionPass lints get executed. See
165- /// [#3123](https://github.com/rust-lang/rust-clippy/pull/3123#issuecomment-422321765)
163+ /// Does not detect attributes applied to macro invocations such as
164+ /// ```no_run
165+ /// #[cfg_attr(rustfmt, rustfmt_skip)]
166+ /// println!("..");
167+ /// ```
166168 ///
167169 /// ### Example
168170 /// ```no_run
@@ -314,11 +316,16 @@ declare_clippy_lint! {
314316
315317declare_clippy_lint ! {
316318 /// ### What it does
317- /// Checks for `any` and `all` combinators in `cfg` with only one condition.
319+ /// Checks for `any` and `all` combinators in `cfg` or `cfg_attr` with only one condition.
318320 ///
319321 /// ### Why is this bad?
320322 /// If there is only one condition, no need to wrap it into `any` or `all` combinators.
321323 ///
324+ /// ### Known Problems
325+ /// Only attributes that are attached to items included in the current compilation will be linted,
326+ /// for the examples below the lint will only fire when `Bar` is compiled - in this case when on
327+ /// a `unix` target.
328+ ///
322329 /// ### Example
323330 /// ```no_run
324331 /// #[cfg(any(unix))]
@@ -474,22 +481,27 @@ declare_clippy_lint! {
474481 "ignored tests without messages"
475482}
476483
477- pub struct Attributes {
484+ pub struct LateAttributes {
478485 msrv : Msrv ,
479486}
480487
481- impl_lint_pass ! ( Attributes => [
488+ impl_lint_pass ! ( LateAttributes => [
482489 INLINE_ALWAYS ,
483490 REPR_PACKED_WITHOUT_ABI ,
491+ DEPRECATED_CLIPPY_CFG_ATTR ,
484492] ) ;
485493
486- impl Attributes {
494+ impl LateAttributes {
487495 pub fn new ( conf : & ' static Conf ) -> Self {
488496 Self { msrv : conf. msrv }
489497 }
490498}
491499
492- impl < ' tcx > LateLintPass < ' tcx > for Attributes {
500+ impl < ' tcx > LateLintPass < ' tcx > for LateAttributes {
501+ fn check_crate ( & mut self , cx : & LateContext < ' tcx > ) {
502+ deprecated_cfg_attr:: check_stripped ( cx) ;
503+ }
504+
493505 fn check_item ( & mut self , cx : & LateContext < ' tcx > , item : & ' tcx Item < ' _ > ) {
494506 let attrs = cx. tcx . hir_attrs ( item. hir_id ( ) ) ;
495507 if let ItemKind :: Fn { ident, .. } = item. kind
@@ -526,35 +538,6 @@ impl EarlyAttributes {
526538}
527539
528540impl_lint_pass ! ( EarlyAttributes => [
529- DEPRECATED_CFG_ATTR ,
530- NON_MINIMAL_CFG ,
531- DEPRECATED_CLIPPY_CFG_ATTR ,
532- UNNECESSARY_CLIPPY_CFG ,
533- ] ) ;
534-
535- impl EarlyLintPass for EarlyAttributes {
536- fn check_attribute ( & mut self , cx : & EarlyContext < ' _ > , attr : & Attribute ) {
537- deprecated_cfg_attr:: check ( cx, attr, & self . msrv ) ;
538- deprecated_cfg_attr:: check_clippy ( cx, attr) ;
539- non_minimal_cfg:: check ( cx, attr) ;
540- }
541-
542- extract_msrv_attr ! ( ) ;
543- }
544-
545- pub struct PostExpansionEarlyAttributes {
546- msrv : MsrvStack ,
547- }
548-
549- impl PostExpansionEarlyAttributes {
550- pub fn new ( conf : & ' static Conf ) -> Self {
551- Self {
552- msrv : MsrvStack :: new ( conf. msrv ) ,
553- }
554- }
555- }
556-
557- impl_lint_pass ! ( PostExpansionEarlyAttributes => [
558541 ALLOW_ATTRIBUTES ,
559542 ALLOW_ATTRIBUTES_WITHOUT_REASON ,
560543 DEPRECATED_SEMVER ,
@@ -564,9 +547,13 @@ impl_lint_pass!(PostExpansionEarlyAttributes => [
564547 SHOULD_PANIC_WITHOUT_EXPECT ,
565548 MIXED_ATTRIBUTES_STYLE ,
566549 DUPLICATED_ATTRIBUTES ,
550+ DEPRECATED_CFG_ATTR ,
551+ DEPRECATED_CLIPPY_CFG_ATTR ,
552+ UNNECESSARY_CLIPPY_CFG ,
553+ NON_MINIMAL_CFG ,
567554] ) ;
568555
569- impl EarlyLintPass for PostExpansionEarlyAttributes {
556+ impl EarlyLintPass for EarlyAttributes {
570557 fn check_crate ( & mut self , cx : & EarlyContext < ' _ > , krate : & ast:: Crate ) {
571558 blanket_clippy_restriction_lints:: check_command_line ( cx) ;
572559 duplicated_attributes:: check ( cx, & krate. attrs ) ;
@@ -585,6 +572,15 @@ impl EarlyLintPass for PostExpansionEarlyAttributes {
585572 if is_lint_level ( ident. name , attr. id ) {
586573 blanket_clippy_restriction_lints:: check ( cx, ident. name , items) ;
587574 }
575+ if matches ! ( ident. name, sym:: cfg_trace | sym:: cfg_attr_trace)
576+ && let Some ( meta_item) = items. first ( ) . and_then ( |item| item. meta_item ( ) )
577+ {
578+ non_minimal_cfg:: check ( cx, items, & self . msrv ) ;
579+ deprecated_cfg_attr:: check ( cx, meta_item) ;
580+ if let Some ( behind_cfg) = items. get ( 1 ) . and_then ( |item| item. meta_item ( ) ) {
581+ unnecessary_clippy_cfg:: check ( cx, meta_item, behind_cfg, attr) ;
582+ }
583+ }
588584 if items. is_empty ( ) || !attr. has_name ( sym:: deprecated) {
589585 return ;
590586 }
@@ -620,14 +616,52 @@ impl EarlyLintPass for PostExpansionEarlyAttributes {
620616 }
621617
622618 fn check_item ( & mut self , cx : & EarlyContext < ' _ > , item : & ' _ ast:: Item ) {
623- match item. kind {
619+ match & item. kind {
624620 ast:: ItemKind :: ExternCrate ( ..) | ast:: ItemKind :: Use ( ..) => useless_attribute:: check ( cx, item, & item. attrs ) ,
621+ ast:: ItemKind :: Struct ( .., variant) => {
622+ for field in variant. fields ( ) {
623+ deprecated_cfg_attr:: check_rustfmt ( cx, & field. attrs , & self . msrv ) ;
624+ }
625+ } ,
626+ ast:: ItemKind :: ForeignMod ( foreign) => {
627+ for item in & foreign. items {
628+ deprecated_cfg_attr:: check_rustfmt ( cx, & item. attrs , & self . msrv )
629+ }
630+ } ,
625631 _ => { } ,
626632 }
627633
634+ deprecated_cfg_attr:: check_rustfmt ( cx, & item. attrs , & self . msrv ) ;
628635 mixed_attributes_style:: check ( cx, item. span , & item. attrs ) ;
629636 duplicated_attributes:: check ( cx, & item. attrs ) ;
630637 }
631638
639+ fn check_stmt ( & mut self , cx : & EarlyContext < ' _ > , stmt : & ast:: Stmt ) {
640+ match & stmt. kind {
641+ ast:: StmtKind :: Let ( local) => deprecated_cfg_attr:: check_rustfmt ( cx, & local. attrs , & self . msrv ) ,
642+ ast:: StmtKind :: Semi ( expr) => deprecated_cfg_attr:: check_rustfmt ( cx, & expr. attrs , & self . msrv ) ,
643+ _ => { } ,
644+ }
645+ }
646+
647+ fn check_arm ( & mut self , cx : & EarlyContext < ' _ > , arm : & ast:: Arm ) {
648+ deprecated_cfg_attr:: check_rustfmt ( cx, & arm. attrs , & self . msrv ) ;
649+ }
650+
651+ fn check_trait_item ( & mut self , cx : & EarlyContext < ' _ > , item : & ast:: AssocItem ) {
652+ deprecated_cfg_attr:: check_rustfmt ( cx, & item. attrs , & self . msrv ) ;
653+ }
654+
655+ fn check_impl_item ( & mut self , cx : & EarlyContext < ' _ > , item : & ast:: AssocItem ) {
656+ deprecated_cfg_attr:: check_rustfmt ( cx, & item. attrs , & self . msrv ) ;
657+ }
658+
659+ fn check_variant ( & mut self , cx : & EarlyContext < ' _ > , variant : & ast:: Variant ) {
660+ deprecated_cfg_attr:: check_rustfmt ( cx, & variant. attrs , & self . msrv ) ;
661+ for field in variant. data . fields ( ) {
662+ deprecated_cfg_attr:: check_rustfmt ( cx, & field. attrs , & self . msrv ) ;
663+ }
664+ }
665+
632666 extract_msrv_attr ! ( ) ;
633667}
0 commit comments