Skip to content

New Lint - Pointless mutation #13977

Open
@kinnison

Description

@kinnison

What it does

The lint should detect situations in which newly created values are mutated and then immediately dropped during assignments.

Advantage

Would spot situations such as the example below and warn the user that it is not acting as the author intended.

Drawbacks

Deliberate discarding of values may end up being slightly less ergonomic in some corner cases.

Example

This example is possibly a bit wordy, but is a simplified example of something encountered during a code review at work:

    struct Thingy {
        foo: Option<(u32, u32)>,
    }

    let mut thingy = Thingy { foo: None };
    thingy.foo = Some((0, 1));

    // This operation is effectively useless (no side-effects etc.)
    thingy.foo.unwrap().1 = 2;

    // What intuitively we'd expected to pass
    assert_eq!(thingy.foo.unwrap().1, 2);

The lint is clearly meant to trigger on the thingy.foo.unwrap().1 = 2; line and probably ought to suggest:

Could be written as:

    thingy.foo.as_mut().unwrap().1 = 2;

Though the exact details of the suggestion would likely tie the lint to only working on replacing the innards of Options or similar.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions