Merge unwrap_or_else_default.rs into or_fun_call.rs

This commit is contained in:
Samuel Moelius 2023-07-19 20:05:16 -04:00
parent 0b63e95dce
commit 84c411272d
9 changed files with 343 additions and 191 deletions

View file

@ -190,7 +190,7 @@ mod issue8239 {
acc.push_str(&f);
acc
})
.unwrap_or_default();
.unwrap_or(String::new());
}
fn more_to_max_suggestion_highest_lines_1() {
@ -203,7 +203,7 @@ mod issue8239 {
acc.push_str(&f);
acc
})
.unwrap_or_default();
.unwrap_or(String::new());
}
fn equal_to_max_suggestion_highest_lines() {
@ -215,7 +215,7 @@ mod issue8239 {
acc.push_str(&f);
acc
})
.unwrap_or_default();
.unwrap_or(String::new());
}
fn less_than_max_suggestion_highest_lines() {
@ -226,7 +226,7 @@ mod issue8239 {
acc.push_str(&f);
acc
})
.unwrap_or_default();
.unwrap_or(String::new());
}
}
@ -257,4 +257,59 @@ mod issue8993 {
}
}
mod lazy {
use super::*;
fn foo() {
struct Foo;
impl Foo {
fn new() -> Foo {
Foo
}
}
struct FakeDefault;
impl FakeDefault {
fn default() -> Self {
FakeDefault
}
}
impl Default for FakeDefault {
fn default() -> Self {
FakeDefault
}
}
let with_new = Some(vec![1]);
with_new.unwrap_or_default();
let with_default_trait = Some(1);
with_default_trait.unwrap_or_default();
let with_default_type = Some(1);
with_default_type.unwrap_or_default();
let real_default = None::<FakeDefault>;
real_default.unwrap_or_default();
let mut map = HashMap::<u64, String>::new();
map.entry(42).or_default();
let mut btree = BTreeMap::<u64, String>::new();
btree.entry(42).or_default();
let stringy = Some(String::new());
let _ = stringy.unwrap_or_default();
// negative tests
let self_default = None::<FakeDefault>;
self_default.unwrap_or_else(<FakeDefault>::default);
let without_default = Some(Foo);
without_default.unwrap_or_else(Foo::new);
}
}
fn main() {}

View file

@ -257,4 +257,59 @@ mod issue8993 {
}
}
mod lazy {
use super::*;
fn foo() {
struct Foo;
impl Foo {
fn new() -> Foo {
Foo
}
}
struct FakeDefault;
impl FakeDefault {
fn default() -> Self {
FakeDefault
}
}
impl Default for FakeDefault {
fn default() -> Self {
FakeDefault
}
}
let with_new = Some(vec![1]);
with_new.unwrap_or_else(Vec::new);
let with_default_trait = Some(1);
with_default_trait.unwrap_or_else(Default::default);
let with_default_type = Some(1);
with_default_type.unwrap_or_else(u64::default);
let real_default = None::<FakeDefault>;
real_default.unwrap_or_else(<FakeDefault as Default>::default);
let mut map = HashMap::<u64, String>::new();
map.entry(42).or_insert_with(String::new);
let mut btree = BTreeMap::<u64, String>::new();
btree.entry(42).or_insert_with(String::new);
let stringy = Some(String::new());
let _ = stringy.unwrap_or_else(String::new);
// negative tests
let self_default = None::<FakeDefault>;
self_default.unwrap_or_else(<FakeDefault>::default);
let without_default = Some(Foo);
without_default.unwrap_or_else(Foo::new);
}
}
fn main() {}

View file

