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

Fix warnings for cloning references in generated code #1676

Conversation

alexander-jackson
Copy link
Contributor

Motivation

In #1674, it was pointed out that axum-macros causes some clippy warnings (and some rustc lints to fail) as the FromRef macro generates code which clones reference types unnecessarily.

Solution

This seems to be when generating the body of the trait implementation, but since we have the type information from syn at that point it's relatively simple to resolve these.

This change:

  • Avoids using .clone if we are generating the implementation for a reference type field
  • Adds a simple UI test to ensure this compiles with the rustc lint set to deny

Fixes #1674.

In tokio-rs#1674, it was pointed out that `axum-macros` causes some `clippy`
warnings (and some `rustc` lints to fail) as the `FromRef` macro
generates code which clones reference types unnecessarily.

This seems to be when generating the body of the trait implementation,
but since we have the type information from `syn` at that point it's
relatively simple to resolve these.

This change:
* Avoids using `.clone` if we are generating the implementation for a
  reference type field
* Adds a simple UI test to ensure this compiles with the `rustc` lint
  set to deny

Fixes tokio-rs#1674.
Copy link
Member

@davidpdrsn davidpdrsn left a comment

Choose a reason for hiding this comment

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

Thanks!

@davidpdrsn
Copy link
Member

Wanna add a note to changelog as well?

@davidpdrsn davidpdrsn changed the title fix: warnings for cloning references Fix warnings for cloning references in generated code Jan 5, 2023
@davidpdrsn davidpdrsn merged commit b6c282a into tokio-rs:main Jan 5, 2023
@alexander-jackson alexander-jackson deleted the fix-warnings-for-cloning-references branch January 5, 2023 17:50
@alexander-jackson
Copy link
Contributor Author

@davidpdrsn I don't mind, happy to add one if it's worth it 😅 not the most exciting of changes though, I presume rustc optimises away the .clone anyway at compile 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.

Rustc and clippy warnings generated by derive(FromRef)
2 participants