2 use rustc_errors::Applicability;
3 use rustc_session::Session;
6 /// Deprecation status of attributes known by Clippy.
8 pub enum DeprecationStatus {
9 /// Attribute is deprecated
11 /// Attribute is deprecated and was replaced by the named attribute
12 Replaced(&'static str),
16 pub const BUILTIN_ATTRIBUTES: &[(&str, DeprecationStatus)] = &[
17 ("author", DeprecationStatus::None),
18 ("cognitive_complexity", DeprecationStatus::None),
20 "cyclomatic_complexity",
21 DeprecationStatus::Replaced("cognitive_complexity"),
23 ("dump", DeprecationStatus::None),
26 pub struct LimitStack {
30 impl Drop for LimitStack {
32 assert_eq!(self.stack.len(), 1);
38 pub fn new(limit: u64) -> Self {
39 Self { stack: vec![limit] }
41 pub fn limit(&self) -> u64 {
42 *self.stack.last().expect("there should always be a value in the stack")
44 pub fn push_attrs(&mut self, sess: &Session, attrs: &[ast::Attribute], name: &'static str) {
45 let stack = &mut self.stack;
46 parse_attrs(sess, attrs, name, |val| stack.push(val));
48 pub fn pop_attrs(&mut self, sess: &Session, attrs: &[ast::Attribute], name: &'static str) {
49 let stack = &mut self.stack;
50 parse_attrs(sess, attrs, name, |val| assert_eq!(stack.pop(), Some(val)));
56 attrs: &'a [ast::Attribute],
58 ) -> impl Iterator<Item = &'a ast::Attribute> {
59 attrs.iter().filter(move |attr| {
60 let attr = if let ast::AttrKind::Normal(ref attr) = attr.kind {
65 let attr_segments = &attr.path.segments;
66 if attr_segments.len() == 2 && attr_segments[0].ident.to_string() == "clippy" {
69 .find_map(|(builtin_name, deprecation_status)| {
70 if *builtin_name == attr_segments[1].ident.to_string() {
71 Some(deprecation_status)
78 sess.span_err(attr_segments[1].ident.span, "Usage of unknown attribute");
81 |deprecation_status| {
83 sess.struct_span_err(attr_segments[1].ident.span, "Usage of deprecated attribute");
84 match *deprecation_status {
85 DeprecationStatus::Deprecated => {
89 DeprecationStatus::Replaced(new_name) => {
91 attr_segments[1].ident.span,
94 Applicability::MachineApplicable,
99 DeprecationStatus::None => {
101 attr_segments[1].ident.to_string() == name
112 fn parse_attrs<F: FnMut(u64)>(sess: &Session, attrs: &[ast::Attribute], name: &'static str, mut f: F) {
113 for attr in get_attr(sess, attrs, name) {
114 if let Some(ref value) = attr.value_str() {
115 if let Ok(value) = FromStr::from_str(&value.as_str()) {
118 sess.span_err(attr.span, "not a number");
121 sess.span_err(attr.span, "bad clippy attribute");
126 /// Return true if the attributes contain any of `proc_macro`,
127 /// `proc_macro_derive` or `proc_macro_attribute`, false otherwise
128 pub fn is_proc_macro(sess: &Session, attrs: &[ast::Attribute]) -> bool {
129 attrs.iter().any(|attr| sess.is_proc_macro_attr(attr))