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 must_use attribute to Mock #135

Merged
merged 1 commit into from
Nov 29, 2023
Merged

Conversation

avandesa
Copy link
Contributor

Description of changes:

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], 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());
  |     +++++++

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

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
@avandesa
Copy link
Contributor Author

Guessing these are new lints, pretty sure they're not related to my change. Might be able to make a pr fixing them for you this evening if you want.

@LukeMathWalker LukeMathWalker merged commit 11af978 into LukeMathWalker:main Nov 29, 2023
2 of 5 checks passed
@LukeMathWalker
Copy link
Owner

That'd be nice if you have the time!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants