rust-lang / rust-lang/rust-clippy

`useless_let_if_seq` ignores pattern if any expr is between variable declaration and conditional assignment.

Open
#11,655 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-negative
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

Lint is ignored if any expression (pure or not) is between a variable declaration and the conditional assignment.

Lint Name

useless_let_if_seq

Reproducer

I tried this code:

let foo;
let _ = bar();

if bar() {
    foo = 42;
} else {
    foo = 0;
}

let mut baz = None;

if bar() {
    baz = Some(42);
}

I expected to see this happen:

warning: `if _ { .. } else { .. }` is an expression
  --> src/lib.rs:5:5
   |
5  | /     let foo;
6  | |     let _ = bar();
7  | |
8  | |     if bar() {
...  |
11 | |         foo = 0;
12 | |     }
   | |_____^ help: it is more idiomatic to write: `let foo = if bar() { 42 } else { 0 };`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_let_if_seq
note: the lint level is defined here
  --> src/lib.rs:1:9
   |
1  | #![warn(clippy::useless_let_if_seq)]
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^

warning: `if _ { .. } else { .. }` is an expression
  --> src/lib.rs:14:5
   |
14 | /     let mut baz = None;
15 | |
16 | |     if bar() {
17 | |         baz = Some(42);
18 | |     }
   | |_____^ help: it is more idiomatic to write: `let <mut> baz = if bar() { Some(42) } else { None };`
   |
   = note: you might not need `mut` at all
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_let_if_seq

warning: `playground` (lib) generated 2 warnings

Instead, this happened:
Only the second warning appeared, due to the statement between the declaration and assignment.

Version
rustc 1.75.0-nightly (bf9a1c8a1 2023-10-08)
binary: rustc
commit-hash: bf9a1c8a193fc373897196321215794c8bebbeec
commit-date: 2023-10-08
host: x86_64-unknown-linux-gnu
release: 1.75.0-nightly
LLVM version: 17.0.2

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 locating the implementation and regression tests for the useless_let_if_seq lint, then reproduce the issue with the provided Rust example. Trace why an intervening expression prevents the first warning, and add coverage so the lint reports both cases while preserving the existing behavior for the second conditional assignment.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.