Skip to content

Commit 0de6544

Browse files
topecongiroflip1995
authored andcommitted
Change Attribute::name to return the last segment
And fix some typos
1 parent 8ad2d21 commit 0de6544

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

src/libsyntax/attr.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ pub fn is_known(attr: &Attribute) -> bool {
110110
const RUST_KNOWN_TOOL: &[&str] = &["clippy", "rustfmt"];
111111

112112
pub fn is_known_tool(attr: &Attribute) -> bool {
113-
RUST_KNOWN_TOOL.contains(&attr.name().as_str().as_ref())
113+
let tool_name =
114+
attr.path.segments.iter().next().expect("empty path in attribute").identifier.name;
115+
RUST_KNOWN_TOOL.contains(&tool_name.as_str().as_ref())
114116
}
115117

116118
impl NestedMetaItem {
@@ -211,7 +213,7 @@ impl NestedMetaItem {
211213
}
212214

213215
fn name_from_path(path: &ast::Path) -> Name {
214-
path.segments.iter().next().unwrap().identifier.name
216+
path.segments.last().expect("empty path in attribute").identifier.name
215217
}
216218

217219
impl Attribute {
@@ -223,8 +225,8 @@ impl Attribute {
223225
matches
224226
}
225227

226-
/// Returns the first segment of the name of this attribute.
227-
/// E.g. `foo` for `#[foo]`, `rustfmt` for `#[rustfmt::skip]`.
228+
/// Returns the **last** segment of the name of this attribute.
229+
/// E.g. `foo` for `#[foo]`, `skip` for `#[rustfmt::skip]`.
228230
pub fn name(&self) -> Name {
229231
name_from_path(&self.path)
230232
}
@@ -1162,6 +1164,7 @@ impl MetaItem {
11621164
fn tokens(&self) -> TokenStream {
11631165
let mut idents = vec![];
11641166
let mut last_pos = BytePos(0 as u32);
1167+
// FIXME: Share code with `parse_path`.
11651168
for (i, segment) in self.name.segments.iter().enumerate() {
11661169
let is_first = i == 0;
11671170
if !is_first {

src/test/compile-fail/unknown-tool-name.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
#![feature(tool_attributes)]
1212

13-
#![foo::bar] //~ ERROR An unkown tool name found in scoped attributes: `foo::bar`. [E0693]
13+
#![foo::bar] //~ ERROR An unknown tool name found in scoped attributes: `foo::bar`. [E0693]
1414

15-
#[foo::bar] //~ ERROR An unkown tool name found in scoped attributes: `foo::bar`. [E0693]
15+
#[foo::bar] //~ ERROR An unknown tool name found in scoped attributes: `foo::bar`. [E0693]
1616
fn main() {}

src/test/compile-fail/unknown_tool_attributes-1.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@
1414

1515
#[foo::bar]
1616
//~^ ERROR scoped attribute `foo::bar` is experimental (see issue #44690) [E0658]
17-
//~^^ ERROR An unkown tool name found in scoped attributes: `foo::bar`. [E0693]
17+
//~^^ ERROR An unknown tool name found in scoped attributes: `foo::bar`. [E0693]
1818
fn main() {}

0 commit comments

Comments
 (0)