]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/utils/attrs.rs
4bb4b087c5566773b5dfa9249c936a486b103f98
[rust.git] / clippy_lints / src / utils / attrs.rs
1 use rustc_ast::ast;
2 use rustc_ast::expand::is_proc_macro_attr;
3 use rustc_errors::Applicability;
4 use rustc_session::Session;
5 use std::str::FromStr;
6
7 /// Deprecation status of attributes known by Clippy.
8 #[allow(dead_code)]
9 pub enum DeprecationStatus {
10     /// Attribute is deprecated
11     Deprecated,
12     /// Attribute is deprecated and was replaced by the named attribute
13     Replaced(&'static str),
14     None,
15 }
16
17 pub const BUILTIN_ATTRIBUTES: &[(&str, DeprecationStatus)] = &[
18     ("author", DeprecationStatus::None),
19     ("cognitive_complexity", DeprecationStatus::None),
20     (
21         "cyclomatic_complexity",
22         DeprecationStatus::Replaced("cognitive_complexity"),
23     ),
24     ("dump", DeprecationStatus::None),
25 ];
26
27 pub struct LimitStack {
28     stack: Vec<u64>,
29 }
30
31 impl Drop for LimitStack {
32     fn drop(&mut self) {
33         assert_eq!(self.stack.len(), 1);
34     }
35 }
36
37 impl LimitStack {
38     #[must_use]
39     pub fn new(limit: u64) -> Self {
40         Self { stack: vec![limit] }
41     }
42     pub fn limit(&self) -> u64 {
43         *self.stack.last().expect("there should always be a value in the stack")
44     }
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));
48     }
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)));
52     }
53 }
54
55 pub fn get_attr<'a>(
56     sess: &'a Session,
57     attrs: &'a [ast::Attribute],
58     name: &'static str,
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 {
62             attr
63         } else {
64             return false;
65         };
66         let attr_segments = &attr.path.segments;
67         if attr_segments.len() == 2 && attr_segments[0].ident.to_string() == "clippy" {
68             BUILTIN_ATTRIBUTES
69                 .iter()
70                 .find_map(|(builtin_name, deprecation_status)| {
71                     if *builtin_name == attr_segments[1].ident.to_string() {
72                         Some(deprecation_status)
73                     } else {
74                         None
75                     }
76                 })
77                 .map_or_else(
78                     || {
79                         sess.span_err(attr_segments[1].ident.span, "Usage of unknown attribute");
80                         false
81                     },
82                     |deprecation_status| {
83                         let mut diag =
84                             sess.struct_span_err(attr_segments[1].ident.span, "Usage of deprecated attribute");
85                         match *deprecation_status {
86                             DeprecationStatus::Deprecated => {
87                                 diag.emit();
88                                 false
89                             },
90                             DeprecationStatus::Replaced(new_name) => {
91                                 diag.span_suggestion(
92                                     attr_segments[1].ident.span,
93                                     "consider using",
94                                     new_name.to_string(),
95                                     Applicability::MachineApplicable,
96                                 );
97                                 diag.emit();
98                                 false
99                             },
100                             DeprecationStatus::None => {
101                                 diag.cancel();
102                                 attr_segments[1].ident.to_string() == name
103                             },
104                         }
105                     },
106                 )
107         } else {
108             false
109         }
110     })
111 }
112
113 fn parse_attrs<F: FnMut(u64)>(sess: &Session, attrs: &[ast::Attribute], name: &'static str, mut f: F) {
114     for attr in get_attr(sess, attrs, name) {
115         if let Some(ref value) = attr.value_str() {
116             if let Ok(value) = FromStr::from_str(&value.as_str()) {
117                 f(value)
118             } else {
119                 sess.span_err(attr.span, "not a number");
120             }
121         } else {
122             sess.span_err(attr.span, "bad clippy attribute");
123         }
124     }
125 }
126
127 /// Return true if the attributes contain any of `proc_macro`,
128 /// `proc_macro_derive` or `proc_macro_attribute`, false otherwise
129 pub fn is_proc_macro(attrs: &[ast::Attribute]) -> bool {
130     attrs.iter().any(is_proc_macro_attr)
131 }