rust-lang / rust-lang/rust-clippy

Bad suggestion of `useless_let_if_seq` when having side effects.

Open
#4,124 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

L-suggestion
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

The version of clippy is 0.0.212.

Here's an example:

pub fn func() {
    // This need to be called first always.
    let mut ret = foo1();
    if cond() {
        // Overwrite with some complex expr.
        ret = (foo2() * 2).to_string().len() as _;
    }
    
    baz(ret)
}

It suggest writing let <mut> ret = if cond() { .. foo2() .. } else { foo1() };, which cause 3 function calls (may have side-effects) being reordered, and foo1() not always being called.

Well, I'm not sure how to write make it more idiomatic in this case...

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 reproducing the useless_let_if_seq suggestion with the Rust example in the issue, using cargo clippy. Read the lint implementation and its existing tests to determine how side effects and evaluation order are handled; done means the example no longer receives an unsafe or misleading suggestion while valid cases still do.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.