rust-lang / rust-lang/rust-clippy

`useless_let_if_seq` issue?

Open
#15,739 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I have this code:

let mut size: usize = 0;
let mut page: u64 = 1;

if let Some(pagination) = pagination {
    size = pagination.size().try_into().unwrap();
    page = pagination.page();
}

and clippy is saying:

warning: `if _ { .. } else { .. }` is an expression
239 | /     let mut page: u64 = 1;
240 | |
241 | |     if let Some(pagination) = pagination {
242 | |         size = pagination.size().try_into().unwrap();
243 | |         page = pagination.page();
244 | |     }
    | |_____^ help: it is more idiomatic to write: `let <mut> page = if let Some(pagination) = pagination { ..; pagination.page() } else { 1 };`
    |
    = 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
    = note: `-W clippy::useless-let-if-seq` implied by `-W clippy::nursery`
    = help: to override `-W clippy::nursery` add `#[allow(clippy::useless_let_if_seq)]`

So I think I should change it to:

let size = input
    .pagination
    .as_ref()
    .map_or(0, |pagination| pagination.size().try_into().unwrap());

let page = input.pagination.as_ref().map_or(1, Input::page);

Am I right?

Isn't the check on the .map_or() duplicated now? Is this a performance regression?

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 useless_let_if_seq lint entry point and its documentation, then compare the suggested rewrite with the Rust example in the issue. Determine whether the rewrite duplicates the pagination check or changes performance, and document the conclusion or identify the lint behavior that needs correction.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.