Repository navigation
Commit ccdf2e9
style(proto): enforce clippy:let_unit_value (#4231)
see #4071.
this commit addresses the `let_unit_value` clippy lint. only two sites
are affected, relating to tracing spans.
this commit introduces a `hyper::trace::Disabled` type to act as a ZST
marker in places that can sometimes conditionally compile to a
`tracing::span::Entered` guard.
this is based upon experimenting in
<https://github.com/cratelyn/hyper-span-macro-example>.
to restate some of those learnings here:
### background
hyper contains a `trace` submodule, introduced in #3326,
that contains macros like the following:
```rust
macro_rules! trace {
($($arg:tt)*) => {
#[cfg(feature = "tracing")]
{
tracing::trace!($($arg)+);
}
}
}
macro_rules! trace_span {
($($arg:tt)*) => {
{
#[cfg(feature = "tracing")]
{
let _span = tracing::trace_span!($($arg)+);
_span.entered()
}
}
}
}
```
these thinly wrap `tracing`'s macros, to account for the fact that tracing
support in hyper is opt-in. it is gated behind a feature flag, and
additionally mandates that the `hyper_unstable_tracing` flag be enabled.
however, this leads to violations of clippy's `let_unit_value` rule when
compiled without the `tracing` feature flag is enabled. this is because
`trace_span!` only returns a `tracing::span::Entered` when that flag is active,
otherwise it returns `()`.
thus, we *must* bind these calls to a variable, so that we properly hold
on to the span guard when the tracing flag is active. however, we must
contend with the fact that when this flag is not active, strict clippy
linting yields us warnings like this:
```text
cargo clippy --no-default-features
Checking hyper-span-macro-example v0.1.0
error: this let-binding has unit value
--> src/main.rs:52:5
|
52 | let _entered = trace_span!("instrumented");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.99.0/index.html#let_unit_value
```
### why not use `#[allow(..)]`
one might try to selectively apply `#[allow(clippy::let_unit_value)]`
attributes to stifle this.
however, applying attributes to expressions is still a nightly feature
we cannot rely on yet, so call-sites for our macros must be updated
rather than modifying the code generated by the macro. see
rust-lang/rust#15701.
this would require us to do something like this throughout the codebase,
wherever we create tracing spans:
```rust
#[cfg_attr(not(feature = "tracing"), allow(clippy::let_unit_value))]
let _entered = trace_span!("instrumented");
```
`#[cfg_attr]` is a bit esoteric, and if we require that instrumenting code in
hyper require this song and dance every time we create a span, it will
generally discourage instrumentation.
### changes
this commit introduce a `Disabled` type to replace the empty `()` when
tracing instrumentation is disabled.
this satisfies clippy's `let_unit_value` rule because macros like
`trace_span!` will never return `()` no matter which feature flags are
active. this _also_ prevents accidentally writing code that would drop
span guards once the tracing flag is enabled, because an inert
`#[must_use]` guard acts as a stand-in when the flag isn't present.
Signed-off-by: katelyn martin <git@katelyn.world>1 parent 75d2355 commit ccdf2e9
2 files changed
Lines changed: 41 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
123 | 123 | | |
124 | 124 | | |
125 | 125 | | |
126 | | - | |
127 | 126 | | |
128 | 127 | | |
129 | 128 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
13 | 34 | | |
14 | 35 | | |
15 | 36 | | |
| |||
27 | 48 | | |
28 | 49 | | |
29 | 50 | | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
30 | 55 | | |
31 | 56 | | |
32 | 57 | | |
| |||
48 | 73 | | |
49 | 74 | | |
50 | 75 | | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
51 | 80 | | |
52 | 81 | | |
53 | 82 | | |
| |||
69 | 98 | | |
70 | 99 | | |
71 | 100 | | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
72 | 105 | | |
73 | 106 | | |
74 | 107 | | |
| |||
90 | 123 | | |
91 | 124 | | |
92 | 125 | | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
93 | 130 | | |
94 | 131 | | |
95 | 132 | | |
| |||
123 | 160 | | |
124 | 161 | | |
125 | 162 | | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
126 | 167 | | |
127 | 168 | | |
128 | 169 | | |
0 commit comments