|
| 1 | +use super::{unnecessary_clippy_cfg, Attribute, DEPRECATED_CFG_ATTR, DEPRECATED_CLIPPY_CFG_ATTR}; |
| 2 | +use clippy_config::msrvs::{self, Msrv}; |
| 3 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 4 | +use rustc_ast::AttrStyle; |
| 5 | +use rustc_errors::Applicability; |
| 6 | +use rustc_lint::EarlyContext; |
| 7 | +use rustc_span::sym; |
| 8 | + |
| 9 | +pub(super) fn check(cx: &EarlyContext<'_>, attr: &Attribute, msrv: &Msrv) { |
| 10 | + // check cfg_attr |
| 11 | + if attr.has_name(sym::cfg_attr) |
| 12 | + && let Some(items) = attr.meta_item_list() |
| 13 | + && items.len() == 2 |
| 14 | + && let Some(feature_item) = items[0].meta_item() |
| 15 | + { |
| 16 | + // check for `rustfmt` |
| 17 | + if feature_item.has_name(sym::rustfmt) |
| 18 | + && msrv.meets(msrvs::TOOL_ATTRIBUTES) |
| 19 | + // check for `rustfmt_skip` and `rustfmt::skip` |
| 20 | + && let Some(skip_item) = &items[1].meta_item() |
| 21 | + && (skip_item.has_name(sym!(rustfmt_skip)) |
| 22 | + || skip_item |
| 23 | + .path |
| 24 | + .segments |
| 25 | + .last() |
| 26 | + .expect("empty path in attribute") |
| 27 | + .ident |
| 28 | + .name |
| 29 | + == sym::skip) |
| 30 | + // Only lint outer attributes, because custom inner attributes are unstable |
| 31 | + // Tracking issue: https://github.com/rust-lang/rust/issues/54726 |
| 32 | + && attr.style == AttrStyle::Outer |
| 33 | + { |
| 34 | + span_lint_and_sugg( |
| 35 | + cx, |
| 36 | + DEPRECATED_CFG_ATTR, |
| 37 | + attr.span, |
| 38 | + "`cfg_attr` is deprecated for rustfmt and got replaced by tool attributes", |
| 39 | + "use", |
| 40 | + "#[rustfmt::skip]".to_string(), |
| 41 | + Applicability::MachineApplicable, |
| 42 | + ); |
| 43 | + } else { |
| 44 | + check_deprecated_cfg_recursively(cx, feature_item); |
| 45 | + if let Some(behind_cfg_attr) = items[1].meta_item() { |
| 46 | + unnecessary_clippy_cfg::check(cx, feature_item, behind_cfg_attr, attr); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +pub(super) fn check_clippy(cx: &EarlyContext<'_>, attr: &Attribute) { |
| 53 | + if attr.has_name(sym::cfg) |
| 54 | + && let Some(list) = attr.meta_item_list() |
| 55 | + { |
| 56 | + for item in list.iter().filter_map(|item| item.meta_item()) { |
| 57 | + check_deprecated_cfg_recursively(cx, item); |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +fn check_deprecated_cfg_recursively(cx: &EarlyContext<'_>, attr: &rustc_ast::MetaItem) { |
| 63 | + if let Some(ident) = attr.ident() { |
| 64 | + if ["any", "all", "not"].contains(&ident.name.as_str()) { |
| 65 | + let Some(list) = attr.meta_item_list() else { return }; |
| 66 | + for item in list.iter().filter_map(|item| item.meta_item()) { |
| 67 | + check_deprecated_cfg_recursively(cx, item); |
| 68 | + } |
| 69 | + } else { |
| 70 | + check_cargo_clippy_attr(cx, attr); |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +fn check_cargo_clippy_attr(cx: &EarlyContext<'_>, item: &rustc_ast::MetaItem) { |
| 76 | + if item.has_name(sym::feature) && item.value_str().is_some_and(|v| v.as_str() == "cargo-clippy") { |
| 77 | + span_lint_and_sugg( |
| 78 | + cx, |
| 79 | + DEPRECATED_CLIPPY_CFG_ATTR, |
| 80 | + item.span, |
| 81 | + "`feature = \"cargo-clippy\"` was replaced by `clippy`", |
| 82 | + "replace with", |
| 83 | + "clippy".to_string(), |
| 84 | + Applicability::MachineApplicable, |
| 85 | + ); |
| 86 | + } |
| 87 | +} |
0 commit comments