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

Custom lint for Mockall static expectations #49

Open
asomers opened this issue Dec 13, 2023 · 0 comments
Open

Custom lint for Mockall static expectations #49

asomers opened this issue Dec 13, 2023 · 0 comments
Labels
A-user-story Area: A user story or a related issue

Comments

@asomers
Copy link

asomers commented Dec 13, 2023

Lint explanation

When mocking a static method with Mockall, it's necessary to create a context object 1. Normally, the context object should live for the entire duration of the test case. But it's easy for users to forget, in which case they'll get confusing errors at runtime. The easiest way to forget is when users use method chaining inappropriately.

Example code

The lint should flag code like this:

MockA::foo_context().expect().returning(|| 99);

And suggest instead writing it like this:

let ctx = MockA::foo_context();
ctx.expect().returning(|| 99);

Notes

Possibly the easiest way to implement this would be to detect when a user uses a *_context() method without a let binding. And since the *_context() methods are autogenerated, Mockall can emit any attributes needed to guide the linter.

Footnotes

  1. https://docs.rs/mockall/latest/mockall/#static-methods

@asomers asomers added the A-user-story Area: A user story or a related issue label Dec 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-user-story Area: A user story or a related issue
Projects
None yet
Development

No branches or pull requests

1 participant