@ -6,11 +6,13 @@ LL | with_constructor.unwrap_or(make());
|
= note: `-D clippy::or-fun-call` implied by `-D warnings`
error: use of `unwrap_or` followed by a call to `new`
error: use of `unwrap_or` to construct default value
--> $DIR/or_fun_call.rs:56:14
|
LL | with_new.unwrap_or(Vec::new());
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
|
= note: `-D clippy::unwrap-or-else-default` implied by `-D warnings`
error: use of `unwrap_or` followed by a function call
--> $DIR/or_fun_call.rs:59:21
@ -30,13 +32,13 @@ error: use of `unwrap_or` followed by a function call
LL | with_err_args.unwrap_or(Vec::with_capacity(12));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(|_| Vec::with_capacity(12))`
error: use of `unwrap_or` followed by a call to `default`
error: use of `unwrap_or` to construct default value
--> $DIR/or_fun_call.rs:68:24
|
LL | with_default_trait.unwrap_or(Default::default());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
error: use of `unwrap_or` followed by a call to `default`
error: use of `unwrap_or` to construct default value
--> $DIR/or_fun_call.rs:71:23
|
LL | with_default_type.unwrap_or(u64::default());
@ -48,13 +50,13 @@ error: use of `unwrap_or` followed by a function call
LL | self_default.unwrap_or(<FakeDefault>::default());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(<FakeDefault>::default)`
error: use of `unwrap_or` followed by a call to `default`
error: use of `unwrap_or` to construct default value
--> $DIR/or_fun_call.rs:77:18
|
LL | real_default.unwrap_or(<FakeDefault as Default>::default());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
error: use of `unwrap_or` followed by a call to `new`
error: use of `unwrap_or` to construct default value
--> $DIR/or_fun_call.rs:80:14
|
LL | with_vec.unwrap_or(vec![]);
@ -66,31 +68,31 @@ error: use of `unwrap_or` followed by a function call
LL | without_default.unwrap_or(Foo::new());
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(Foo::new)`
error: use of `or_insert` followed by a call to `new`
error: use of `or_insert` to construct default value
--> $DIR/or_fun_call.rs:86:19
|
LL | map.entry(42).or_insert(String::new());
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `or_default()`
error: use of `or_insert` followed by a call to `new`
error: use of `or_insert` to construct default value
--> $DIR/or_fun_call.rs:89:23
|
LL | map_vec.entry(42).or_insert(vec![]);
| ^^^^^^^^^^^^^^^^^ help: try: `or_default()`
error: use of `or_insert` followed by a call to `new`
error: use of `or_insert` to construct default value
--> $DIR/or_fun_call.rs:92:21
|
LL | btree.entry(42).or_insert(String::new());
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `or_default()`
error: use of `or_insert` followed by a call to `new`
error: use of `or_insert` to construct default value
--> $DIR/or_fun_call.rs:95:25
|
LL | btree_vec.entry(42).or_insert(vec![]);
| ^^^^^^^^^^^^^^^^^ help: try: `or_default()`
error: use of `unwrap_or` followed by a call to `new`
error: use of `unwrap_or` to construct default value
--> $DIR/or_fun_call.rs:98:21
|
LL | let _ = stringy.unwrap_or(String::new());
@ -132,30 +134,6 @@ error: use of `unwrap_or` followed by a function call
LL | None.unwrap_or( unsafe { ptr_to_ref(s) } );
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(|| unsafe { ptr_to_ref(s) })`
error: use of `unwrap_or` followed by a call to `new`
--> $DIR/or_fun_call.rs:193:14
|
LL | .unwrap_or(String::new());
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
error: use of `unwrap_or` followed by a call to `new`
--> $DIR/or_fun_call.rs:206:14
|
LL | .unwrap_or(String::new());
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
error: use of `unwrap_or` followed by a call to `new`
--> $DIR/or_fun_call.rs:218:14
|
LL | .unwrap_or(String::new());
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
error: use of `unwrap_or` followed by a call to `new`
--> $DIR/or_fun_call.rs:229:10
|
LL | .unwrap_or(String::new());
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
error: use of `map_or` followed by a function call
--> $DIR/or_fun_call.rs:254:25
|
@ -168,5 +146,47 @@ error: use of `map_or` followed by a function call
LL | let _ = Some(4).map_or(g(), f);
| ^^^^^^^^^^^^^^ help: try: `map_or_else(g, f)`
error: aborting due to 28 previous errors
error: use of `unwrap_or_else` to construct default value
--> $DIR/or_fun_call.rs:286:18
|
LL | with_new.unwrap_or_else(Vec::new);
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
error: use of `unwrap_or_else` to construct default value
--> $DIR/or_fun_call.rs:289:28
|
LL | with_default_trait.unwrap_or_else(Default::default);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
error: use of `unwrap_or_else` to construct default value
--> $DIR/or_fun_call.rs:292:27
|
LL | with_default_type.unwrap_or_else(u64::default);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
error: use of `unwrap_or_else` to construct default value
--> $DIR/or_fun_call.rs:295:22
|
LL | real_default.unwrap_or_else(<FakeDefault as Default>::default);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
error: use of `or_insert_with` to construct default value
--> $DIR/or_fun_call.rs:298:23
|
LL | map.entry(42).or_insert_with(String::new);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `or_default()`
error: use of `or_insert_with` to construct default value
--> $DIR/or_fun_call.rs:301:25
|
LL | btree.entry(42).or_insert_with(String::new);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `or_default()`
error: use of `unwrap_or_else` to construct default value
--> $DIR/or_fun_call.rs:304:25
|
LL | let _ = stringy.unwrap_or_else(String::new);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
error: aborting due to 31 previous errors

