2 use rustc_ast::expand::is_proc_macro_attr;
3 use rustc_errors::Applicability;
4 use rustc_session::Session;
7 /// Deprecation status of attributes known by Clippy.
9 pub enum DeprecationStatus {
10 /// Attribute is deprecated
12 /// Attribute is deprecated and was replaced by the named attribute
13 Replaced(&'static str),
17 pub const BUILTIN_ATTRIBUTES: &[(&str, DeprecationStatus)] = &[
18 ("author", DeprecationStatus::None),
19 ("cognitive_complexity", DeprecationStatus::None),
21 "cyclomatic_complexity",
22 DeprecationStatus::Replaced("cognitive_complexity"),
24 ("dump", DeprecationStatus::None),
27 pub struct LimitStack {
31 impl Drop for LimitStack {
33 assert_eq!(self.stack.len(), 1);
39 pub fn new(limit: u64) -> Self {
40 Self { stack: vec![limit] }
42 pub fn limit(&self) -> u64 {
43 *self.stack.last().expect("there should always be a value in the stack")
45 pub fn push_attrs(&mut self, sess: &Session, attrs: &[ast::Attribute], name: &'static str) {
46 let stack = &mut self.stack;
47 parse_attrs(sess, attrs, name, |val| stack.push(val));
49 pub fn pop_attrs(&mut self, sess: &Session, attrs: &[ast::Attribute], name: &'static str) {
50 let stack = &mut self.stack;
51 parse_attrs(sess, attrs, name, |val| assert_eq!(stack.pop(), Some(val)));
57 attrs: &'a [ast::Attribute],
59 ) -> impl Iterator<Item = &'a ast::Attribute> {
60 attrs.iter().filter(move |attr| {
61 let attr = if let ast::AttrKind::Normal(ref attr) = attr.kind {
66 let attr_segments = &attr.path.segments;
67 if attr_segments.len() == 2 && attr_segments[0].ident.to_string() == "clippy" {
68 if let Some(deprecation_status) =
71 .find_map(|(builtin_name, deprecation_status)| {
72 if *builtin_name == attr_segments[1].ident.to_string() {
73 Some(deprecation_status)
79 let mut diag = sess.struct_span_err(attr_segments[1].ident.span, "Usage of deprecated attribute");
80 match *deprecation_status {
81 DeprecationStatus::Deprecated => {
85 DeprecationStatus::Replaced(new_name) => {
87 attr_segments[1].ident.span,
90 Applicability::MachineApplicable,
95 DeprecationStatus::None => {
97 attr_segments[1].ident.to_string() == name
101 sess.span_err(attr_segments[1].ident.span, "Usage of unknown attribute");
110 fn parse_attrs<F: FnMut(u64)>(sess: &Session, attrs: &[ast::Attribute], name: &'static str, mut f: F) {
111 for attr in get_attr(sess, attrs, name) {
112 if let Some(ref value) = attr.value_str() {
113 if let Ok(value) = FromStr::from_str(&value.as_str()) {
116 sess.span_err(attr.span, "not a number");
119 sess.span_err(attr.span, "bad clippy attribute");
124 /// Return true if the attributes contain any of `proc_macro`,
125 /// `proc_macro_derive` or `proc_macro_attribute`, false otherwise
126 pub fn is_proc_macro(attrs: &[ast::Attribute]) -> bool {
127 attrs.iter().any(is_proc_macro_attr)