1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
14 use syntax
::attr
::AttrMetaMethods
;
16 use syntax
::visit
::Visitor
;
18 #[derive(Copy, Clone, PartialEq)]
27 fn from_item(item
: &ast
::Item
) -> Target
{
29 ast
::ItemKind
::Fn(..) => Target
::Fn
,
30 ast
::ItemKind
::Struct(..) => Target
::Struct
,
31 ast
::ItemKind
::Enum(..) => Target
::Enum
,
37 struct CheckAttrVisitor
<'a
> {
41 impl<'a
> CheckAttrVisitor
<'a
> {
42 fn check_inline(&self, attr
: &ast
::Attribute
, target
: Target
) {
43 if target
!= Target
::Fn
{
44 span_err
!(self.sess
, attr
.span
, E0518
, "attribute should be applied to function");
48 fn check_repr(&self, attr
: &ast
::Attribute
, target
: Target
) {
49 let words
= match attr
.meta_item_list() {
56 let word
: &str = &word
.name();
57 let message
= match word
{
59 if target
!= Target
::Struct
&& target
!= Target
::Enum
{
60 "attribute should be applied to struct or enum"
67 if target
!= Target
::Struct
{
68 "attribute should be applied to struct"
73 "i8" | "u8" | "i16" | "u16" |
74 "i32" | "u32" | "i64" | "u64" |
75 "isize" | "usize" => {
76 if target
!= Target
::Enum
{
77 "attribute should be applied to enum"
84 span_err
!(self.sess
, attr
.span
, E0517
, "{}", message
);
88 fn check_attribute(&self, attr
: &ast
::Attribute
, target
: Target
) {
89 let name
: &str = &attr
.name();
91 "inline" => self.check_inline(attr
, target
),
92 "repr" => self.check_repr(attr
, target
),
98 impl<'a
, 'v
> Visitor
<'v
> for CheckAttrVisitor
<'a
> {
99 fn visit_item(&mut self, item
: &ast
::Item
) {
100 let target
= Target
::from_item(item
);
101 for attr
in &item
.attrs
{
102 self.check_attribute(attr
, target
);
104 visit
::walk_item(self, item
);
108 pub fn check_crate(sess
: &Session
, krate
: &ast
::Crate
) {
109 visit
::walk_crate(&mut CheckAttrVisitor { sess: sess }
, krate
);