Skip to content

OpAssign can cause unused_assignments false positives #31895

Closed
@durka

Description

@durka

This code

#![feature(augmented_assignments, op_assign_traits)]

use std::ops::AddAssign;

struct MutRef<'a>(&'a mut i32);

impl<'a> AddAssign<i32> for MutRef<'a> {
    fn add_assign(&mut self, rhs: i32) {
        *self.0 += rhs;
    }
}

fn main() {
    let mut a = 1;
    {
        let mut b = MutRef(&mut a);
        b += 1;
    }
    println!("{}", a);
}

reads the variable b by calling its AddAssign implementation. However, the warning still triggers. If you expand b.add_assign(1) manually, there is no warning.

cc @bluss. Found by "jatentaki" on IRC.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions