Unnamed repository; edit this file 'description' to name the repository.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext};

// Diagnostic: mut-ref-in-imm-ref-pat
//
// This diagnostic is triggered when a binding tries to mutably borrow through
// an `&` pattern.
pub(crate) fn mut_ref_in_imm_ref_pat(
    ctx: &DiagnosticsContext<'_, '_>,
    d: &hir::MutRefInImmRefPat,
) -> Diagnostic {
    Diagnostic::new_with_syntax_node_ptr(
        ctx,
        DiagnosticCode::RustcHardError("E0596"),
        "cannot borrow as mutable inside an `&` pattern",
        d.pat.map(Into::into),
    )
    .stable()
}

#[cfg(test)]
mod tests {
    use crate::tests::check_diagnostics;

    #[test]
    fn mut_ref_in_imm_ref_pat() {
        check_diagnostics(
            r#"
#![feature(ref_pat_eat_one_layer_2024)]

fn main() {
    let &ref mut _x = &mut 0;
       //^^^^^^^^^^ error: cannot borrow as mutable inside an `&` pattern
}
"#,
        );
    }
}