Skip to content

Commit 7d58ba2

Browse files
committed
1 parent 63987aa commit 7d58ba2

File tree

5 files changed

+13
-13
lines changed

5 files changed

+13
-13
lines changed

clippy_lints/src/utils/higher.rs

+3
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,9 @@ pub fn vec_macro<'e>(cx: &LateContext<'_, '_>, expr: &'e hir::Expr<'_>) -> Optio
280280

281281
None
282282
}
283+
else if match_def_path(cx, fun_def_id, &paths::VEC_NEW) && args.is_empty() {
284+
Some(VecArgs::Vec(&[]))
285+
}
283286
else {
284287
None
285288
};

clippy_lints/src/utils/paths.rs

+1
Original file line numberDiff line numberDiff line change
@@ -131,5 +131,6 @@ pub const VEC_AS_MUT_SLICE: [&str; 4] = ["alloc", "vec", "Vec", "as_mut_slice"];
131131
pub const VEC_AS_SLICE: [&str; 4] = ["alloc", "vec", "Vec", "as_slice"];
132132
pub const VEC_DEQUE: [&str; 4] = ["alloc", "collections", "vec_deque", "VecDeque"];
133133
pub const VEC_FROM_ELEM: [&str; 3] = ["alloc", "vec", "from_elem"];
134+
pub const VEC_NEW: [&str; 4] = ["alloc", "vec", "Vec", "new"];
134135
pub const WEAK_ARC: [&str; 3] = ["alloc", "sync", "Weak"];
135136
pub const WEAK_RC: [&str; 3] = ["alloc", "rc", "Weak"];

tests/ui/or_fun_call.fixed

+1-3
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ fn or_fun_call() {
5353
with_default_type.unwrap_or_default();
5454

5555
let with_vec = Some(vec![1]);
56-
with_vec.unwrap_or_else(|| vec![]);
57-
58-
// FIXME #944: ~|SUGGESTION with_vec.unwrap_or_else(|| vec![]);
56+
with_vec.unwrap_or_default();
5957

6058
let without_default = Some(Foo);
6159
without_default.unwrap_or_else(Foo::new);

tests/ui/or_fun_call.rs

-2
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ fn or_fun_call() {
5555
let with_vec = Some(vec![1]);
5656
with_vec.unwrap_or(vec![]);
5757

58-
// FIXME #944: ~|SUGGESTION with_vec.unwrap_or_else(|| vec![]);
59-
6058
let without_default = Some(Foo);
6159
without_default.unwrap_or(Foo::new());
6260

tests/ui/or_fun_call.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -42,38 +42,38 @@ error: use of `unwrap_or` followed by a call to `default`
4242
LL | with_default_type.unwrap_or(u64::default());
4343
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_default_type.unwrap_or_default()`
4444

45-
error: use of `unwrap_or` followed by a function call
46-
--> $DIR/or_fun_call.rs:56:14
45+
error: use of `unwrap_or` followed by a call to `new`
46+
--> $DIR/or_fun_call.rs:56:5
4747
|
4848
LL | with_vec.unwrap_or(vec![]);
49-
| ^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| vec![])`
49+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_vec.unwrap_or_default()`
5050

5151
error: use of `unwrap_or` followed by a function call
52-
--> $DIR/or_fun_call.rs:61:21
52+
--> $DIR/or_fun_call.rs:59:21
5353
|
5454
LL | without_default.unwrap_or(Foo::new());
5555
| ^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(Foo::new)`
5656

5757
error: use of `or_insert` followed by a function call
58-
--> $DIR/or_fun_call.rs:64:19
58+
--> $DIR/or_fun_call.rs:62:19
5959
|
6060
LL | map.entry(42).or_insert(String::new());
6161
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `or_insert_with(String::new)`
6262

6363
error: use of `or_insert` followed by a function call
64-
--> $DIR/or_fun_call.rs:67:21
64+
--> $DIR/or_fun_call.rs:65:21
6565
|
6666
LL | btree.entry(42).or_insert(String::new());
6767
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `or_insert_with(String::new)`
6868

6969
error: use of `unwrap_or` followed by a function call
70-
--> $DIR/or_fun_call.rs:70:21
70+
--> $DIR/or_fun_call.rs:68:21
7171
|
7272
LL | let _ = stringy.unwrap_or("".to_owned());
7373
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| "".to_owned())`
7474

7575
error: use of `or` followed by a function call
76-
--> $DIR/or_fun_call.rs:95:35
76+
--> $DIR/or_fun_call.rs:93:35
7777
|
7878
LL | let _ = Some("a".to_string()).or(Some("b".to_string()));
7979
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `or_else(|| Some("b".to_string()))`

0 commit comments

Comments
 (0)