Skip to content

Rewrite your app in Rust, piece by piece, checked against the original at every step. Works with TypeScript, JavaScript, Python, Go, Ruby, Java, C, PHP, C#, Kotlin, PowerShell and Swift.

fix(ruby): import magnus Object and method only for batch crates (#1246)

Fabián Reyes8794d33a62f1
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5f46a0ae..bc204769 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,11 @@
## Unreleased
+- Ruby: a crate bridged without `--batch` passes `gate`'s clippy check. The generated `src/lib.rs`
+ imported magnus's `Object` and `method`, which only the batch classes use, so clippy with
+ `-D warnings` failed on `unused imports: Object and method` in generated code the port's author
+ cannot edit. They are now imported only when the crate has a batch class (found porting the
+ `text` gem's `soundex.rb` and `levenshtein.rb`, by @cfreyes7).
- Ruby: `bridge` no longer stops with "`soundex` is defined twice" on a module that declares
`module_function :soundex` and then `module_function :soundex_str`. A `module_function` call
now matches the symbols it names exactly, `?`, `!` and `=` suffixes and operator names
diff --git a/src/codegen_ruby.rs b/src/codegen_ruby.rs
index f27d839c..96ceab05 100644
--- a/src/codegen_ruby.rs
+++ b/src/codegen_ruby.rs
@@ -726,10 +726,17 @@ fn via_json(ty: &str) -> bool {
/// original defaults and keywords).
pub fn lib_rs(m: &Manifest, api: &crate::codegen::PortApi) -> Result<String, String> {
let lib = m.lib_name();
+ let (batches, batch_init) = crate::codegen_batch_native::ruby_rs(m, api)?;
+ // `Object` and `method!` serve only the batch classes: a crate without one failed clippy on them.
+ let magnus_use = if batches.is_empty() {
+ "Error, ExceptionClass, Module, Ruby, function, value::ReprValue"
+ } else {
+ "Error, ExceptionClass, Module, Object, Ruby, function, method, value::ReprValue"
+ };
let mut out = format!(
"//! Ruby extension for `{unit}`, generated by rust-anything from `src/port.rs`. Regenerate with\n\
//! `rust-anything refresh`. Values convert directly between Ruby and Rust: no JSON.\n\n\
- use magnus::{{Error, ExceptionClass, Module, Object, Ruby, function, method, value::ReprValue}};\n\
+ use magnus::{{{magnus_use}}};\n\
#[allow(unused_imports)]\nuse serde_json::Value;\n\n\
pub mod port;\nmod rbconv;\n\n\
/// Resolves a `::`-separated constant path like `\"Textkit::ParseError\"` to an `Exception` subclass.\n\
@@ -860,7 +867,6 @@ pub fn lib_rs(m: &Manifest, api: &crate::codegen::PortApi) -> Result<String, Str
f.params.len()
));
}
- let (batches, batch_init) = crate::codegen_batch_native::ruby_rs(m, api)?;
out.push_str(&batches);
let _ = writeln!(
out,
@@ -1085,6 +1091,13 @@ mod tests {
assert!(rs.contains("split(\"::\")"), "{rs}");
assert!(rs.contains("value::ReprValue"), "{rs}");
assert!(rs.contains("const_get\", (part, false)"), "{rs}");
+ // No batch class: `Object` and `method` would be unused imports and fail clippy.
+ assert!(
+ rs.contains(
+ "use magnus::{Error, ExceptionClass, Module, Ruby, function, value::ReprValue};"
+ ),
+ "{rs}"
+ );
}
#[test]