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

Warnings on missing .create() #113

Merged
merged 3 commits into from
Mar 15, 2020
Merged

Warnings on missing .create() #113

merged 3 commits into from
Mar 15, 2020

Conversation

dbr
Copy link
Contributor

@dbr dbr commented Mar 11, 2020

As discussed in #112

dbr added 2 commits March 11, 2020 12:03
This results in the Mock never being registered to the server, which is most likely a mistake
Warns about neglecting to assign the Mock to a variable as per docs
@dbr dbr changed the title Createwarn Warnings on missing .create() Mar 11, 2020
@lipanski lipanski merged commit 708360b into lipanski:master Mar 15, 2020
@lipanski
Copy link
Owner

@dbr looks great, thanks 🥇

@@ -1064,20 +1068,21 @@ impl Mock {
/// let _m = mock("GET", "/").with_body("hello world").create();
/// ```
///
pub fn create(self) -> Self {
#[must_use]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this does what you intend. This ensures someone who calls create() uses the result (Self), but doesn't require that create() itself is called. And since the intended pattern is to bind the Mock to an unused variable (let _m = mock(...);) that satisfies the must_use requirement, so normal usage won't see this.

For example:

mock("GET", "/").with_status(200); // No warning
mock("GET", "/").with_status(200).create(); // Warning: must_use
let _m = mock("GET", "/").with_status(200); // No warning
let _m = mock("GET", "/").with_status(200).create(); // No warning

Arguably all the builder methods on Mock should be #[must_use], but it still won't ensure that .create() is called.

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.

3 participants