Skip to content

Commit

Permalink
Change: Result<Option<String>, String> -> Option<String> and warn!
Browse files Browse the repository at this point in the history
  • Loading branch information
sevenc-nanashi committed Aug 27, 2023
1 parent 50b6171 commit 05d7035
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/bindgen/ir/annotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl AnnotationSet {
.collect();

let must_use = attrs.has_attr_word("must_use");
let deprecated = attrs.find_deprecated_note()?;
let deprecated = attrs.find_deprecated_note();
let mut annotations = HashMap::new();

// Look at each line for an annotation
Expand Down
26 changes: 16 additions & 10 deletions src/bindgen/utilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,16 @@ pub trait SynAttributeHelpers {
})
}

fn find_deprecated_note(&self) -> Result<Option<String>, String> {
fn find_deprecated_note(&self) -> Option<String> {
let attrs = self.attrs();
// #[deprecated = ""]
if let Some(note) = attrs.attr_name_value_lookup("deprecated") {
return Ok(Some(note));
return Some(note);
}

// #[deprecated]
if attrs.has_attr_word("deprecated") {
return Ok(Some("".to_string()));
return Some("".to_string());
}

// #[deprecated(note = "")]
Expand All @@ -136,25 +136,31 @@ pub trait SynAttributeHelpers {
false
}
}) {
let args: syn::punctuated::Punctuated<syn::MetaNameValue, Token![,]> = attr
.parse_args_with(syn::punctuated::Punctuated::parse_terminated)
.map_err(|e| format!("Couldn't parse deprecated attribute: {}", e.to_string()))?;
let Ok(args): Result<syn::punctuated::Punctuated<syn::MetaNameValue, Token![,]>, _> =
attr.parse_args_with(syn::punctuated::Punctuated::parse_terminated)
else {
warn!("couldn't parse deprecated attribute");
return None;
};

let Some(lit) = args
.iter()
.find(|arg| arg.path.is_ident("note"))
.map(|arg| &arg.lit)
else {
return Err("Couldn't parse deprecated attribute: no `note` field".to_string());
warn!("couldn't parse deprecated attribute: no `note` field");
return None;
};

return if let syn::Lit::Str(lit) = lit {
Ok(Some(lit.value()))
Some(lit.value())
} else {
Err("deprecated attribute must be a string".to_string())
warn!("deprecated attribute must be a string");
None
};
}

Ok(None)
None
}

fn is_no_mangle(&self) -> bool {
Expand Down

0 comments on commit 05d7035

Please sign in to comment.