rust-lang / rust-lang/rust

False positive of "unused_braces" when combined with Mutex

Open
#143,562 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the supplied Rust program with rustc or the linked Rust Playground and confirm the unused_braces warnings and Mutex behavior. Trace the compiler's unused_braces handling from that diagnostic, then add coverage for this case and verify that braces with the shown lock-related side effect are not reported as unnecessary.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.