Repository: rust-lang/rust-bindgen
Pull request #3100 (2025-01-27 09:55:42)

Mangle identifiers named 'gen' now that it's a reserved keyword
This addresses https://github.com/rust-lang/rust-bindgen/issues/3093



Patch:
diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml
index 7333d62a..d8602f9a 100644
--- a/.github/workflows/publish.yml
+++ b/.github/workflows/publish.yml
@@ -11,7 +11,7 @@ env:
 jobs:
   cargo-publish:
     runs-on: ubuntu-latest
-    if: ${{ (github.event.workflow_run.conclusion == 'success') && (github.event.workflow.name == 'Release') }}
+    if: ${{ !github.event.pull_request && (github.event.workflow_run.conclusion == 'success') && (github.event.workflow.name == 'Release') }}
     steps:
       - name: Print workflow event name
         run: echo "${{ github.event.workflow.name }}"
diff --git a/Cargo.toml b/Cargo.toml
index 9537f4a1..6978f49e 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -57,10 +57,8 @@ cast_possible_truncation = "allow"
 cast_possible_wrap = "allow"
 cast_precision_loss = "allow"
 cast_sign_loss = "allow"
-checked_conversions = "allow"
 default_trait_access = "allow"
 explicit_into_iter_loop = "allow"
-flat_map_option = "allow"
 ignored_unit_patterns = "allow"
 implicit_hasher = "allow"
 items_after_statements = "allow"
