rust-lang / rust-lang/rust-clippy

Discourage evaluation in log macros

Open
#8,936 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.