View file

@ -4,7 +4,7 @@
#![allow(dead_code)]
#![allow(clippy::unnecessary_wraps, clippy::unnecessary_literal_unwrap)]
/// Checks implementation of the `UNWRAP_OR_ELSE_DEFAULT` lint.
/// Checks implementation of the `UNWRAP_OR_DEFAULT` lint.
fn unwrap_or_else_default() {
struct Foo;
@ -111,4 +111,21 @@ fn type_certainty(option: Option<Vec<u64>>) {
option.unwrap_or_else(Vec::new).push(1);
}
fn method_call_with_deref() {
use std::cell::RefCell;
use std::collections::HashMap;
let cell = RefCell::new(HashMap::<u64, HashMap<u64, String>>::new());
let mut outer_map = cell.borrow_mut();
#[allow(unused_assignments)]
let mut option = None;
option = Some(0);
let inner_map = outer_map.get_mut(&option.unwrap()).unwrap();
let _ = inner_map.entry(0).or_default();
}
fn main() {}

View file

@ -4,7 +4,7 @@
#![allow(dead_code)]
#![allow(clippy::unnecessary_wraps, clippy::unnecessary_literal_unwrap)]
/// Checks implementation of the `UNWRAP_OR_ELSE_DEFAULT` lint.
/// Checks implementation of the `UNWRAP_OR_DEFAULT` lint.
fn unwrap_or_else_default() {
struct Foo;
@ -111,4 +111,21 @@ fn type_certainty(option: Option<Vec<u64>>) {
option.unwrap_or_else(Vec::new).push(1);
}
fn method_call_with_deref() {
use std::cell::RefCell;
use std::collections::HashMap;
let cell = RefCell::new(HashMap::<u64, HashMap<u64, String>>::new());
let mut outer_map = cell.borrow_mut();
#[allow(unused_assignments)]
let mut option = None;
option = Some(0);
let inner_map = outer_map.get_mut(&option.unwrap()).unwrap();
let _ = inner_map.entry(0).or_insert_with(Default::default);
}
fn main() {}

View file

@ -1,88 +1,94 @@
error: use of `.unwrap_or_else(..)` to construct default value
--> $DIR/unwrap_or_else_default.rs:48:5
error: use of `unwrap_or_else` to construct default value
--> $DIR/unwrap_or_else_default.rs:48:14
|
LL | with_new.unwrap_or_else(Vec::new);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `with_new.unwrap_or_default()`
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
|
= note: `-D clippy::unwrap-or-else-default` implied by `-D warnings`
error: use of `.unwrap_or_else(..)` to construct default value
--> $DIR/unwrap_or_else_default.rs:62:5
error: use of `unwrap_or_else` to construct default value
--> $DIR/unwrap_or_else_default.rs:62:23
|
LL | with_real_default.unwrap_or_else(<HasDefaultAndDuplicate as Default>::default);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `with_real_default.unwrap_or_default()`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
error: use of `.unwrap_or_else(..)` to construct default value
--> $DIR/unwrap_or_else_default.rs:65:5
error: use of `unwrap_or_else` to construct default value
--> $DIR/unwrap_or_else_default.rs:65:24
|
LL | with_default_trait.unwrap_or_else(Default::default);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `with_default_trait.unwrap_or_default()`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
error: use of `.unwrap_or_else(..)` to construct default value
--> $DIR/unwrap_or_else_default.rs:68:5
error: use of `unwrap_or_else` to construct default value
--> $DIR/unwrap_or_else_default.rs:68:23
|
LL | with_default_type.unwrap_or_else(u64::default);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `with_default_type.unwrap_or_default()`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
error: use of `.unwrap_or_else(..)` to construct default value
--> $DIR/unwrap_or_else_default.rs:71:5
error: use of `unwrap_or_else` to construct default value
--> $DIR/unwrap_or_else_default.rs:71:23
|
LL | with_default_type.unwrap_or_else(Vec::new);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `with_default_type.unwrap_or_default()`
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
error: use of `.unwrap_or_else(..)` to construct default value
--> $DIR/unwrap_or_else_default.rs:74:5
error: use of `unwrap_or_else` to construct default value
--> $DIR/unwrap_or_else_default.rs:74:18
|
LL | empty_string.unwrap_or_else(|| "".to_string());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `empty_string.unwrap_or_default()`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
error: use of `.unwrap_or_else(..)` to construct default value
--> $DIR/unwrap_or_else_default.rs:78:5
error: use of `unwrap_or_else` to construct default value
--> $DIR/unwrap_or_else_default.rs:78:12
|
LL | option.unwrap_or_else(Vec::new).push(1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `option.unwrap_or_default()`
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
error: use of `.unwrap_or_else(..)` to construct default value
--> $DIR/unwrap_or_else_default.rs:81:5
error: use of `unwrap_or_else` to construct default value
--> $DIR/unwrap_or_else_default.rs:81:12
|
LL | option.unwrap_or_else(Vec::new).push(1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `option.unwrap_or_default()`
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
error: use of `.unwrap_or_else(..)` to construct default value
--> $DIR/unwrap_or_else_default.rs:84:5
error: use of `unwrap_or_else` to construct default value
--> $DIR/unwrap_or_else_default.rs:84:12
|
LL | option.unwrap_or_else(Vec::new).push(1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `option.unwrap_or_default()`
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
error: use of `.unwrap_or_else(..)` to construct default value
--> $DIR/unwrap_or_else_default.rs:87:5
error: use of `unwrap_or_else` to construct default value
--> $DIR/unwrap_or_else_default.rs:87:12
|
LL | option.unwrap_or_else(Vec::new).push(1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `option.unwrap_or_default()`
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
error: use of `.unwrap_or_else(..)` to construct default value
--> $DIR/unwrap_or_else_default.rs:90:5
error: use of `unwrap_or_else` to construct default value
--> $DIR/unwrap_or_else_default.rs:90:12
|
LL | option.unwrap_or_else(Vec::new).push(1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `option.unwrap_or_default()`
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
error: use of `.unwrap_or_else(..)` to construct default value
--> $DIR/unwrap_or_else_default.rs:93:5
error: use of `unwrap_or_else` to construct default value
--> $DIR/unwrap_or_else_default.rs:93:12
|
LL | option.unwrap_or_else(Vec::new).push(1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `option.unwrap_or_default()`
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
error: use of `.unwrap_or_else(..)` to construct default value
--> $DIR/unwrap_or_else_default.rs:96:5
error: use of `unwrap_or_else` to construct default value
--> $DIR/unwrap_or_else_default.rs:96:12
|
LL | option.unwrap_or_else(Vec::new).push(1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `option.unwrap_or_default()`
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
error: use of `.unwrap_or_else(..)` to construct default value
--> $DIR/unwrap_or_else_default.rs:99:5
error: use of `unwrap_or_else` to construct default value
--> $DIR/unwrap_or_else_default.rs:99:12
|
LL | option.unwrap_or_else(Vec::new).push(1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `option.unwrap_or_default()`
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
error: aborting due to 14 previous errors
error: use of `or_insert_with` to construct default value
--> $DIR/unwrap_or_else_default.rs:128:32
|
LL | let _ = inner_map.entry(0).or_insert_with(Default::default);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `or_default()`
error: aborting due to 15 previous errors