Skip to content

Commit

Permalink
Suppress "dead code" warnings when automocking a struct's private met…
Browse files Browse the repository at this point in the history
…hod.

It might be used only by other public methods in the same struct.
  • Loading branch information
asomers committed Jul 24, 2022
1 parent 4db3758 commit ac3ebec
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## Fixed

- Suppress "dead code" warnings when automocking a struct's private method. It
might be used only by other public methods in the same struct.
([#397](https://github.com/asomers/mockall/pull/397))

- Fixed using Mockall when a function named `Ok` is in scope. The `anyhow`
crate, for example, creates a function by this name.
([#389](https://github.com/asomers/mockall/pull/389))
Expand Down
2 changes: 0 additions & 2 deletions mockall/tests/automock_associated_const.rs
Expand Up @@ -32,15 +32,13 @@ trait Foo {
}
}

#[allow(dead_code)]
struct Bar {}

#[automock]
impl Foo for Bar {
const X: i32 = 42;
}

#[allow(dead_code)]
pub struct Baz {}
#[automock]
impl Baz {
Expand Down
3 changes: 1 addition & 2 deletions mockall/tests/automock_foreign_c.rs
Expand Up @@ -15,8 +15,7 @@ mod ffi {
}

// Ensure we can still use the original mocked function
#[allow(dead_code)]
fn normal_usage() {
pub fn normal_usage() {
unsafe {
ffi::foo(42);
}
Expand Down
1 change: 0 additions & 1 deletion mockall/tests/automock_instrument.rs
Expand Up @@ -2,7 +2,6 @@
//! A trait that uses tracing::instrument should be automockable. The mock
//! method won't be instrumented, though.
#![deny(warnings)]
#![allow(dead_code)]

use mockall::*;
use tracing::instrument;
Expand Down
22 changes: 22 additions & 0 deletions mockall/tests/automock_nonpub_methods.rs
@@ -0,0 +1,22 @@
// vim: tw=80
//! Using automock on a struct with private methods should not result in any
//! "dead_code" warnings. Such methods are often private helpers used only by
//! other public methods.
#[deny(dead_code)]

pub mod mymod {
use mockall::automock;

pub struct Foo{}

#[automock]
impl Foo {
fn private_helper(&self) {}
fn static_private_helper() {}

pub fn pubfunc(&self) {
Self::static_private_helper();
self.private_helper()
}
}
}
1 change: 0 additions & 1 deletion mockall/tests/mock_cfg.rs
@@ -1,7 +1,6 @@
// vim: tw=80
//! mock's methods and trait impls can be conditionally compiled
#![deny(warnings)]
#![allow(dead_code)]

use mockall::*;

Expand Down
1 change: 0 additions & 1 deletion mockall/tests/mock_docs.rs
@@ -1,7 +1,6 @@
// vim: tw=80
#![deny(missing_docs)]
#![deny(warnings)]
#![allow(dead_code)]

use mockall::*;

Expand Down
Expand Up @@ -10,7 +10,6 @@ mock! {
fn foo<Q: 'static>(t: T, q: Q) -> u64;
// We must use a different method for every should_panic test, so the
// shared mutex doesn't get poisoned.
#[allow(dead_code)]
fn foo2<Q: 'static>(t: T, q: Q) -> u64;
fn foo3<Q: 'static>(t: T, q: Q) -> u64;
}
Expand Down
1 change: 0 additions & 1 deletion mockall/tests/mock_life0.rs
Expand Up @@ -2,7 +2,6 @@
//! mock a method whose self parameter has an explicit lifetime
//! https://github.com/asomers/mockall/issues/95
#![deny(warnings)]
#![allow(dead_code)]

use mockall::*;

Expand Down
1 change: 0 additions & 1 deletion mockall/tests/mock_nonpub.rs
Expand Up @@ -2,7 +2,6 @@
//! methods can use non-public types, as long as the object's visibility is
//! compatible.
#![deny(warnings)]
#![allow(dead_code)]

use mockall::*;

Expand Down
1 change: 0 additions & 1 deletion mockall/tests/mock_struct_with_static_method.rs
@@ -1,6 +1,5 @@
// vim: tw=80
#![deny(warnings)]
#![allow(dead_code)]

use mockall::*;
use std::sync::Mutex;
Expand Down
17 changes: 14 additions & 3 deletions mockall_derive/src/mock_function.rs
Expand Up @@ -448,10 +448,19 @@ impl MockFunction {
let no_match_msg = quote!(std::format!(
"{}: No matching expectation found", #desc));
let sig = &self.sig;
let vis = if self.trait_.is_some() {
&Visibility::Inherited
let (vis, dead_code) = if self.trait_.is_some() {
(&Visibility::Inherited, quote!())
} else {
&self.call_vis
let dead_code = if let Visibility::Inherited = self.call_vis {
// This private method may be a helper only used by the struct's
// other methods, which we are mocking. If so, the mock method
// will be dead code. But we can't simply eliminate it, because
// it might also be used by other code in the same module.
quote!(#[allow(dead_code)])
} else {
quote!()
};
(&self.call_vis, dead_code)
};
let substruct_obj = if let Some(trait_) = &self.trait_ {
let ident = format_ident!("{}_expectations", trait_);
Expand All @@ -477,6 +486,7 @@ impl MockFunction {
quote!(
// Don't add a doc string. The original is included in #attrs
#(#attrs)*
#dead_code
#vis #sig {
let no_match_msg = #no_match_msg;
#deref {
Expand All @@ -497,6 +507,7 @@ impl MockFunction {
quote!(
// Don't add a doc string. The original is included in #attrs
#(#attrs)*
#dead_code
#vis #sig {
let no_match_msg = #no_match_msg;
#deref self.#substruct_obj #name.#call#tbf(#(#call_exprs,)*)
Expand Down

0 comments on commit ac3ebec

Please sign in to comment.