Skip to content

Commit 0eccb4f

Browse files
committed
Use an iterator + collect instead of a for loop
The panics are removed as rust-lang/rust#65785 has made them an error and the compiler will complain anyway for ill formed attributes. The use _could_ allow them, technically. But this doesn't really exist: https://github.com/search?q=allow%28ill_formed_attribute_input%29&type=code Shows 4 occurences at time of commit. I would also think that users that enable this, should know what they are doing ;) Signed-off-by: Marcel Müller <[email protected]>
1 parent 055ab13 commit 0eccb4f

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed

crates/proc_macros/src/util.rs

+17-21
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,26 @@
1-
use darling::ToTokens;
21
use syn::visit_mut::VisitMut;
3-
use syn::{Attribute, Expr, GenericArgument, ItemImpl, Lit, Meta, PathArguments, Type, TypePath};
2+
use syn::{Attribute, Expr, ExprLit, GenericArgument, ItemImpl, Lit, Meta, MetaNameValue, PathArguments, Type, TypePath};
43

54
/// From a let of attributes to an item, extracts the ones that are documentation, as strings.
65
pub fn extract_doc_lines(attributes: &[Attribute]) -> Vec<String> {
7-
let mut docs = Vec::new();
8-
9-
for attr in attributes {
10-
if &attr.path().to_token_stream().to_string() != "doc" {
11-
continue;
12-
}
13-
14-
if let Meta::NameValue(x) = &attr.meta {
15-
match &x.value {
16-
Expr::Lit(x) => match &x.lit {
17-
Lit::Str(x) => {
18-
docs.push(x.value());
19-
}
20-
_ => panic!("Unexpected content in doc string: not a string."),
21-
},
22-
_ => panic!("Unexpected content in doc string: not a literal."),
6+
attributes
7+
.iter()
8+
.filter_map(|attr| {
9+
if !attr.path().is_ident("doc") {
10+
return None;
2311
}
24-
}
25-
}
2612

27-
docs
13+
let Meta::NameValue(MetaNameValue {
14+
value: Expr::Lit(ExprLit { lit: Lit::Str(litstr), .. }),
15+
..
16+
}) = &attr.meta
17+
else {
18+
return None;
19+
};
20+
21+
Some(litstr.value())
22+
})
23+
.collect()
2824
}
2925

3026
struct LifetimeRemover;

0 commit comments

Comments
 (0)