rust-lang / rust-lang/rust-clippy
Discourage evaluation in log macros
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
What it does
This lint would suggest not to perform evaluations in log macro parameters. This is because an expression can panic within the log macro, but it wouldn't be evaluated if a logger implementation is not already configured.
Lint Name
Evaluation in Log Macro
Category
No response
Advantage
- Reduces the possibility of an uncaught panic when an evaluation is performed without a logger enabled, which is primarily a concern during testing.
Drawbacks
- Will require an additional variable to be used to hold the output of the evaluation.
Example
fn will_panic() -> u64 {
panic!();
0
}
// Not evaluated unless a logger has previously been configured.
// This is particularly problematic in tests, where one would either need to have a global logger set up using something
// like `ctor` or a local logger like `testing_logger` would need to be configured per each test, which is easily forgotten.
info!("{}", will_panic());
Could be written as:
let placeholder = will_panic();
info!("{placeholder}");
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading the linked log macro documentation and the issue's panic example. Determine how the proposed lint should identify evaluations in log macro parameters and how it should handle logger configuration. Done means the lint consistently flags the described pattern and supports the suggested pre-evaluation form, with tests covering the example.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100