Skip to content

Commit 3e294b2

Browse files
committed
Revert "or_fn_call: ignore nullary associated const fns"
This reverts commit 7a66e65.
1 parent 29b12f2 commit 3e294b2

File tree

4 files changed

+29
-27
lines changed

4 files changed

+29
-27
lines changed

clippy_lints/src/utils/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,7 @@ pub fn is_ctor_or_promotable_const_function(cx: &LateContext<'_>, expr: &Expr<'_
893893
return match res {
894894
def::Res::Def(DefKind::Variant | DefKind::Ctor(..), ..) => true,
895895
// FIXME: check the constness of the arguments, see https://github.com/rust-lang/rust-clippy/pull/5682#issuecomment-638681210
896-
def::Res::Def(DefKind::Fn | DefKind::AssocFn, def_id) if has_no_arguments(cx, def_id) => {
896+
def::Res::Def(DefKind::Fn, def_id) if has_no_arguments(cx, def_id) => {
897897
const_eval::is_const_fn(cx.tcx, def_id)
898898
},
899899
def::Res::Def(_, def_id) => cx.tcx.is_promotable_const_fn(def_id),

tests/ui/or_fun_call.fixed

+6-11
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ fn or_fun_call() {
5858
let without_default = Some(Foo);
5959
without_default.unwrap_or_else(Foo::new);
6060

61+
let mut map = HashMap::<u64, String>::new();
62+
map.entry(42).or_insert_with(String::new);
63+
64+
let mut btree = BTreeMap::<u64, String>::new();
65+
btree.entry(42).or_insert_with(String::new);
66+
6167
let stringy = Some(String::from(""));
6268
let _ = stringy.unwrap_or_else(|| "".to_owned());
6369

@@ -116,17 +122,6 @@ pub fn skip_const_fn_with_no_args() {
116122
Some(42)
117123
}
118124
let _ = None.or(foo());
119-
120-
// See issue #5693.
121-
let mut map = std::collections::HashMap::new();
122-
map.insert(1, vec![1]);
123-
map.entry(1).or_insert(vec![]);
124-
125-
let mut map = HashMap::<u64, String>::new();
126-
map.entry(42).or_insert(String::new());
127-
128-
let mut btree = BTreeMap::<u64, String>::new();
129-
btree.entry(42).or_insert(String::new());
130125
}
131126

132127
fn main() {}

tests/ui/or_fun_call.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ fn or_fun_call() {
5858
let without_default = Some(Foo);
5959
without_default.unwrap_or(Foo::new());
6060

61+
let mut map = HashMap::<u64, String>::new();
62+
map.entry(42).or_insert(String::new());
63+
64+
let mut btree = BTreeMap::<u64, String>::new();
65+
btree.entry(42).or_insert(String::new());
66+
6167
let stringy = Some(String::from(""));
6268
let _ = stringy.unwrap_or("".to_owned());
6369

@@ -116,17 +122,6 @@ pub fn skip_const_fn_with_no_args() {
116122
Some(42)
117123
}
118124
let _ = None.or(foo());
119-
120-
// See issue #5693.
121-
let mut map = std::collections::HashMap::new();
122-
map.insert(1, vec![1]);
123-
map.entry(1).or_insert(vec![]);
124-
125-
let mut map = HashMap::<u64, String>::new();
126-
map.entry(42).or_insert(String::new());
127-
128-
let mut btree = BTreeMap::<u64, String>::new();
129-
btree.entry(42).or_insert(String::new());
130125
}
131126

132127
fn main() {}

tests/ui/or_fun_call.stderr

+16-4
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,35 @@ error: use of `unwrap_or` followed by a function call
6060
LL | without_default.unwrap_or(Foo::new());
6161
| ^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(Foo::new)`
6262

63+
error: use of `or_insert` followed by a function call
64+
--> $DIR/or_fun_call.rs:62:19
65+
|
66+
LL | map.entry(42).or_insert(String::new());
67+
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `or_insert_with(String::new)`
68+
69+
error: use of `or_insert` followed by a function call
70+
--> $DIR/or_fun_call.rs:65:21
71+
|
72+
LL | btree.entry(42).or_insert(String::new());
73+
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `or_insert_with(String::new)`
74+
6375
error: use of `unwrap_or` followed by a function call
64-
--> $DIR/or_fun_call.rs:62:21
76+
--> $DIR/or_fun_call.rs:68:21
6577
|
6678
LL | let _ = stringy.unwrap_or("".to_owned());
6779
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| "".to_owned())`
6880

6981
error: use of `or` followed by a function call
70-
--> $DIR/or_fun_call.rs:87:35
82+
--> $DIR/or_fun_call.rs:93:35
7183
|
7284
LL | let _ = Some("a".to_string()).or(Some("b".to_string()));
7385
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `or_else(|| Some("b".to_string()))`
7486

7587
error: use of `or` followed by a function call
76-
--> $DIR/or_fun_call.rs:91:10
88+
--> $DIR/or_fun_call.rs:97:10
7789
|
7890
LL | .or(Some(Bar(b, Duration::from_secs(2))));
7991
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `or_else(|| Some(Bar(b, Duration::from_secs(2))))`
8092

81-
error: aborting due to 13 previous errors
93+
error: aborting due to 15 previous errors
8294

0 commit comments

Comments
 (0)