From 3932c5ee5db23615b69d9b38e221b36b04d6dd1f Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Thu, 28 Mar 2019 16:15:42 -0600 Subject: [PATCH] Ensure imports used in job bodies are considered used If a trait is imported, but only used in the job body (which is in its own module and only in scope because of `use super::*;`), the compiler warns that the import is unused. This might be a bug in the compiler, but we can work around it by moving the generated `Job` impl out of the generated module. Fixes #6 --- integration_tests/tests/codegen.rs | 22 ++++++++++++++++++++++ swirl_proc_macro/src/background_job.rs | 20 ++++++++++---------- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/integration_tests/tests/codegen.rs b/integration_tests/tests/codegen.rs index 22b62f6..19c40ea 100644 --- a/integration_tests/tests/codegen.rs +++ b/integration_tests/tests/codegen.rs @@ -60,3 +60,25 @@ fn env_can_have_any_name() { runner.run_all_pending_jobs().unwrap(); runner.assert_no_failed_jobs().unwrap(); } + +#[test] +#[forbid(unused_imports)] +fn test_imports_only_used_in_job_body_are_not_warned_as_unused() { + use std::io::prelude::*; + + #[swirl::background_job] + fn uses_trait_import() -> Result<(), swirl::PerformError> { + let mut buf = Vec::new(); + buf.write_all(b"foo")?; + let s = String::from_utf8(buf)?; + assert_eq!(s, "foo"); + Ok(()) + } + + let runner = TestGuard::dummy_runner(); + let conn = runner.connection_pool().get().unwrap(); + uses_trait_import().enqueue(&conn).unwrap(); + + runner.run_all_pending_jobs().unwrap(); + runner.assert_no_failed_jobs().unwrap(); +} diff --git a/swirl_proc_macro/src/background_job.rs b/swirl_proc_macro/src/background_job.rs index 3c05c01..4470cfa 100644 --- a/swirl_proc_macro/src/background_job.rs +++ b/swirl_proc_macro/src/background_job.rs @@ -29,6 +29,16 @@ pub fn expand(item: syn::ItemFn) -> Result { } } + impl swirl::Job for Job { + type Environment = #env_type; + const JOB_TYPE: &'static str = stringify!(#name); + + #fn_token perform(self, #env_pat: &Self::Environment) #return_type { + let Job { #(#arg_names),* } = self; + #(#body)* + } + } + mod #name { use super::*; @@ -38,16 +48,6 @@ pub fn expand(item: syn::ItemFn) -> Result { #(#struct_def),* } - impl swirl::Job for Job { - type Environment = #env_type; - const JOB_TYPE: &'static str = stringify!(#name); - - #fn_token perform(self, #env_pat: &Self::Environment) #return_type { - let Job { #(#arg_names),* } = self; - #(#body)* - } - } - swirl::register_job!(Job); } };