From 1b89aa950ac623b3157afeac3dd827976b7a556a Mon Sep 17 00:00:00 2001 From: Kartavya Vashishtha Date: Fri, 22 Jul 2022 00:08:11 +0530 Subject: [PATCH] subscriber: mark builders as must_use (#2239) ## Motivation Builders not marked `#[must_use]` can not be initialized sometimes, causing silent failures. Eg. ```rust fn main() { tracing_subscriber::fmt(); tracing::info!("hello"); } ``` won't print anything. ## Solution Added `#[must_use]` to builder types in the tracing-subscriber crate. --- tracing-subscriber/src/filter/env/builder.rs | 1 + tracing-subscriber/src/fmt/mod.rs | 13 +++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/tracing-subscriber/src/filter/env/builder.rs b/tracing-subscriber/src/filter/env/builder.rs index 36b5205431..7bc65484ea 100644 --- a/tracing-subscriber/src/filter/env/builder.rs +++ b/tracing-subscriber/src/filter/env/builder.rs @@ -11,6 +11,7 @@ use tracing::level_filters::STATIC_MAX_LEVEL; /// /// [builder]: https://rust-unofficial.github.io/patterns/patterns/creational/builder.html #[derive(Debug, Clone)] +#[must_use] pub struct Builder { regex: bool, env: Option, diff --git a/tracing-subscriber/src/fmt/mod.rs b/tracing-subscriber/src/fmt/mod.rs index 9103ce8f16..025e17504d 100644 --- a/tracing-subscriber/src/fmt/mod.rs +++ b/tracing-subscriber/src/fmt/mod.rs @@ -243,6 +243,7 @@ pub type Formatter< /// Configures and constructs `Subscriber`s. #[cfg_attr(docsrs, doc(cfg(all(feature = "fmt", feature = "std"))))] #[derive(Debug)] +#[must_use] pub struct SubscriberBuilder< N = format::DefaultFields, E = format::Format, @@ -465,7 +466,8 @@ impl Default for SubscriberBuilder { SubscriberBuilder { filter: Subscriber::DEFAULT_MAX_LEVEL, inner: Default::default(), - }.log_internal_errors(true) + } + .log_internal_errors(true) } } @@ -626,12 +628,15 @@ where /// By default, `fmt::Layer` will write any `FormatEvent`-internal errors to /// the writer. These errors are unlikely and will only occur if there is a /// bug in the `FormatEvent` implementation or its dependencies. - /// + /// /// If writing to the writer fails, the error message is printed to stderr /// as a fallback. - /// + /// /// [`FormatEvent`]: crate::fmt::FormatEvent - pub fn log_internal_errors(self, log_internal_errors: bool) -> SubscriberBuilder, F, W> { + pub fn log_internal_errors( + self, + log_internal_errors: bool, + ) -> SubscriberBuilder, F, W> { SubscriberBuilder { inner: self.inner.log_internal_errors(log_internal_errors), ..self