diff --git a/bindgen/codegen/mod.rs b/bindgen/codegen/mod.rs
index 157478d3..ccf63253 100644
--- a/bindgen/codegen/mod.rs
+++ b/bindgen/codegen/mod.rs
@@ -2630,12 +2630,9 @@ impl CodeGenerator for CompInfo {
                     } else {
                         self.fields()
                             .iter()
-                            .filter_map(|field| match *field {
-                                Field::DataMember(ref f) if f.name().is_some() => Some(f),
-                                _ => None,
-                            })
-                            .flat_map(|field| {
-                                let name = field.name().unwrap();
+                            .filter_map(|field| {
+                                let Field::DataMember(field) = field else { return None };
+                                let name = field.name()?;
                                 field.offset().map(|offset| {
                                     let field_offset = offset / 8;
                                     let field_name = ctx.rust_ident(name);
@@ -2704,7 +2701,7 @@ impl CodeGenerator for CompInfo {
             let mut method_names = Default::default();
             if ctx.options().codegen_config.methods() {
                 for method in self.methods() {
-                    assert!(method.kind() != MethodKind::Constructor);
+                    assert_ne!(method.kind(), MethodKind::Constructor);
                     method.codegen_method(
                         ctx,
                         &mut methods,
diff --git a/bindgen/ir/context.rs b/bindgen/ir/context.rs
index 6b47560e..c6bc9025 100644
--- a/bindgen/ir/context.rs
+++ b/bindgen/ir/context.rs
@@ -782,7 +782,7 @@ If you encounter an error missing from this list, please file an issue or a PR!"
     /// codegen'd, even if its parent is not allowlisted. See issue #769 for
     /// details.
     fn add_item_to_module(&mut self, item: &Item) {
-        assert!(item.id() != self.root_module);
+        assert_ne!(item.id(), self.root_module);
         assert!(self.resolve_item_fallible(item.id()).is_none());
 
         if let Some(ref mut parent) = self.items[item.parent_id().0] {
@@ -804,7 +804,7 @@ If you encounter an error missing from this list, please file an issue or a PR!"
             self.current_module
         );
 
-        self.items[(self.current_module.0).0]
+        self.items[self.current_module.0 .0]
             .as_mut()
             .expect("Should always have an item for self.current_module")
             .as_module_mut()
@@ -867,7 +867,7 @@ If you encounter an error missing from this list, please file an issue or a PR!"
                 "abstract" | "alignof" | "as" | "async" | "await" | "become" |
                     "box" | "break" | "const" | "continue" | "crate" | "do" |
                     "dyn" | "else" | "enum" | "extern" | "false" | "final" |
-                    "fn" | "for" | "if" | "impl" | "in" | "let" | "loop" |
+                    "fn" | "for" | "gen" | "if" | "impl" | "in" | "let" | "loop" |
                     "macro" | "match" | "mod" | "move" | "mut" | "offsetof" |
                     "override" | "priv" | "proc" | "pub" | "pure" | "ref" |
                     "return" | "Self" | "self" | "sizeof" | "static" |
@@ -1232,7 +1232,7 @@ If you encounter an error missing from this list, please file an issue or a PR!"
         &self,
     ) -> traversal::AssertNoDanglingItemsTraversal {
         assert!(self.in_codegen_phase());
-        assert!(self.current_module == self.root_module);
+        assert_eq!(self.current_module, self.root_module);
 
         let roots = self.items().map(|(id, _)| id);
         traversal::AssertNoDanglingItemsTraversal::new(
@@ -1248,7 +1248,7 @@ If you encounter an error missing from this list, please file an issue or a PR!"
     fn assert_every_item_in_a_module(&self) {
         if cfg!(feature = "__testing_only_extra_assertions") {
             assert!(self.in_codegen_phase());
-            assert!(self.current_module == self.root_module);
+            assert_eq!(self.current_module, self.root_module);
 
             for (id, _item) in self.items() {
                 if id == self.root_module {
@@ -2346,7 +2346,7 @@ If you encounter an error missing from this list, please file an issue or a PR!"
     /// allowlisted.
     pub(crate) fn allowlisted_items(&self) -> &ItemSet {
         assert!(self.in_codegen_phase());
-        assert!(self.current_module == self.root_module);
+        assert_eq!(self.current_module, self.root_module);
 
         self.allowlisted.as_ref().unwrap()
     }
@@ -2359,7 +2359,7 @@ If you encounter an error missing from this list, please file an issue or a PR!"
         derive_trait: DeriveTrait,
     ) -> CanDerive {
         assert!(self.in_codegen_phase());
-        assert!(self.current_module == self.root_module);
+        assert_eq!(self.current_module, self.root_module);
 
         *self
             .blocklisted_types_implement_traits
@@ -2407,14 +2407,14 @@ If you encounter an error missing from this list, please file an issue or a PR!"
     /// Get a reference to the set of items we should generate.
     pub(crate) fn codegen_items(&self) -> &ItemSet {
         assert!(self.in_codegen_phase());
-        assert!(self.current_module == self.root_module);
+        assert_eq!(self.current_module, self.root_module);
         self.codegen_items.as_ref().unwrap()
     }
 
     /// Compute the allowlisted items set and populate `self.allowlisted`.
     fn compute_allowlisted_and_codegen_items(&mut self) {
         assert!(self.in_codegen_phase());
-        assert!(self.current_module == self.root_module);
+        assert_eq!(self.current_module, self.root_module);
         assert!(self.allowlisted.is_none());
         let _t = self.timer("compute_allowlisted_and_codegen_items");
 
diff --git a/bindgen/ir/objc.rs b/bindgen/ir/objc.rs
index d413d6bb..6cdadb13 100644
--- a/bindgen/ir/objc.rs
+++ b/bindgen/ir/objc.rs
@@ -294,10 +294,7 @@ impl ObjCMethod {
         }
 
         // Check right amount of arguments
-        assert!(
-            args.len() == split_name.len() - 1,
-            "Incorrect method name or arguments for objc method, {args:?} vs {split_name:?}"
-        );
+        assert_eq!(args.len(), split_name.len() - 1, "Incorrect method name or arguments for objc method, {args:?} vs {split_name:?}");
 
         // Get arguments without type signatures to pass to `msg_send!`
         let mut args_without_types = vec![];
diff --git a/bindgen/ir/var.rs b/bindgen/ir/var.rs
index 4f61448f..85e127fd 100644
--- a/bindgen/ir/var.rs
+++ b/bindgen/ir/var.rs
@@ -234,10 +234,7 @@ impl ClangSubItemParser for Var {
                                 assert_eq!(c.len_utf8(), 1);
                                 c as u8
                             }
-                            CChar::Raw(c) => {
-                                assert!(c <= u64::from(u8::MAX));
-                                c as u8
-                            }
+                            CChar::Raw(c) => u8::try_from(c).unwrap(),
                         };
 
                         (TypeKind::Int(IntKind::U8), VarType::Char(c))
diff --git a/bindgen/lib.rs b/bindgen/lib.rs
index 2e568560..94c6246e 100644
--- a/bindgen/lib.rs
+++ b/bindgen/lib.rs
@@ -1144,8 +1144,9 @@ fn parse(context: &mut BindgenContext) -> Result<(), BindgenError> {
         cursor.visit_sorted(ctx, |ctx, child| parse_one(ctx, child, None));
     });
 
-    assert!(
-        context.current_module() == context.root_module(),
+    assert_eq!(
+        context.current_module(),
+        context.root_module(),
         "How did this happen?"
     );
     Ok(())


Test patch:
diff --git a/bindgen-tests/tests/expectations/tests/keywords.rs b/bindgen-tests/tests/expectations/tests/keywords.rs
index ec56bbfa..0a55ee04 100644
--- a/bindgen-tests/tests/expectations/tests/keywords.rs
+++ b/bindgen-tests/tests/expectations/tests/keywords.rs
@@ -87,6 +87,10 @@ unsafe extern "C" {
     #[link_name = "\u{1}fn"]
     pub static mut fn_: ::std::os::raw::c_int;
 }
+unsafe extern "C" {
+    #[link_name = "\u{1}gen"]
+    pub static mut gen_: ::std::os::raw::c_int;
+}
 unsafe extern "C" {
     #[link_name = "\u{1}impl"]
     pub static mut impl_: ::std::os::raw::c_int;
diff --git a/bindgen-tests/tests/headers/keywords.h b/bindgen-tests/tests/headers/keywords.h
index 3b3fc497..49924193 100644
--- a/bindgen-tests/tests/headers/keywords.h
+++ b/bindgen-tests/tests/headers/keywords.h
@@ -21,6 +21,7 @@ int box;
 int crate;
 int false;
 int fn;
+int gen;
 int impl;
 int in;
 int let;