Skip to content

Commit 07a3784

Browse files
committed
Fix map_unwrap_or lint
``` cargo clippy --workspace --exclude bindgen-integration --exclude tests_expectations -- -W clippy::map_unwrap_or ```
1 parent 061cc5e commit 07a3784

File tree

5 files changed

+18
-28
lines changed

5 files changed

+18
-28
lines changed

bindgen/clang.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,10 +1112,8 @@ impl Iterator for ClangTokenIterator<'_> {
11121112
/// (including '_') and does not start with a digit.
11131113
pub(crate) fn is_valid_identifier(name: &str) -> bool {
11141114
let mut chars = name.chars();
1115-
let first_valid = chars
1116-
.next()
1117-
.map(|c| c.is_alphabetic() || c == '_')
1118-
.unwrap_or(false);
1115+
let first_valid =
1116+
chars.next().is_some_and(|c| c.is_alphabetic() || c == '_');
11191117

11201118
first_valid && chars.all(|c| c.is_alphanumeric() || c == '_')
11211119
}

bindgen/codegen/mod.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5145,14 +5145,10 @@ pub(crate) mod utils {
51455145
return Ok(());
51465146
}
51475147

5148-
let path = context
5149-
.options()
5150-
.wrap_static_fns_path
5151-
.as_ref()
5152-
.map(PathBuf::from)
5153-
.unwrap_or_else(|| {
5154-
std::env::temp_dir().join("bindgen").join("extern")
5155-
});
5148+
let path = context.options().wrap_static_fns_path.as_ref().map_or_else(
5149+
|| std::env::temp_dir().join("bindgen").join("extern"),
5150+
PathBuf::from,
5151+
);
51565152

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

bindgen/codegen/serialize.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ use super::{CodegenError, WrapAsVariadic};
1414

1515
fn get_loc(item: &Item) -> String {
1616
item.location()
17-
.map(|x| x.to_string())
18-
.unwrap_or_else(|| "unknown".to_owned())
17+
.map_or_else(|| "unknown".to_owned(), |x| x.to_string())
1918
}
2019

2120
pub(super) trait CSerialize<'a> {

bindgen/ir/item.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -786,16 +786,14 @@ impl Item {
786786

787787
match *self.kind() {
788788
ItemKind::Var(ref var) => var.name().to_owned(),
789-
ItemKind::Module(ref module) => {
790-
module.name().map(ToOwned::to_owned).unwrap_or_else(|| {
791-
format!("_bindgen_mod_{}", self.exposed_id(ctx))
792-
})
793-
}
794-
ItemKind::Type(ref ty) => {
795-
ty.sanitized_name(ctx).map(Into::into).unwrap_or_else(|| {
796-
format!("_bindgen_ty_{}", self.exposed_id(ctx))
797-
})
798-
}
789+
ItemKind::Module(ref module) => module.name().map_or_else(
790+
|| format!("_bindgen_mod_{}", self.exposed_id(ctx)),
791+
ToOwned::to_owned,
792+
),
793+
ItemKind::Type(ref ty) => ty.sanitized_name(ctx).map_or_else(
794+
|| format!("_bindgen_ty_{}", self.exposed_id(ctx)),
795+
Into::into,
796+
),
799797
ItemKind::Function(ref fun) => {
800798
let mut name = fun.name().to_owned();
801799

@@ -1702,8 +1700,7 @@ impl Item {
17021700
ty.spelling()
17031701
);
17041702
Item::type_param(Some(id), location, ctx)
1705-
.map(Ok)
1706-
.unwrap_or(Err(ParseError::Recurse))
1703+
.ok_or(ParseError::Recurse)
17071704
} else {
17081705
result
17091706
}

bindgen/options/cli.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ where
703703
}
704704

705705
fn add_derives(&self, info: &DeriveInfo<'_>) -> Vec<String> {
706-
if self.kind.map(|kind| kind == info.kind).unwrap_or(true) &&
706+
if self.kind.map_or(true, |kind| kind == info.kind) &&
707707
self.regex_set.matches(info.name)
708708
{
709709
return self.derives.clone();
@@ -743,7 +743,7 @@ where
743743
}
744744

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

0 commit comments

Comments
 (0)