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

Handle the #[inline], #[cold], and #[must_use] attributes #555

Merged
merged 2 commits into from
Mar 1, 2024
Merged

Conversation

asomers
Copy link
Owner

@asomers asomers commented Feb 24, 2024

Fixes #552

For methods, emit #[must_use] on the mock method definition, but nowhere
else.  For traits, ignore it.
@asomers
Copy link
Owner Author

asomers commented Feb 24, 2024

@tgnottingham does this branch work for you?

@tgnottingham
Copy link

tgnottingham commented Feb 25, 2024

@asomers -- yes, it works for my use case! Thanks for the quick fix!

This hasn't affected me yet in practice, but I noticed that there are still some warnings emitted for #[must_use] (regardless of whether or not there's a default method implementation):

#[mockall::automock]
trait Foo {
    #[must_use]
    fn foo(&self) -> i32;
}

fn main() {}

On Rust 1.76.0:

warning: `#[must_use]` has no effect when applied to a struct field
 --> src/main.rs:3:5
  |
3 |     #[must_use]
  |     ^^^^^^^^^^^
  |
  = note: `#[warn(unused_attributes)]` on by default

warning: `#[must_use]` has no effect when applied to a struct field
 --> src/main.rs:3:5
  |
3 |     #[must_use]
  |     ^^^^^^^^^^^

warning: `#[must_use]` has no effect when applied to an expression
 --> src/main.rs:3:5
  |
3 |     #[must_use]
  |     ^^^^^^^^^^^

warning: unused attribute
 --> src/main.rs:3:5
  |
3 |     #[must_use]
  |     ^^^^^^^^^^^ help: remove this attribute
  |
note: attribute also specified here
 --> src/main.rs:1:1
  |
1 | #[mockall::automock]
  | ^^^^^^^^^^^^^^^^^^^^
  = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
  = note: this warning originates in the attribute macro `mockall::automock` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: `#[must_use]` has no effect when applied to a module
 --> src/main.rs:3:5
  |
3 |     #[must_use]
  |     ^^^^^^^^^^^

Without the &self parameter on foo, only the no effect when applied to a module warning is generated.

@asomers
Copy link
Owner Author

asomers commented Feb 25, 2024

@tgnottingham I cannot reproduce those warnings. Are you sure that you're using the "inline" branch?

@tgnottingham
Copy link

My mistake, I was on the first commit to that branch. Everything works great on the second commit. Thanks again!

@asomers asomers merged commit d4477ad into master Mar 1, 2024
3 checks passed
@asomers asomers deleted the inline branch March 1, 2024 20:43
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.

Can't apply automock to traits with default method implementions marked #[inline]
2 participants