Skip to content

Commit 7a66e65

Browse files
committed
or_fn_call: ignore nullary associated const fns
1 parent c88c614 commit 7a66e65

File tree

4 files changed

+27
-29
lines changed

4 files changed

+27
-29
lines changed

clippy_lints/src/utils/mod.rs

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

tests/ui/or_fun_call.fixed

+11-6
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,6 @@ 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-
6761
let stringy = Some(String::from(""));
6862
let _ = stringy.unwrap_or_else(|| "".to_owned());
6963

@@ -122,6 +116,17 @@ pub fn skip_const_fn_with_no_args() {
122116
Some(42)
123117
}
124118
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());
125130
}
126131

127132
fn main() {}

tests/ui/or_fun_call.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,6 @@ 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-
6761
let stringy = Some(String::from(""));
6862
let _ = stringy.unwrap_or("".to_owned());
6963

@@ -122,6 +116,17 @@ pub fn skip_const_fn_with_no_args() {
122116
Some(42)
123117
}
124118
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());
125130
}
126131

127132
fn main() {}

tests/ui/or_fun_call.stderr

+4-16
Original file line numberDiff line numberDiff line change
@@ -60,35 +60,23 @@ 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-
7563
error: use of `unwrap_or` followed by a function call
76-
--> $DIR/or_fun_call.rs:68:21
64+
--> $DIR/or_fun_call.rs:62:21
7765
|
7866
LL | let _ = stringy.unwrap_or("".to_owned());
7967
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| "".to_owned())`
8068

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

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

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

0 commit comments

Comments
 (0)