Check that the suggested method exists in unwrap_or_default

This commit is contained in:
Alex Macleod 2023-08-19 18:06:46 +00:00
parent d9e6aac8bb
commit 8f2d47ea72
3 changed files with 77 additions and 2 deletions

View file

@ -130,4 +130,34 @@ fn method_call_with_deref() {
let _ = inner_map.entry(0).or_default();
}
fn missing_suggested_method() {
#[derive(Copy, Clone)]
struct S<T>(T);
impl<T> S<T> {
fn or_insert_with(&mut self, default: impl FnOnce() -> T) -> &mut T {
&mut self.0
}
fn or_insert(&mut self, default: T) -> &mut T {
&mut self.0
}
fn unwrap_or_else(self, default: impl FnOnce() -> T) -> T {
self.0
}
fn unwrap_or(self, default: T) -> T {
self.0
}
}
// Don't lint when or_default/unwrap_or_default do not exist on the type
let mut s = S(1);
s.or_insert_with(Default::default);
s.or_insert(Default::default());
s.unwrap_or_else(Default::default);
s.unwrap_or(Default::default());
}
fn main() {}

View file

@ -130,4 +130,34 @@ fn method_call_with_deref() {
let _ = inner_map.entry(0).or_insert_with(Default::default);
}
fn missing_suggested_method() {
#[derive(Copy, Clone)]
struct S<T>(T);
impl<T> S<T> {
fn or_insert_with(&mut self, default: impl FnOnce() -> T) -> &mut T {
&mut self.0
}
fn or_insert(&mut self, default: T) -> &mut T {
&mut self.0
}
fn unwrap_or_else(self, default: impl FnOnce() -> T) -> T {
self.0
}
fn unwrap_or(self, default: T) -> T {
self.0
}
}
// Don't lint when or_default/unwrap_or_default do not exist on the type
let mut s = S(1);
s.or_insert_with(Default::default);
s.or_insert(Default::default());
s.unwrap_or_else(Default::default);
s.unwrap_or(Default::default());
}
fn main() {}