Open t-webber opened 8 months ago
The second suggestion it gives is the one that works here. It's telling you to do this:
#[warn(clippy::pattern_type_mismatch)]
fn main() {
let wrapped_data: Option<String> = Some("Hello".to_owned());
if let &Some(ref data) = &wrapped_data {
println!("Access to data: {}", &data);
};
println!("Data still owned: {wrapped_data:?}");
}
Summary
When I match an
Option<String>
, and when ownership is not required, I often reference the data using a&
. However, dereferencing with&
doesn't give the right type (&Option<String>
instead ofOption<&String>
), so with[(clippy::pattern_type_mismatch)]
, I get a warning. Thus I useas_ref()
, and that is what I fealclippy
should tell me to do. Instead, theclippy
warning tells me to dereference the expression, but by doing so, the ownership is gone for the lines after the end of thematch
expression.Reproducer
I tried this code:
Clippy suggests:
However, if dereferenced,
wrapped_data
looses ownership and the last line will fail.Clippy should suggest:
Version
Additional Labels
No response