Skip to content

Incorrect suggestion by useless_let_if_seq lint #2918

@df5602

Description

@df5602

Hi,

Given the following code:

fn test(idx: usize) -> bool { idx % 2 == 0 } fn algo(mut x: usize, y: usize, height: usize, width: usize) { let mut last_row_length = 0; let mut sx = x; if last_row_length != 0 && test(y * width + x) { loop { last_row_length -= 1; if last_row_length == 0 { return; } x += 1; if !test(y * width + x) { break; } } sx = x; } else { while x != 0 && !test(y * width + x - 1) { x -= 1; if y != 0 && !test((y - 1) * width + x) { algo(x, y - 1, width, height); } last_row_length += 1; } } println!("{}", sx); }

clippy suggests the following:

warning: `if _ { .. } else { .. }` is an expression --> src/main.rs:7:5 | 7 | / let mut sx = x; 8 | | 9 | | if last_row_length != 0 && test(y * width + x) { 10 | | loop { ... | 30 | | } 31 | | } | |_____^ help: it is more idiomatic to write: `let <mut> sx = if last_row_length != 0 && test(y * width + x) { ..; x } else { ..; x };` | = note: #[warn(useless_let_if_seq)] on by default = note: you might not need `mut` at all = help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/v0.0.212/index.html#useless_let_if_seq 

This is incorrect, since x is modified in the loop in the else path, but NOT assigned to sx.

(Playground link)

(Note: the code itself is not the most idiomatic, since I'm transcribing an algorithm from another language...)

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when appliedL-suggestionLint: Improving, adding or fixing lint suggestions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions