Skip to content

Commit f2b4234

Browse files
committed
Remove Option from the return type of Attribute::name()
1 parent b93af01 commit f2b4234

File tree

17 files changed

+46
-36
lines changed

17 files changed

+46
-36
lines changed

src/librustc/hir/check_attr.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,8 @@ impl<'a, 'tcx> CheckAttrVisitor<'a, 'tcx> {
5050
self.tcx.target_features_enabled(self.tcx.hir.local_def_id(item.id));
5151

5252
for attr in &item.attrs {
53-
if let Some(name) = attr.name() {
54-
if name == "inline" {
55-
self.check_inline(attr, item, target)
56-
}
53+
if attr.name() == "inline" {
54+
self.check_inline(attr, item, target)
5755
}
5856
}
5957

@@ -81,10 +79,7 @@ impl<'a, 'tcx> CheckAttrVisitor<'a, 'tcx> {
8179
// ```
8280
let hints: Vec<_> = item.attrs
8381
.iter()
84-
.filter(|attr| match attr.name() {
85-
Some(name) => name == "repr",
86-
None => false,
87-
})
82+
.filter(|attr| attr.name() == "repr")
8883
.filter_map(|attr| attr.meta_item_list())
8984
.flat_map(|hints| hints)
9085
.collect();

src/librustc/ich/impls_syntax.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,7 @@ impl<'gcx> HashStable<StableHashingContext<'gcx>> for [ast::Attribute] {
178178
let filtered: AccumulateVec<[&ast::Attribute; 8]> = self
179179
.iter()
180180
.filter(|attr| {
181-
!attr.is_sugared_doc &&
182-
attr.name().map(|name| !hcx.is_ignored_attr(name)).unwrap_or(true)
181+
!attr.is_sugared_doc && !hcx.is_ignored_attr(attr.name())
183182
})
184183
.collect();
185184

@@ -206,7 +205,7 @@ impl<'gcx> HashStable<StableHashingContext<'gcx>> for ast::Attribute {
206205
hcx: &mut StableHashingContext<'gcx>,
207206
hasher: &mut StableHasher<W>) {
208207
// Make sure that these have been filtered out.
209-
debug_assert!(self.name().map(|name| !hcx.is_ignored_attr(name)).unwrap_or(true));
208+
debug_assert!(!hcx.is_ignored_attr(self.name()));
210209
debug_assert!(!self.is_sugared_doc);
211210

212211
let ast::Attribute {

src/librustc/lint/levels.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ impl<'a> LintLevelsBuilder<'a> {
197197
"malformed lint attribute");
198198
};
199199
for attr in attrs {
200-
let level = match attr.name().and_then(|name| Level::from_str(&name.as_str())) {
200+
let level = match Level::from_str(&attr.name().as_str()) {
201201
None => continue,
202202
Some(lvl) => lvl,
203203
};

src/librustc/middle/stability.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ impl<'a, 'tcx: 'a> Annotator<'a, 'tcx> {
205205
} else {
206206
// Emit errors for non-staged-api crates.
207207
for attr in attrs {
208-
let tag = unwrap_or!(attr.name(), continue);
208+
let tag = attr.name();
209209
if tag == "unstable" || tag == "stable" || tag == "rustc_deprecated" {
210210
attr::mark_used(attr);
211211
self.tcx.sess.span_err(attr.span(), "stability attributes may not be used \

src/librustc_lint/builtin.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -663,9 +663,8 @@ impl LintPass for DeprecatedAttr {
663663

664664
impl EarlyLintPass for DeprecatedAttr {
665665
fn check_attribute(&mut self, cx: &EarlyContext, attr: &ast::Attribute) {
666-
let name = unwrap_or!(attr.name(), return);
667666
for &&(n, _, ref g) in &self.depr_attrs {
668-
if name == n {
667+
if attr.name() == n {
669668
if let &AttributeGate::Gated(Stability::Deprecated(link),
670669
ref name,
671670
ref reason,

src/librustc_lint/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
#![feature(rustc_diagnostic_macros)]
3434
#![feature(slice_patterns)]
3535

36-
#[macro_use]
3736
extern crate syntax;
3837
#[macro_use]
3938
extern crate rustc;

src/librustc_lint/unused.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,6 @@ impl LintPass for UnusedAttributes {
174174
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedAttributes {
175175
fn check_attribute(&mut self, cx: &LateContext, attr: &ast::Attribute) {
176176
debug!("checking attribute: {:?}", attr);
177-
let name = unwrap_or!(attr.name(), return);
178-
179177
// Note that check_name() marks the attribute as used if it matches.
180178
for &(ref name, ty, _) in BUILTIN_ATTRIBUTES {
181179
match ty {
@@ -195,6 +193,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedAttributes {
195193
}
196194
}
197195

196+
let name = attr.name();
198197
if !attr::is_used(attr) {
199198
debug!("Emitting warning for: {:?}", attr);
200199
cx.span_lint(UNUSED_ATTRIBUTES, attr.span, "unused attribute");

src/librustc_resolve/macros.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ impl<'a> base::Resolver for Resolver<'a> {
210210
fn find_legacy_attr_invoc(&mut self, attrs: &mut Vec<ast::Attribute>)
211211
-> Option<ast::Attribute> {
212212
for i in 0..attrs.len() {
213-
let name = unwrap_or!(attrs[i].name(), continue);
213+
let name = attrs[i].name();
214214

215215
if self.session.plugin_attributes.borrow().iter()
216216
.any(|&(ref attr_nm, _)| name == &**attr_nm) {
@@ -230,11 +230,11 @@ impl<'a> base::Resolver for Resolver<'a> {
230230

231231
// Check for legacy derives
232232
for i in 0..attrs.len() {
233-
let name = unwrap_or!(attrs[i].name(), continue);
233+
let name = attrs[i].name();
234234

235235
if name == "derive" {
236236
let result = attrs[i].parse_list(&self.session.parse_sess, |parser| {
237-
parser.parse_path(PathStyle::Mod)
237+
parser.parse_path_allowing_meta(PathStyle::Mod)
238238
});
239239

240240
let mut traits = match result {

src/librustdoc/clean/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3359,7 +3359,7 @@ impl Clean<Vec<Item>> for doctree::Import {
33593359
// #[doc(no_inline)] attribute is present.
33603360
// Don't inline doc(hidden) imports so they can be stripped at a later stage.
33613361
let denied = self.vis != hir::Public || self.attrs.iter().any(|a| {
3362-
a.name().unwrap() == "doc" && match a.meta_item_list() {
3362+
a.name() == "doc" && match a.meta_item_list() {
33633363
Some(l) => attr::list_contains_name(&l, "no_inline") ||
33643364
attr::list_contains_name(&l, "hidden"),
33653365
None => false,

src/librustdoc/html/render.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3077,7 +3077,7 @@ fn render_attributes(w: &mut fmt::Formatter, it: &clean::Item) -> fmt::Result {
30773077
let mut attrs = String::new();
30783078

30793079
for attr in &it.attrs.other_attrs {
3080-
let name = attr.name().unwrap();
3080+
let name = attr.name();
30813081
if !ATTRIBUTE_WHITELIST.contains(&&*name.as_str()) {
30823082
continue;
30833083
}

src/libsyntax/attr.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,10 @@ impl Attribute {
218218
matches
219219
}
220220

221-
pub fn name(&self) -> Option<Name> {
222-
match self.path.segments.len() {
223-
1 => Some(self.path.segments[0].identifier.name),
224-
_ => None,
225-
}
221+
/// Returns the first segment of the name of this attribute.
222+
/// E.g. `foo` for `#[foo]`, `rustfmt` for `#[rustfmt::skip]`.
223+
pub fn name(&self) -> Name {
224+
name_from_path(&self.path)
226225
}
227226

228227
pub fn value_str(&self) -> Option<Symbol> {

src/libsyntax/ext/derive.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ pub fn collect_derives(cx: &mut ExtCtxt, attrs: &mut Vec<ast::Attribute>) -> Vec
2626
return true;
2727
}
2828

29-
match attr.parse_list(cx.parse_sess, |parser| parser.parse_path(PathStyle::Mod)) {
29+
match attr.parse_list(cx.parse_sess,
30+
|parser| parser.parse_path_allowing_meta(PathStyle::Mod)) {
3031
Ok(ref traits) if traits.is_empty() => {
3132
cx.span_warn(attr.span, "empty trait list in `derive`");
3233
false

src/libsyntax/feature_gate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1074,7 +1074,7 @@ macro_rules! gate_feature {
10741074
impl<'a> Context<'a> {
10751075
fn check_attribute(&self, attr: &ast::Attribute, is_macro: bool) {
10761076
debug!("check_attribute(attr = {:?})", attr);
1077-
let name = unwrap_or!(attr.name(), return).as_str();
1077+
let name = attr.name().as_str();
10781078
for &(n, ty, ref gateage) in BUILTIN_ATTRIBUTES {
10791079
if name == n {
10801080
if let Gated(_, name, desc, ref has_feature) = *gateage {

src/libsyntax/parse/parser.rs

+20
Original file line numberDiff line numberDiff line change
@@ -1988,6 +1988,26 @@ impl<'a> Parser<'a> {
19881988
Ok(ast::Path { segments, span: lo.to(self.prev_span) })
19891989
}
19901990

1991+
/// Like `parse_path`, but also supports parsing `Word` meta items into paths for back-compat.
1992+
/// This is used when parsing derive macro paths in `#[derive]` attributes.
1993+
pub fn parse_path_allowing_meta(&mut self, style: PathStyle) -> PResult<'a, ast::Path> {
1994+
let meta_name = match self.token {
1995+
token::Interpolated(ref nt) => match nt.0 {
1996+
token::NtMeta(ref meta) => match meta.node {
1997+
ast::MetaItemKind::Word => Some(meta.name.clone()),
1998+
_ => None,
1999+
},
2000+
_ => None,
2001+
},
2002+
_ => None,
2003+
};
2004+
if let Some(path) = meta_name {
2005+
self.bump();
2006+
return Ok(path);
2007+
}
2008+
self.parse_path(style)
2009+
}
2010+
19912011
fn parse_path_segments(&mut self,
19922012
segments: &mut Vec<PathSegment>,
19932013
style: PathStyle,

src/libsyntax/print/pprust.rs

+1
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,7 @@ pub trait PrintState<'a> {
770770
ast::MetaItemKind::Word => self.print_attribute_path(&item.name)?,
771771
ast::MetaItemKind::NameValue(ref value) => {
772772
self.print_attribute_path(&item.name)?;
773+
self.writer().space()?;
773774
self.word_space("=")?;
774775
self.print_literal(value)?;
775776
}

src/libsyntax_ext/deriving/custom.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,9 @@ struct MarkAttrs<'a>(&'a [ast::Name]);
2222

2323
impl<'a> Visitor<'a> for MarkAttrs<'a> {
2424
fn visit_attribute(&mut self, attr: &Attribute) {
25-
if let Some(name) = attr.name() {
26-
if self.0.contains(&name) {
27-
mark_used(attr);
28-
mark_known(attr);
29-
}
25+
if self.0.contains(&attr.name()) {
26+
mark_used(attr);
27+
mark_known(attr);
3028
}
3129
}
3230

src/libsyntax_ext/deriving/generic/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ impl<'a> TraitDef<'a> {
467467
attrs.extend(item.attrs
468468
.iter()
469469
.filter(|a| {
470-
a.name().is_some() && match &*a.name().unwrap().as_str() {
470+
match &*a.name().as_str() {
471471
"allow" | "warn" | "deny" | "forbid" | "stable" | "unstable" => true,
472472
_ => false,
473473
}

0 commit comments

Comments
 (0)