Skip to content

Commit

Permalink
Add some must_use warnings
Browse files Browse the repository at this point in the history
There's no good reason to create a Context object but not set any
expectations.  And when the "nightly" feature is off, there's no good
reason to create an expectation but not set its return value.  Warn the
user if he does one of those things.

Suggested by @ArekPiekarz
  • Loading branch information
asomers committed Aug 29, 2019
1 parent 8e5568d commit e6c959d
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
## [Unreleased] - ReleaseDate
### Added

- Warnings for misued expectations and context objects
([#37](https://github.com/asomers/mockall/pull/37))

- Methods with closure arguments and where clauses can now be mocked.
([#35](https://github.com/asomers/mockall/pull/35))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ fn ctx_hygiene() {
}

#[cfg_attr(not(feature = "nightly"), ignore)]
#[cfg_attr(not(feature = "nightly"), allow(unused_must_use))]
#[test]
fn return_default() {
let _m = BAR_MTX.lock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mock! {
#[should_panic(expected =
"Can only return default values for types that impl std::Default")]
#[cfg_attr(not(feature = "nightly"), ignore)]
#[cfg_attr(not(feature = "nightly"), allow(unused_must_use))]
fn return_default() {
let mut mock = MockExternalStruct::<NonDefault>::new();
mock.expect_foo();
Expand Down
1 change: 1 addition & 0 deletions mockall/tests/mock_return_mutable_reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mock! {
#[test]
#[cfg_attr(not(feature = "nightly"),
should_panic(expected = "Returning default values requires"))]
#[cfg_attr(not(feature = "nightly"), allow(unused_must_use))]
fn return_default() {
let mut mock = MockFoo::new();
mock.expect_foo();
Expand Down
1 change: 1 addition & 0 deletions mockall/tests/mock_return_reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ fn return_const() {
#[test]
#[cfg_attr(not(feature = "nightly"),
should_panic(expected = "Returning default values requires"))]
#[cfg_attr(not(feature = "nightly"), allow(unused_must_use))]
fn return_default() {
let mut mock = MockFoo::new();
mock.expect_foo();
Expand Down
1 change: 1 addition & 0 deletions mockall/tests/mock_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ fn return_const() {

#[cfg_attr(not(feature = "nightly"),
should_panic(expected = "Returning default values requires"))]
#[cfg_attr(not(feature = "nightly"), allow(unused_must_use))]
#[test]
fn return_default() {
let mut mock = MockFoo::new();
Expand Down
1 change: 1 addition & 0 deletions mockall/tests/mock_struct_with_static_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ fn ctx_hygiene() {
}

#[cfg_attr(not(feature = "nightly"), ignore)]
#[cfg_attr(not(feature = "nightly"), allow(unused_must_use))]
#[test]
fn return_default() {
let _m = BAR_MTX.lock();
Expand Down
4 changes: 4 additions & 0 deletions mockall_derive/src/expectation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,7 @@ impl<'a> StaticExpectation<'a> {
};

let context_ts = quote!(
#[must_use = "Context only serves to create expectations" ]
#v struct Context #s_ig #s_wc {
// Prevent "unused type parameter" errors
// Surprisingly, PhantomData<Fn(generics)> is Send even if
Expand All @@ -970,6 +971,9 @@ impl<'a> StaticExpectation<'a> {
.collect::<Vec<_>>();
}

#[cfg_attr(not(feature = "nightly"), must_use =
"Must set return value when not using the \"nightly\" feature")
]
#v fn expect #meth_ig ( &self,) -> ExpectationGuard #e_tg
#meth_wc
{
Expand Down
13 changes: 9 additions & 4 deletions mockall_derive/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,12 +305,17 @@ fn gen_mock_method(mod_ident: Option<&syn::Ident>,
#[cfg(any(test, not(feature = "extra-docs")))]
let docstr: Option<syn::Attribute> = None;
let expect_ident = format_ident!("expect_{}", ident);
quote!(#attrs #docstr #expect_vis fn #expect_ident #ig(&mut self)
quote!(
#[cfg_attr(not(feature = "nightly"), must_use =
"Must set return value when not using the \"nightly\" feature")
]
#attrs #docstr #expect_vis fn #expect_ident #ig(&mut self)
-> &mut #mod_ident::#expectation
#wc
{
#expect_obj_name.expect#call_turbofish()
})
{
#expect_obj_name.expect#call_turbofish()
}
)
}.to_tokens(&mut expect_output);

// Finally this method's contribution to the checkpoint method
Expand Down

0 comments on commit e6c959d

Please sign in to comment.