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

attributes: fix #[instrument(err)] with mutable parameters #1167

Merged
merged 6 commits into from Feb 4, 2021

Conversation

okready
Copy link
Contributor

@okready okready commented Jan 1, 2021

Motivation

The closure generated by #[instrument(err)] for non-async functions is
not mutable, preventing any captured variables from being mutated. If a
function has any mutable parameters that are modified within the
function, adding the #[instrument(err)] attribute will result in a
compiler error.

Solution

This change makes it so the closure is executed directly as a temporary
variable instead of storing it in a named variable prior to its use,
avoiding the need to explicitly declare it as mutable altogether or to
add an #[allow(unused_mut)] annotation on the closure declaration to
silence lint warnings for closures that do not need to be mutable.

## Motivation

The closure generated by `#[instrument(err)]` for non-async functions is
not mutable, preventing any captured variables from being mutated. If a
function has any mutable parameters that are modified within the
function, adding the `#[instrument(err)]` attribute will result in a
compiler error.

## Solution

This change makes it so the closure is executed directly as a temporary
variable instead of storing it in a named variable prior to its use,
avoiding the need to explicitly declare it as mutable altogether or to
add an `#[allow(unused_mut)]` annotation on the closure declaration to
silence lint warnings for closures that do not need to be mutable.
@okready okready requested review from hawkw and a team as code owners January 1, 2021 00:03
Copy link
Member

@hawkw hawkw left a comment

Choose a reason for hiding this comment

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

this looks good to me, thanks for fixing this!

tracing-attributes/src/lib.rs Outdated Show resolved Hide resolved
tracing-attributes/src/lib.rs Outdated Show resolved Hide resolved
hawkw and others added 3 commits February 2, 2021 16:19
The clippy::unit_arg warning is triggered for the Ok branch in the match
statements generated for the #[instrument(err)] attribute against the
function result if that function returns a unit Ok value. This commit
disables the unit_arg lint check for the Ok match arms and cleans up
other attempts to isolate the lint error.
@hawkw
Copy link
Member

hawkw commented Feb 4, 2021

okay i've spent like an hour trying to figure out the Clippy lint and i just do not get it --- as far as i can tell, the pattern the lint fires on no longer appears anywhere in the cargo expand output, and yet it still triggers the lint. maybe @Manishearth can figure this out.

@okready
Copy link
Contributor Author

okready commented Feb 4, 2021

Thanks @hawkw for looking into the issue. Sorry for not catching it originally!

I've managed to track down the clippy::unit_arg cause to the Ok match arms on the function's result value. It seems that if the result has a unit Ok value, Clippy will complain in the match statement because (in most cases) you don't need to specify a named variable for the unit value (e.g. Ok(x)). I have a fix that should be ready to push shortly.

@hawkw
Copy link
Member

hawkw commented Feb 4, 2021

ah, okay, looks like it's just a clippy bug: rust-lang/rust-clippy#6447 (which looks like it trips on proc-macros a lot, see also rwf2/Rocket#1495). let's just add an allow attribute.

@hawkw
Copy link
Member

hawkw commented Feb 4, 2021

Thanks @hawkw for looking into the issue. Sorry for not catching it originally!

I've managed to track down the clippy::unit_arg cause to the Ok match arms on the function's result value. It seems that if the result has a unit Ok value, Clippy will complain in the match statement because (in most cases) you don't need to specify a named variable for the unit value (e.g. Ok(x)). I have a fix that should be ready to push shortly.

great, i'm glad you figured it out --- if you have a fix, that's fantastic. otherwise, i think it would be fine to just add an allow attribute.

@okready
Copy link
Contributor Author

okready commented Feb 4, 2021

ah, okay, looks like it's just a clippy bug: rust-lang/rust-clippy#6447 (which looks like it trips on proc-macros a lot, see also SergioBenitez/Rocket#1495). let's just add an allow attribute.

I think in this case the lint may technically be correct, as the generated code for the tests is explicitly matching against a Result<(), E>, which Clippy can see is always a unit value in the Ok arms, so the attribute may still be necessary even after the bug is fixed (outside of generating different Ok match arm code for unit types).

@hawkw
Copy link
Member

hawkw commented Feb 4, 2021

ah, okay, looks like it's just a clippy bug: rust-lang/rust-clippy#6447 (which looks like it trips on proc-macros a lot, see also SergioBenitez/Rocket#1495). let's just add an allow attribute.

I think in this case the lint may technically be correct, as the generated code for the tests is explicitly matching against a Result<(), E>, which Clippy can see is always a unit value in the Ok arms, so the attribute may still be necessary even after the bug is fixed (outside of generating different Ok match arm code for unit types).

yeah, that makes sense. it looks like you've pushed the fix for this, thanks so much for figuring it out! i'm going to go ahead and merge this now.

@hawkw hawkw merged commit c0939c3 into tokio-rs:master Feb 4, 2021
hawkw pushed a commit that referenced this pull request Feb 4, 2021
Motivation
----------

The closure generated by `#[instrument(err)]` for non-async functions is
not mutable, preventing any captured variables from being mutated. If a
function has any mutable parameters that are modified within the
function, adding the `#[instrument(err)]` attribute will result in a
compiler error.

