Skip to content

Commit a194c89

Browse files
committed
style(prometheus-client-derive): fix rustfmt and clippy reports
Signed-off-by: ADD-SP <[email protected]>
1 parent ecbe6f6 commit a194c89

File tree

2 files changed

+26
-46
lines changed

2 files changed

+26
-46
lines changed

prometheus-client-derive/src/registrant/attribute.rs

+12-23
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,19 @@
1+
use proc_macro2::Span;
12
use quote::ToTokens;
23
use syn::spanned::Spanned;
3-
use proc_macro2::Span;
44

55
// do not derive debug since this needs "extra-traits"
66
// feature for crate `syn`, which slows compile time
77
// too much, and is not needed as this struct is not
88
// public.
9+
#[derive(Default)]
910
pub struct Attribute {
1011
pub help: Option<syn::LitStr>,
1112
pub unit: Option<syn::LitStr>,
1213
pub rename: Option<syn::LitStr>,
1314
pub skip: bool,
1415
}
1516

16-
impl Default for Attribute {
17-
fn default() -> Self {
18-
Attribute {
19-
help: None,
20-
unit: None,
21-
rename: None,
22-
skip: false,
23-
}
24-
}
25-
}
26-
2717
impl Attribute {
2818
fn with_help(mut self, doc: syn::LitStr) -> Self {
2919
self.help = Some(doc);
@@ -39,10 +29,11 @@ impl Attribute {
3929
let mut acc = merged
4030
.help
4131
.unwrap_or_else(|| syn::LitStr::new("", doc.span()))
42-
.value().trim()
32+
.value()
33+
.trim()
4334
.to_string();
4435
acc.push(' ');
45-
acc.push_str(&doc.value().trim());
36+
acc.push_str(doc.value().trim());
4637
merged.help = Some(syn::LitStr::new(&acc, Span::call_site()));
4738
}
4839
if let Some(unit) = other.unit {
@@ -82,12 +73,12 @@ impl syn::parse::Parse for Attribute {
8273
syn::Meta::NameValue(meta) if meta.path.is_ident("doc") => {
8374
if let syn::Expr::Lit(lit) = meta.value {
8475
let lit_str = syn::parse2::<syn::LitStr>(lit.lit.to_token_stream())?;
85-
return Ok(Attribute::default().with_help(lit_str));
76+
Ok(Attribute::default().with_help(lit_str))
8677
} else {
87-
return Err(syn::Error::new_spanned(
78+
Err(syn::Error::new_spanned(
8879
meta.value,
8980
"Expected a string literal for doc attribute",
90-
));
81+
))
9182
}
9283
}
9384
syn::Meta::List(meta) if meta.path.is_ident("registrant") => {
@@ -135,12 +126,10 @@ impl syn::parse::Parse for Attribute {
135126
})?;
136127
Ok(attr)
137128
}
138-
_ => {
139-
return Err(syn::Error::new(
140-
span,
141-
r#"Unknown attribute, expected `#[doc(...)]` or `#[registrant(<key>[=value], ...)]`"#,
142-
))
143-
}
129+
_ => Err(syn::Error::new(
130+
span,
131+
r#"Unknown attribute, expected `#[doc(...)]` or `#[registrant(<key>[=value], ...)]`"#,
132+
)),
144133
}
145134
}
146135
}

prometheus-client-derive/src/registrant/field.rs

+14-23
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use quote::ToTokens;
2-
use crate::registrant::attribute;
31
use super::attribute::Attribute;
2+
use crate::registrant::attribute;
3+
use quote::ToTokens;
44

55
// do not derive debug since this needs "extra-traits"
66
// feature for crate `syn`, which slows compile time
@@ -25,13 +25,10 @@ impl Field {
2525
}
2626

2727
pub(super) fn help(&self) -> syn::LitStr {
28-
self.attr.help.clone()
29-
.unwrap_or_else(|| {
30-
syn::LitStr::new(
31-
"",
32-
self.ident.span(),
33-
)
34-
})
28+
self.attr
29+
.help
30+
.clone()
31+
.unwrap_or_else(|| syn::LitStr::new("", self.ident.span()))
3532
}
3633

3734
pub(super) fn unit(&self) -> Option<&syn::LitStr> {
@@ -47,28 +44,22 @@ impl TryFrom<syn::Field> for Field {
4744
type Error = syn::Error;
4845

4946
fn try_from(field: syn::Field) -> Result<Self, Self::Error> {
50-
let ident = field.ident.clone().expect("Fields::Named should have an identifier");
51-
let name = syn::LitStr::new(
52-
&ident.to_string(),
53-
ident.span(),
54-
);
47+
let ident = field
48+
.ident
49+
.clone()
50+
.expect("Fields::Named should have an identifier");
51+
let name = syn::LitStr::new(&ident.to_string(), ident.span());
5552
let attr = field
5653
.attrs
5754
.into_iter()
5855
// ignore unknown attributes, which might be defined by another derive macros.
59-
.filter(|attr| attr.path().is_ident("doc") || attr.path().is_ident("registrant") )
56+
.filter(|attr| attr.path().is_ident("doc") || attr.path().is_ident("registrant"))
6057
.try_fold(vec![], |mut acc, attr| {
6158
acc.push(syn::parse2::<Attribute>(attr.meta.into_token_stream())?);
6259
Ok::<Vec<attribute::Attribute>, syn::Error>(acc)
6360
})?
6461
.into_iter()
65-
.try_fold(Attribute::default(), |acc, attr| {
66-
acc.merge(attr)
67-
})?;
68-
Ok(Field{
69-
ident,
70-
name,
71-
attr,
72-
})
62+
.try_fold(Attribute::default(), |acc, attr| acc.merge(attr))?;
63+
Ok(Field { ident, name, attr })
7364
}
7465
}

0 commit comments

Comments
 (0)