Skip to content

Give a "modifying moved binding" or "unread value" warning in move-closures #37707

Closed
@critiqjo

Description

@critiqjo

Take a look at this logically wrong example:

fn main() {
    let mut x = 1;
    let y = (1..10).fold(0, move|acc, e| {
        x *= e; // should warn: new value of `x` never read
        acc + e
    });
    println!("{} {}", x, y); // outputs: `1 45`
}

It would be best if the compiler points out that "updating x inside the closure will not have an external effect." Though, I would be happy if the compiler at least pointed out that "the modified value in the new binding x is never read."

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions