Skip to content

Commit e188848

Browse files
committed
Initial implementation of RFC rust-lang#44690
1 parent ba4e8d7 commit e188848

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

src/libsyntax/ast.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -429,11 +429,31 @@ pub enum NestedMetaItemKind {
429429
/// E.g. `#[test]`, `#[derive(..)]` or `#[feature = "foo"]`
430430
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
431431
pub struct MetaItem {
432-
pub name: Name,
432+
pub name: Name, //MetaItemName,
433433
pub node: MetaItemKind,
434434
pub span: Span,
435435
}
436436

437+
/*#[derive(Clone, Eq, RustcEncodable, RustcDecodable, Hash, Debug, PartialEq)]
438+
pub enum MetaItemName {
439+
/// A single MetaItem name
440+
Single(Name),
441+
442+
/// A collection of MetaItem names
443+
///
444+
/// Used for attribute paths
445+
Namespaced(Vec<Name>),
446+
}*/
447+
448+
/*impl From<MetaItemName> for Name {
449+
fn from(m: MetaItemName) -> Self {
450+
match m {
451+
MetaItemName::Single(n) => n,
452+
MetaItemName::Namespaced(v) => v[0],
453+
}
454+
}
455+
}*/
456+
437457
/// A compile-time attribute item.
438458
///
439459
/// E.g. `#[test]`, `#[derive(..)]` or `#[feature = "foo"]`

src/libsyntax/attr.rs

+29-7
Original file line numberDiff line numberDiff line change
@@ -349,15 +349,37 @@ impl Attribute {
349349
}
350350

351351
pub fn parse_meta<'a>(&self, sess: &'a ParseSess) -> PResult<'a, MetaItem> {
352+
// TODO: Find a new place to put the whitelist
353+
static WHITELIST: [&str; 2] = ["clippy", "rustfmt"];
354+
352355
if self.path.segments.len() > 1 {
353-
sess.span_diagnostic.span_err(self.path.span, "expected ident, found path");
354-
}
356+
//sess.span_diagnostic.span_err(self.path.span, "expected ident, found path");
355357

356-
Ok(MetaItem {
357-
name: self.path.segments.last().unwrap().identifier.name,
358-
node: self.parse(sess, |parser| parser.parse_meta_item_kind())?,
359-
span: self.span,
360-
})
358+
let mut is_valid_tool = false;
359+
360+
for wl_name in WHITELIST.iter() {
361+
if *wl_name == self.path.segments[0].identifier.name.as_str() {
362+
is_valid_tool = true;
363+
}
364+
}
365+
366+
if !is_valid_tool {
367+
sess.span_diagnostic.span_err(self.path.span, "Invalid tool name");
368+
}
369+
370+
Ok(MetaItem {
371+
name: Name::intern(&self.path.segments.iter().map(|s| s.identifier.name.as_str().to_string()).collect::<Vec<String>>().join("::")),
372+
node: self.parse(sess, |parser| parser.parse_meta_item_kind())?,
373+
span: self.span,
374+
})
375+
376+
} else {
377+
Ok(MetaItem {
378+
name: self.path.segments.last().unwrap().identifier.name,
379+
node: self.parse(sess, |parser| parser.parse_meta_item_kind())?,
380+
span: self.span,
381+
})
382+
}
361383
}
362384

363385
/// Convert self to a normal #[doc="foo"] comment, if it is a

0 commit comments

Comments
 (0)