Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some must_use warnings #37

Merged
merged 1 commit into from
Aug 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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