Solution
--------

This change makes it so the closure is executed directly as a temporary
variable instead of storing it in a named variable prior to its use,
avoiding the need to explicitly declare it as mutable altogether or to
add an `#[allow(unused_mut)]` annotation on the closure declaration to
silence lint warnings for closures that do not need to be mutable.
@hawkw hawkw mentioned this pull request Feb 4, 2021
hawkw pushed a commit that referenced this pull request Feb 4, 2021
Motivation
----------

The closure generated by `#[instrument(err)]` for non-async functions is
not mutable, preventing any captured variables from being mutated. If a
function has any mutable parameters that are modified within the
function, adding the `#[instrument(err)]` attribute will result in a
compiler error.

Solution
--------

This change makes it so the closure is executed directly as a temporary
variable instead of storing it in a named variable prior to its use,
avoiding the need to explicitly declare it as mutable altogether or to
add an `#[allow(unused_mut)]` annotation on the closure declaration to
silence lint warnings for closures that do not need to be mutable.
hawkw added a commit that referenced this pull request Feb 4, 2021
Fixed

- Compiler error when using `#[instrument(err)]` on functions with
  mutable parameters ([#1167])
- Missing function visibility modifier when using `#[instrument]` with
  `async-trait` ([#977])
- Multiple documentation fixes and improvements ([#965], [#981],
  [#1215])

 Changed

- `tracing-futures` dependency is no longer required when using
  `#[instrument]` on async functions ([#808])

Thanks to @nagisa, @Txuritan, @TaKO8Ki, and @okready for contributing to
this release!

[#1167]: #1167
[#977]: #977
[#965]: #965
[#981]: #981
[#1215]: #1215
[#808]: #808
hawkw added a commit that referenced this pull request Feb 4, 2021
Fixed

- Compiler error when using `#[instrument(err)]` on functions with
  mutable parameters ([#1167])
- Missing function visibility modifier when using `#[instrument]` with
  `async-trait` ([#977])
- Multiple documentation fixes and improvements ([#965], [#981],
  [#1215])

 Changed

- `tracing-futures` dependency is no longer required when using
  `#[instrument]` on async functions ([#808])

Thanks to @nagisa, @Txuritan, @TaKO8Ki, and @okready for contributing to
this release!

[#1167]: #1167
[#977]: #977
[#965]: #965
[#981]: #981
[#1215]: #1215
[#808]: #808
hawkw added a commit that referenced this pull request Feb 4, 2021
### Fixed

- Compiler error when using `#[instrument(err)]` on functions with
  mutable parameters ([#1167])
- Missing function visibility modifier when using `#[instrument]` with
  `async-trait` ([#977])
- Multiple documentation fixes and improvements ([#965], [#981],
  [#1215])

 ### Changed

- `tracing-futures` dependency is no longer required when using
  `#[instrument]` on async functions ([#808])

Thanks to @nagisa, @Txuritan, @TaKO8Ki, and @okready for contributing to
this release!

[#1167]: #1167
[#977]: #977
[#965]: #965
[#981]: #981
[#1215]: #1215
[#808]: #808
hawkw added a commit that referenced this pull request Feb 4, 2021
Fixed

- **attributes**: Compiler error when using `#[instrument(err)]` on
  functions with mutable parameters ([#1167])
- **attributes**: Missing function visibility modifier when using
  `#[instrument]` with `async-trait` ([#977])
- **attributes** Removed unused `syn` features ([#928])
- **log**: Fixed an issue where the `tracing` macros would generate code
  for events whose levels are disabled statically by the `log` crate's
  `static_max_level_XXX` features ([#1175])
- Fixed deprecations and clippy lints ([#1195])
- Several documentation fixes and improvements ([#941], [#965], [#981],
  [#1146], [#1215])

Changed

- **attributes**: `tracing-futures` dependency is no longer required
  when using `#[instrument]` on async functions ([#808])
- **attributes**: Updated `tracing-attributes` minimum dependency to
  v0.1.12 ([#1222])

Thanks to @nagisa, @Txuritan, @TaKO8Ki, @okready, and @krojew for
contributing to this release!
hawkw added a commit that referenced this pull request Feb 4, 2021
### Fixed

- **attributes**: Compiler error when using `#[instrument(err)]` on
  functions with mutable parameters ([#1167])
- **attributes**: Missing function visibility modifier when using
  `#[instrument]` with `async-trait` ([#977])
- **attributes** Removed unused `syn` features ([#928])
- **log**: Fixed an issue where the `tracing` macros would generate code
  for events whose levels are disabled statically by the `log` crate's
  `static_max_level_XXX` features ([#1175])
- Fixed deprecations and clippy lints ([#1195])
- Several documentation fixes and improvements ([#941], [#965], [#981],
  [#1146], [#1215])

### Changed

- **attributes**: `tracing-futures` dependency is no longer required
  when using `#[instrument]` on async functions ([#808])
- **attributes**: Updated `tracing-attributes` minimum dependency to
  v0.1.12 ([#1222])

Thanks to @nagisa, @Txuritan, @TaKO8Ki, @okready, and @krojew for
contributing to this release!
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.

None yet

2 participants