Warning: unconditional allocation
do_something().context(BadThingHappenedSnafu { username: username.to_string() });
^^^^^^^^^^^^ Remove this function call
Notes
SNAFU automatically calls Into::into on all arguments to context selectors, mostly for converting reference types to owned types (like &str to String). When used with the context method, if the user performs the allocation, it will happen all the time, even when no error has occurred.
This is conceptually very similar to Clippy's clippy::or_fun_call warning for Ok::<_, ()>(String::new()).unwrap_or(String::from("junk")).
I might also want to investigate if there is a similar lint that would warn about unneeded usages of with_context, which accepts a closure.
Lint explanation
Avoid unconditionally allocating when providing context for SNAFU errors.
Example code
Notes
SNAFU automatically calls
Into::into
on all arguments to context selectors, mostly for converting reference types to owned types (like&str
toString
). When used with thecontext
method, if the user performs the allocation, it will happen all the time, even when no error has occurred.This is conceptually very similar to Clippy's
clippy::or_fun_call
warning forOk::<_, ()>(String::new()).unwrap_or(String::from("junk"))
.I might also want to investigate if there is a similar lint that would warn about unneeded usages of
with_context
, which accepts a closure.