Skip to content

False positive of "unused_braces" when combined with Mutex #143562

@kindabit

Description

@kindabit

Code

use std::sync::Mutex;

struct Test {
    a: i32,
    b: i32,
}

struct VecVec {
    v: Vec<i32>,
}

impl VecVec {
    pub fn push(mut self, value: i32) -> Self {
        self.v.push(value);
        self
    }
}

pub fn main() {
    let test = Mutex::new(Test {
        a: 12,
        b: 34,
    });
    
    let vec_vec = VecVec { v: Vec::new() }
        .push({ test.lock().unwrap().a })  // the program will hang if this pair of braces is removed
        .push({ test.lock().unwrap().b });
    
    println!("len: {}", vec_vec.v.len());
}

Current output

Compiling playground v0.0.1 (/playground)
warning: unnecessary braces around method argument
  --> src/lib.rs:27:15
   |
27 |         .push({ test.lock().unwrap().b });
   |               ^^                      ^^
   |
   = note: `#[warn(unused_braces)]` on by default
help: remove these braces
   |
27 -         .push({ test.lock().unwrap().b });
27 +         .push(test.lock().unwrap().b);
   |

warning: unnecessary braces around method argument
  --> src/lib.rs:26:15
   |
26 |         .push({ test.lock().unwrap().a })  // the program will hang if this pair of braces is removed
   |               ^^                      ^^
   |
help: remove these braces
   |
26 -         .push({ test.lock().unwrap().a })  // the program will hang if this pair of braces is removed
26 +         .push(test.lock().unwrap().a)  // the program will hang if this pair of braces is removed
   |

warning: `playground` (lib) generated 2 warnings (run `cargo fix --lib -p playground` to apply 2 suggestions)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.57s

Desired output

The compiler should detect the side effects and not treat these braces as unnecessary.

Rationale and extra context

Rust Playground

Other cases

Rust Version

This is the output of `rustc --version --verbose` on my computer:

rustc 1.88.0 (6b00bc388 2025-06-23)
binary: rustc
commit-hash: 6b00bc3880198600130e1cf62b8f8a93494488cc
commit-date: 2025-06-23
host: x86_64-pc-windows-msvc
release: 1.88.0
LLVM version: 20.1.5

This issue could also be reproduced on rust playground.

Anything else?

No response

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions