Open
Description
Using the following flags
--force-warn clippy::ref-binding-to-reference
this code:
fn f1(_: &str) {}
macro_rules! m2 {
($e:expr) => {
f1(*$e)
};
}
macro_rules! m3 {
($i:ident) => {
Some(ref $i)
};
}
fn main() {
let x = String::new();
// Ok, the pattern is from a macro
let _: &&String = match Some(&x) {
m3!(x) => x,
None => return,
};
// Err, reference to a &String
let _: &&String = match Some(&x) {
Some(ref x) => x,
//~^ ref_binding_to_reference
None => return,
};
}
caused the following diagnostics:
Checking _a v0.1.0 (/tmp/icemaker_global_tempdir.10JLGLYCNz9G/icemaker_clippyfix_tempdir.O7oZxdpBlDxD/_a)
warning: this pattern creates a reference to a reference
--> src/main.rs:24:14
|
24 | Some(ref x) => x,
| ^^^^^
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#ref_binding_to_reference
= note: requested on the command line with `--force-warn clippy::ref-binding-to-reference`
help: try
|
24 - Some(ref x) => x,
24 + Some(x) => &x,
|
warning: `_a` (bin "_a") generated 1 warning (run `cargo clippy --fix --bin "_a"` to apply 1 suggestion)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.18s
However after applying these diagnostics, the resulting code:
fn f1(_: &str) {}
macro_rules! m2 {
($e:expr) => {
f1(*$e)
};
}
macro_rules! m3 {
($i:ident) => {
Some(ref $i)
};
}
fn main() {
let x = String::new();
// Ok, the pattern is from a macro
let _: &&String = match Some(&x) {
m3!(x) => x,
None => return,
};
// Err, reference to a &String
let _: &&String = match Some(&x) {
Some(x) => &x,
//~^ ref_binding_to_reference
None => return,
};
}
no longer compiled:
Checking _a v0.1.0 (/tmp/icemaker_global_tempdir.10JLGLYCNz9G/icemaker_clippyfix_tempdir.O7oZxdpBlDxD/_a)
error[E0597]: `x` does not live long enough
--> src/main.rs:24:20
|
24 | Some(x) => &x,
| - ^-
| | ||
| | |`x` dropped here while still borrowed
| | borrowed value does not live long enough
| binding `x` declared here
For more information about this error, try `rustc --explain E0597`.
error: could not compile `_a` (bin "_a" test) due to 1 previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `_a` (bin "_a") due to 1 previous error
Version:
rustc 1.90.0-nightly (c83e217d2 2025-07-06)
binary: rustc
commit-hash: c83e217d268d25960a0c79c6941bcb3917a6a0af
commit-date: 2025-07-06
host: x86_64-unknown-linux-gnu
release: 1.90.0-nightly
LLVM version: 20.1.7