未验证 提交 9b3aea60 编写于 作者: S Seiichi Uchida 提交者: flip1995

Remove Option from the return type of Attribute::name()

上级 759bd01e
......@@ -153,10 +153,7 @@ fn check_repr(&self, item: &hir::Item, target: Target) {
// ```
let hints: Vec<_> = item.attrs
.iter()
.filter(|attr| match attr.name() {
Some(name) => name == "repr",
None => false,
})
.filter(|attr| attr.name() == "repr")
.filter_map(|attr| attr.meta_item_list())
.flat_map(|hints| hints)
.collect();
......
......@@ -199,8 +199,7 @@ fn hash_stable<W: StableHasherResult>(&self,
let filtered: AccumulateVec<[&ast::Attribute; 8]> = self
.iter()
.filter(|attr| {
!attr.is_sugared_doc &&
attr.name().map(|name| !hcx.is_ignored_attr(name)).unwrap_or(true)
!attr.is_sugared_doc && !hcx.is_ignored_attr(attr.name())
})
.collect();
......@@ -227,7 +226,7 @@ fn hash_stable<W: StableHasherResult>(&self,
hcx: &mut StableHashingContext<'a>,
hasher: &mut StableHasher<W>) {
// Make sure that these have been filtered out.
debug_assert!(self.name().map(|name| !hcx.is_ignored_attr(name)).unwrap_or(true));
debug_assert!(!hcx.is_ignored_attr(self.name()));
debug_assert!(!self.is_sugared_doc);
let ast::Attribute {
......
......@@ -198,7 +198,7 @@ pub fn push(&mut self, attrs: &[ast::Attribute]) -> BuilderPush {
"malformed lint attribute");
};
for attr in attrs {
let level = match attr.name().and_then(|name| Level::from_str(&name.as_str())) {
let level = match Level::from_str(&attr.name().as_str()) {
None => continue,
Some(lvl) => lvl,
};
......
......@@ -205,7 +205,7 @@ fn annotate<F>(&mut self, id: NodeId, attrs: &[Attribute],
} else {
// Emit errors for non-staged-api crates.
for attr in attrs {
let tag = unwrap_or!(attr.name(), continue);
let tag = attr.name();
if tag == "unstable" || tag == "stable" || tag == "rustc_deprecated" {
attr::mark_used(attr);
self.tcx.sess.span_err(attr.span(), "stability attributes may not be used \
......
......@@ -675,9 +675,8 @@ fn get_lints(&self) -> LintArray {
impl EarlyLintPass for DeprecatedAttr {
fn check_attribute(&mut self, cx: &EarlyContext, attr: &ast::Attribute) {
let name = unwrap_or!(attr.name(), return);
for &&(n, _, ref g) in &self.depr_attrs {
if name == n {
if attr.name() == n {
if let &AttributeGate::Gated(Stability::Deprecated(link),
ref name,
ref reason,
......
......@@ -30,7 +30,6 @@
#![feature(quote)]
#![feature(rustc_diagnostic_macros)]
#[macro_use]
extern crate syntax;
#[macro_use]
extern crate rustc;
......
......@@ -192,8 +192,6 @@ fn get_lints(&self) -> LintArray {
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedAttributes {
fn check_attribute(&mut self, cx: &LateContext, attr: &ast::Attribute) {
debug!("checking attribute: {:?}", attr);
let name = unwrap_or!(attr.name(), return);
// Note that check_name() marks the attribute as used if it matches.
for &(ref name, ty, _) in BUILTIN_ATTRIBUTES {
match ty {
......@@ -213,6 +211,7 @@ fn check_attribute(&mut self, cx: &LateContext, attr: &ast::Attribute) {
}
}
let name = attr.name();
if !attr::is_used(attr) {
debug!("Emitting warning for: {:?}", attr);
cx.span_lint(UNUSED_ATTRIBUTES, attr.span, "unused attribute");
......
......@@ -209,7 +209,7 @@ fn resolve_imports(&mut self) {
fn find_legacy_attr_invoc(&mut self, attrs: &mut Vec<ast::Attribute>, allow_derive: bool)
-> Option<ast::Attribute> {
for i in 0..attrs.len() {
let name = unwrap_or!(attrs[i].name(), continue);
let name = attrs[i].name();
if self.session.plugin_attributes.borrow().iter()
.any(|&(ref attr_nm, _)| name == &**attr_nm) {
......@@ -231,11 +231,11 @@ fn find_legacy_attr_invoc(&mut self, attrs: &mut Vec<ast::Attribute>, allow_deri
// Check for legacy derives
for i in 0..attrs.len() {
let name = unwrap_or!(attrs[i].name(), continue);
let name = attrs[i].name();
if name == "derive" {
let result = attrs[i].parse_list(&self.session.parse_sess, |parser| {
parser.parse_path(PathStyle::Mod)
parser.parse_path_allowing_meta(PathStyle::Mod)
});
let mut traits = match result {
......
......@@ -3661,7 +3661,7 @@ fn clean(&self, cx: &DocContext) -> Vec<Item> {
// #[doc(no_inline)] attribute is present.
// Don't inline doc(hidden) imports so they can be stripped at a later stage.
let denied = self.vis != hir::Public || self.attrs.iter().any(|a| {
a.name().unwrap() == "doc" && match a.meta_item_list() {
a.name() == "doc" && match a.meta_item_list() {
Some(l) => attr::list_contains_name(&l, "no_inline") ||
attr::list_contains_name(&l, "hidden"),
None => false,
......
......@@ -3319,7 +3319,7 @@ fn render_attributes(w: &mut fmt::Formatter, it: &clean::Item) -> fmt::Result {
let mut attrs = String::new();
for attr in &it.attrs.other_attrs {
let name = attr.name().unwrap();
let name = attr.name();
if !ATTRIBUTE_WHITELIST.contains(&&*name.as_str()) {
continue;
}
......
......@@ -217,11 +217,10 @@ pub fn check_name(&self, name: &str) -> bool {
matches
}
pub fn name(&self) -> Option<Name> {
match self.path.segments.len() {
1 => Some(self.path.segments[0].identifier.name),
_ => None,
}
/// Returns the first segment of the name of this attribute.
/// E.g. `foo` for `#[foo]`, `rustfmt` for `#[rustfmt::skip]`.
pub fn name(&self) -> Name {
name_from_path(&self.path)
}
pub fn value_str(&self) -> Option<Symbol> {
......
......@@ -26,7 +26,8 @@ pub fn collect_derives(cx: &mut ExtCtxt, attrs: &mut Vec<ast::Attribute>) -> Vec
return true;
}
match attr.parse_list(cx.parse_sess, |parser| parser.parse_path(PathStyle::Mod)) {
match attr.parse_list(cx.parse_sess,
|parser| parser.parse_path_allowing_meta(PathStyle::Mod)) {
Ok(ref traits) if traits.is_empty() => {
cx.span_warn(attr.span, "empty trait list in `derive`");
false
......
......@@ -1132,7 +1132,7 @@ struct Context<'a> {
impl<'a> Context<'a> {
fn check_attribute(&self, attr: &ast::Attribute, is_macro: bool) {
debug!("check_attribute(attr = {:?})", attr);
let name = unwrap_or!(attr.name(), return).as_str();
let name = attr.name().as_str();
for &(n, ty, ref gateage) in BUILTIN_ATTRIBUTES {
if name == n {
if let Gated(_, name, desc, ref has_feature) = *gateage {
......
......@@ -1955,19 +1955,19 @@ pub fn parse_path_common(&mut self, style: PathStyle, enable_warning: bool)
/// Like `parse_path`, but also supports parsing `Word` meta items into paths for back-compat.
/// This is used when parsing derive macro paths in `#[derive]` attributes.
pub fn parse_path_allowing_meta(&mut self, style: PathStyle) -> PResult<'a, ast::Path> {
let meta_ident = match self.token {
let meta_name = match self.token {
token::Interpolated(ref nt) => match nt.0 {
token::NtMeta(ref meta) => match meta.node {
ast::MetaItemKind::Word => Some(meta.ident),
ast::MetaItemKind::Word => Some(meta.name.clone()),
_ => None,
},
_ => None,
},
_ => None,
};
if let Some(ident) = meta_ident {
if let Some(path) = meta_name {
self.bump();
return Ok(ast::Path::from_ident(ident));
return Ok(path);
}
self.parse_path(style)
}
......
......@@ -776,6 +776,7 @@ fn print_meta_item(&mut self, item: &ast::MetaItem) -> io::Result<()> {
ast::MetaItemKind::Word => self.print_attribute_path(&item.name)?,
ast::MetaItemKind::NameValue(ref value) => {
self.print_attribute_path(&item.name)?;
self.writer().space()?;
self.word_space("=")?;
self.print_literal(value)?;
}
......
......@@ -22,11 +22,9 @@
impl<'a> Visitor<'a> for MarkAttrs<'a> {
fn visit_attribute(&mut self, attr: &Attribute) {
if let Some(name) = attr.name() {
if self.0.contains(&name) {
mark_used(attr);
mark_known(attr);
}
if self.0.contains(&attr.name()) {
mark_used(attr);
mark_known(attr);
}
}
......
......@@ -472,7 +472,7 @@ pub fn expand_ext(self,
attrs.extend(item.attrs
.iter()
.filter(|a| {
a.name().is_some() && match &*a.name().unwrap().as_str() {
match &*a.name().as_str() {
"allow" | "warn" | "deny" | "forbid" | "stable" | "unstable" => true,
_ => false,
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册