rust-lang / rust-lang/rust-clippy

New lint: Unused temporary variable

Open
#5,880 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint E-hard L-correctness
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

What it does

Lint should detect if we create a temporary variable that is dropped without usage (or side-effects).

See example below for creating a String which should have been assigned to the shadowed name, but instead does nothing.

Categories (optional)
  • Kind: clippy::correctness
What is the advantage of the recommended code over the original code

Original code creates a temporary String, which has the correct transformation applied but is then dropped, and the .len() is called on the original &str. This is not the intended behaviour but there are no warnings or errors.

The new code correctly assigns the temporary variable (to shadow the same username variable), which is then used.

The lint could just check the that variable is assigned and used later in the code.

Drawbacks

How can we distinguish if a method called on the temporary variable has side effects, and that is what was intended? Perhaps only apply for standard library types and methods.

Example
validate_with(|username: &str| -> Result<(), &str> {
                // Here String is immediately dropped
                username.to_string().retain(|c| !c.is_whitespace() && c.is_digit(10)); 
                if username.len() != 16 { // This erroneously checks against original &str
                    return Err("Mullvad account number should be 16 digits!");
                }
                Ok(())
            })

Could be written as:

validate_with(|username: &str| -> Result<(), &str> {
                // Now the temporary String is assigned and used
                let username = username.to_string().retain(|c| !c.is_whitespace() && c.is_digit(10)); 
                if username.len() != 16 { // Checks new String as intended
                    return Err("Mullvad account number should be 16 digits!");
                }
                Ok(())
            })

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 with the issue's Rust examples and review existing Clippy lint patterns for temporary values and method side effects. Define which cases are safe to diagnose, especially whether standard-library types or methods are required, then add coverage for the dropped temporary and the intended shadowing form.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.