Skip to content

Commit

Permalink
Fix map_unwrap_or lint
Browse files Browse the repository at this point in the history
```
cargo clippy --workspace --exclude bindgen-integration --exclude tests_expectations -- -W clippy::map_unwrap_or
```
  • Loading branch information
nyurik authored and pvdrz committed Dec 1, 2024
1 parent 3c1a619 commit 09f32b3
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 28 deletions.
6 changes: 2 additions & 4 deletions bindgen/clang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1110,10 +1110,8 @@ impl Iterator for ClangTokenIterator<'_> {
/// (including '_') and does not start with a digit.
pub(crate) fn is_valid_identifier(name: &str) -> bool {
let mut chars = name.chars();
let first_valid = chars
.next()
.map(|c| c.is_alphabetic() || c == '_')
.unwrap_or(false);
let first_valid =
chars.next().is_some_and(|c| c.is_alphabetic() || c == '_');

first_valid && chars.all(|c| c.is_alphanumeric() || c == '_')
}
Expand Down
12 changes: 4 additions & 8 deletions bindgen/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5145,14 +5145,10 @@ pub(crate) mod utils {
return Ok(());
}

let path = context
.options()
.wrap_static_fns_path
.as_ref()
.map(PathBuf::from)
.unwrap_or_else(|| {
std::env::temp_dir().join("bindgen").join("extern")
});
let path = context.options().wrap_static_fns_path.as_ref().map_or_else(
|| std::env::temp_dir().join("bindgen").join("extern"),
PathBuf::from,
);

let dir = path.parent().unwrap();

Expand Down
3 changes: 1 addition & 2 deletions bindgen/codegen/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ use super::{CodegenError, WrapAsVariadic};

fn get_loc(item: &Item) -> String {
item.location()
.map(|x| x.to_string())
.unwrap_or_else(|| "unknown".to_owned())
.map_or_else(|| "unknown".to_owned(), |x| x.to_string())
}

pub(super) trait CSerialize<'a> {
Expand Down
21 changes: 9 additions & 12 deletions bindgen/ir/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -786,16 +786,14 @@ impl Item {

match *self.kind() {
ItemKind::Var(ref var) => var.name().to_owned(),
ItemKind::Module(ref module) => {
module.name().map(ToOwned::to_owned).unwrap_or_else(|| {
format!("_bindgen_mod_{}", self.exposed_id(ctx))
})
}
ItemKind::Type(ref ty) => {
ty.sanitized_name(ctx).map(Into::into).unwrap_or_else(|| {
format!("_bindgen_ty_{}", self.exposed_id(ctx))
})
}
ItemKind::Module(ref module) => module.name().map_or_else(
|| format!("_bindgen_mod_{}", self.exposed_id(ctx)),
ToOwned::to_owned,
),
ItemKind::Type(ref ty) => ty.sanitized_name(ctx).map_or_else(
|| format!("_bindgen_ty_{}", self.exposed_id(ctx)),
Into::into,
),
ItemKind::Function(ref fun) => {
let mut name = fun.name().to_owned();

Expand Down Expand Up @@ -1702,8 +1700,7 @@ impl Item {
ty.spelling()
);
Item::type_param(Some(id), location, ctx)
.map(Ok)
.unwrap_or(Err(ParseError::Recurse))
.ok_or(ParseError::Recurse)
} else {
result
}
Expand Down
4 changes: 2 additions & 2 deletions bindgen/options/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ where
}

fn add_derives(&self, info: &DeriveInfo<'_>) -> Vec<String> {
if self.kind.map(|kind| kind == info.kind).unwrap_or(true) &&
if self.kind.map_or(true, |kind| kind == info.kind) &&
self.regex_set.matches(info.name)
{
return self.derives.clone();
Expand Down Expand Up @@ -745,7 +745,7 @@ where
}

fn add_attributes(&self, info: &AttributeInfo<'_>) -> Vec<String> {
if self.kind.map(|kind| kind == info.kind).unwrap_or(true) &&
if self.kind.map_or(true, |kind| kind == info.kind) &&
self.regex_set.matches(info.name)
{
return self.attributes.clone();
Expand Down

0 comments on commit 09f32b3

Please sign in to comment.