Skip to content

unused_variables and unused_assignments lints do not catch array usage #65467

@jamesmunns

Description

@jamesmunns

I recently had a problem where I was accidentally implicitly copying an array with if let bindings, and the writes to the original array were never made (I needed to add ref in my bindings). I thought it was strange that there was no warnings for this, and noticed this is true for these lints in general. Here's a small reproduction (also on the playground).

fn main() {
    // Write but never read. No warning is emitted
    let mut arr = [0; 5];
    arr[0] = 4;
    [0][0] = 0;
    
    // Write but never read, the following warnings are emitted
    
    // warning: variable `x` is assigned to, but never used
    //  --> src/main.rs:7:13
    //   |
    // 7 |     let mut x = 4;
    //   |             ^
    //   |
    //   = note: `#[warn(unused_variables)]` on by default
    //   = note: consider using `_x` instead
    
    // warning: value assigned to `x` is never read
    //  --> src/main.rs:8:5
    //   |
    // 8 |     x = 12;
    //   |     ^
    //   |
    //   = note: `#[warn(unused_assignments)]` on by default
    //   = help: maybe it is overwritten before being read?
    let mut x = 4;
    x = 12;
}

I would expect these lints to fire here, is this expected behavior? I tried a quick search and wasn't able to find an existing issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-arrayArea: `[T; N]`A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions