Skip to content

Commit d80f2d3

Browse files
committed
use Symbol comparison in get_skip_names
1 parent 9a0a9ec commit d80f2d3

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

src/skip.rs

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
//! Module that contains skip related stuffs.
22
33
use syntax::ast;
4+
use syntax::symbol::{sym, Symbol};
5+
6+
macro_rules! sym {
7+
($tt:tt) => {
8+
Symbol::intern(stringify!($tt))
9+
};
10+
}
411

512
/// Take care of skip name stack. You can update it by attributes slice or
613
/// by other context. Query this context to know if you need skip a block.
@@ -12,9 +19,9 @@ pub(crate) struct SkipContext {
1219

1320
impl SkipContext {
1421
pub(crate) fn update_with_attrs(&mut self, attrs: &[ast::Attribute]) {
15-
self.macros.append(&mut get_skip_names("macros", attrs));
22+
self.macros.append(&mut get_skip_names(attrs, sym!(macros)));
1623
self.attributes
17-
.append(&mut get_skip_names("attributes", attrs));
24+
.append(&mut get_skip_names(attrs, sym::attributes));
1825
}
1926

2027
pub(crate) fn update(&mut self, mut other: SkipContext) {
@@ -31,40 +38,31 @@ impl SkipContext {
3138
}
3239
}
3340

34-
static RUSTFMT: &str = "rustfmt";
35-
static SKIP: &str = "skip";
36-
3741
/// Say if you're playing with `rustfmt`'s skip attribute
3842
pub(crate) fn is_skip_attr(segments: &[ast::PathSegment]) -> bool {
39-
if segments.len() < 2 || segments[0].ident.as_str() != RUSTFMT {
43+
is_skip_attr_with(segments, |s| s == sym!(macros) || s == sym::attributes)
44+
}
45+
46+
fn is_skip_attr_with(segments: &[ast::PathSegment], pred: impl FnOnce(Symbol) -> bool) -> bool {
47+
if segments.len() < 2 || segments[0].ident.name != sym::rustfmt {
4048
return false;
4149
}
4250
match segments.len() {
43-
2 => segments[1].ident.as_str() == SKIP,
44-
3 => {
45-
segments[1].ident.as_str() == SKIP
46-
&& ["macros", "attributes"]
47-
.iter()
48-
.any(|&n| n == segments[2].ident.name.as_str())
49-
}
51+
2 => segments[1].ident.name == sym!(skip),
52+
3 => segments[1].ident.name == sym!(skip) && pred(segments[2].ident.name),
5053
_ => false,
5154
}
5255
}
5356

54-
fn get_skip_names(kind: &str, attrs: &[ast::Attribute]) -> Vec<String> {
57+
fn get_skip_names(attrs: &[ast::Attribute], kind: Symbol) -> Vec<String> {
5558
let mut skip_names = vec![];
56-
let path = format!("{}::{}::{}", RUSTFMT, SKIP, kind);
5759
for attr in attrs {
58-
// syntax::ast::Path is implemented partialEq
59-
// but it is designed for segments.len() == 1
60-
if format!("{}", attr.path) != path {
61-
continue;
62-
}
63-
64-
if let Some(list) = attr.meta_item_list() {
65-
for nested_meta_item in list {
66-
if let Some(name) = nested_meta_item.ident() {
67-
skip_names.push(name.to_string());
60+
if is_skip_attr_with(&attr.item.path.segments, |s| s == kind) {
61+
if let Some(list) = attr.meta_item_list() {
62+
for nested_meta_item in list {
63+
if let Some(name) = nested_meta_item.ident() {
64+
skip_names.push(name.to_string());
65+
}
6866
}
6967
}
7068
}

0 commit comments

Comments
 (0)