Skip to content

Commit 09f32b3

Browse files
nyurikpvdrz
authored andcommitted
Fix map_unwrap_or lint
``` cargo clippy --workspace --exclude bindgen-integration --exclude tests_expectations -- -W clippy::map_unwrap_or ```
1 parent 3c1a619 commit 09f32b3

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
@@ -1110,10 +1110,8 @@ impl Iterator for ClangTokenIterator<'_> {
11101110
/// (including '_') and does not start with a digit.
11111111
pub(crate) fn is_valid_identifier(name: &str) -> bool {
11121112
let mut chars = name.chars();
1113-
let first_valid = chars
1114-
.next()
1115-
.map(|c| c.is_alphabetic() || c == '_')
1116-
.unwrap_or(false);
1113+
let first_valid =
1114+
chars.next().is_some_and(|c| c.is_alphabetic() || c == '_');
11171115

11181116
first_valid && chars.all(|c| c.is_alphanumeric() || c == '_')
11191117
}

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
@@ -705,7 +705,7 @@ where
705705
}
706706

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

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

0 commit comments

Comments
 (0)