Skip to content

codegen: address style audit findings — clones, wildcards, unwrap#121

Draft
iainmcgin wants to merge 1 commit into
mainfrom
rust-style-audit-fixes
Draft

codegen: address style audit findings — clones, wildcards, unwrap#121
iainmcgin wants to merge 1 commit into
mainfrom
rust-style-audit-fixes

Conversation

@iainmcgin
Copy link
Copy Markdown
Collaborator

Three changes from a code-style audit of hand-written sources, focused on iteration/ownership, error handling, and dispatch structure.

Changes

protoc-gen-buffa/src/main.rs — build the CodeGeneratorResponse file list by consuming generated with .into_iter() instead of cloning every name and content string. The vector is the sole owner and is not used afterward, so the clones were pure overhead on every plugin invocation (2N string clones per protoc run).

buffa-codegen/src/oneof.rs::to_snake_case — replace c.to_lowercase().next().unwrap() with result.extend(c.to_lowercase()). Removes the unwrap and correctly preserves multi-codepoint lowercase mappings (e.g. İi\u{307}) instead of silently truncating them. Behavior is unchanged for ASCII, which is the only thing protobuf identifiers contain.

buffa-codegen/src/{impl_message,impl_text,view}.rs — replace nine _ => unreachable!() arms over the protobuf Type enum with explicit enumeration of the unhandled variants. Each match is now exhaustive over Type's 18 variants. A future protobuf edition that adds a Type variant will produce a compile error in buffa-codegen rather than a runtime panic during codegen, which is consistent with how wire_type_byte and wire_type_token already fail. Two of the replaced arms also had no diagnostic message at all (view.rs:1021, impl_text.rs:716).

Notes

No change to generated output — the unreachable!() arms never produce tokens. Verified with task gen-wkt-types (no drift).

task lint and task test both pass.

Three changes from a code-style audit of hand-written sources:

- `protoc-gen-buffa/src/main.rs`: build the `CodeGeneratorResponse` file
  list by consuming `generated` with `.into_iter()` instead of cloning
  every name and content string. The vector is the sole owner and is
  not used afterward, so the clones were pure overhead on every plugin
  invocation.

- `buffa-codegen/src/oneof.rs::to_snake_case`: replace
  `c.to_lowercase().next().unwrap()` with `result.extend(c.to_lowercase())`.
  Removes the unwrap and correctly preserves multi-codepoint lowercase
  mappings instead of silently truncating them. Behavior is unchanged
  for ASCII (the only thing protobuf identifiers contain).

- `buffa-codegen/src/{impl_message,impl_text,view}.rs`: replace nine
  `_ => unreachable!()` arms over the protobuf `Type` enum with explicit
  enumeration of the unhandled variants. Each match is now exhaustive,
  so a future protobuf edition adding a `Type` variant produces a
  compile error in codegen rather than a runtime panic. Two of the
  replaced arms also had no diagnostic message at all.

No change to generated output — the unreachable arms never produce
tokens. Verified with `task gen-wkt-types` (no drift).
@github-actions
Copy link
Copy Markdown

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant