Skip to content

Commit

Permalink
Add must_use attribute to Mock (#135)
Browse files Browse the repository at this point in the history
It's easy to forget to mount a mock to a server, even though the docs call this out. This commit tags the `Mock` struct with [`#[must_use]`][reference], so that Rust itself can warn users when they forget to mount their mocks. The help string

Here's the warning emitted:
```
warning: unused `Mock` that must be used
 --> examples\must_use.rs:9:5
  |
9 |     Mock::given(method("GET")).respond_with(response.clone());
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `Mock`s have to be mounted or registered with a `MockServer` to become effective
  = note: `#[warn(unused_must_use)]` on by default
help: use `let _ = ...` to ignore the resulting value
  |
9 |     let _ = Mock::given(method("GET")).respond_with(response.clone());
  |     +++++++
```

[reference]: https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-must_use-attribute
  • Loading branch information
avandesa authored Nov 29, 2023
1 parent 9461acb commit 11af978
Showing 1 changed file with 1 addition and 0 deletions.
1 change: 1 addition & 0 deletions src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ impl Debug for Matcher {
/// [`register`]: MockServer::register
/// [`mount`]: Mock::mount
/// [`mount_as_scoped`]: Mock::mount_as_scoped
#[must_use = "`Mock`s have to be mounted or registered with a `MockServer` to become effective"]
pub struct Mock {
pub(crate) matchers: Vec<Matcher>,
pub(crate) response: Box<dyn Respond>,
Expand Down

0 comments on commit 11af978

Please sign in to comment.