1
Fork 0

Rollup merge of #82033 - magurotuna:issue82016, r=jyn514

Refactor `get_word_attr` to return only `Option`

This commit removes `bool` from the return type of `NestedAttributesExt::get_word_attr` so it will return only `Option<ast::NestedMetaItem>` for less redundancy.

Closes #82016

r? `@jyn514`
This commit is contained in:
Yuki Okushi 2021-02-13 16:36:48 +09:00 committed by GitHub
commit 4c8e38aa60
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 17 deletions

View file

@ -2161,18 +2161,20 @@ fn clean_use_statement(
return Vec::new(); return Vec::new();
} }
let (doc_meta_item, please_inline) = import.attrs.lists(sym::doc).get_word_attr(sym::inline); let inline_attr = import.attrs.lists(sym::doc).get_word_attr(sym::inline);
let pub_underscore = import.vis.node.is_pub() && name == kw::Underscore; let pub_underscore = import.vis.node.is_pub() && name == kw::Underscore;
if pub_underscore && please_inline { if pub_underscore {
rustc_errors::struct_span_err!( if let Some(ref inline) = inline_attr {
cx.tcx.sess, rustc_errors::struct_span_err!(
doc_meta_item.unwrap().span(), cx.tcx.sess,
E0780, inline.span(),
"anonymous imports cannot be inlined" E0780,
) "anonymous imports cannot be inlined"
.span_label(import.span, "anonymous import") )
.emit(); .span_label(import.span, "anonymous import")
.emit();
}
} }
// We consider inlining the documentation of `pub use` statements, but we // We consider inlining the documentation of `pub use` statements, but we
@ -2205,7 +2207,7 @@ fn clean_use_statement(
} }
Import::new_glob(resolve_use_source(cx, path), true) Import::new_glob(resolve_use_source(cx, path), true)
} else { } else {
if !please_inline { if inline_attr.is_none() {
if let Res::Def(DefKind::Mod, did) = path.res { if let Res::Def(DefKind::Mod, did) = path.res {
if !did.is_local() && did.index == CRATE_DEF_INDEX { if !did.is_local() && did.index == CRATE_DEF_INDEX {
// if we're `pub use`ing an extern crate root, don't inline it unless we // if we're `pub use`ing an extern crate root, don't inline it unless we

View file

@ -438,7 +438,7 @@ impl AttributesExt for [ast::Attribute] {
crate trait NestedAttributesExt { crate trait NestedAttributesExt {
/// Returns `true` if the attribute list contains a specific `Word` /// Returns `true` if the attribute list contains a specific `Word`
fn has_word(self, word: Symbol) -> bool; fn has_word(self, word: Symbol) -> bool;
fn get_word_attr(self, word: Symbol) -> (Option<ast::NestedMetaItem>, bool); fn get_word_attr(self, word: Symbol) -> Option<ast::NestedMetaItem>;
} }
impl<I: Iterator<Item = ast::NestedMetaItem> + IntoIterator<Item = ast::NestedMetaItem>> impl<I: Iterator<Item = ast::NestedMetaItem> + IntoIterator<Item = ast::NestedMetaItem>>
@ -448,11 +448,8 @@ impl<I: Iterator<Item = ast::NestedMetaItem> + IntoIterator<Item = ast::NestedMe
self.into_iter().any(|attr| attr.is_word() && attr.has_name(word)) self.into_iter().any(|attr| attr.is_word() && attr.has_name(word))
} }
fn get_word_attr(mut self, word: Symbol) -> (Option<ast::NestedMetaItem>, bool) { fn get_word_attr(mut self, word: Symbol) -> Option<ast::NestedMetaItem> {
match self.find(|attr| attr.is_word() && attr.has_name(word)) { self.find(|attr| attr.is_word() && attr.has_name(word))
Some(a) => (Some(a), true),
None => (None, false),
}
} }
} }