Skip to content

Commit b85f632

Browse files
committed
Auto merge of rust-lang#13528 - zvavybir:master, r=Alexendoo
Improved wording of or_fun_call lint The current wording (e.g. ``use of `ok_or` followed by a function call``) is potentially confusing (at least it confused me) by suggesting that the function that follows the (in this case) `ok_or` is the problem and not the function that is an argument to it. The code in my program that triggered the confusing message is the following: ```rust let file_id = buf .lines() .next() .ok_or(( InternalError::ProblemReadingFromInbox, anyhow!("No first line in inbox response ({file:?}): {buf:?}"), )) .html_context(stream, lang)?; ``` I thought that `html_context` was the problem and that I should do something along the following lines: ```rust let file_id = buf .lines() .next() .ok_or_else( ( InternalError::ProblemReadingFromInbox, anyhow!("No first line in inbox response ({file:?}): {buf:?}"), ), html_context(stream, lang), )? ``` This is of course wrong. My confusion was only cleared up through the help message indicating what I should try instead. If someone has a better idea of a replacement wording (currently e.g. ``` function call inside of `ok_or` ```), I'm all ears. changelog: none
2 parents 6f1def7 + 8c46c49 commit b85f632

9 files changed

+54
-54
lines changed

clippy_lints/src/methods/expect_fun_call.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ pub(super) fn check<'tcx>(
143143
cx,
144144
EXPECT_FUN_CALL,
145145
span_replace_word,
146-
format!("use of `{name}` followed by a function call"),
146+
format!("function call inside of `{name}`"),
147147
"try",
148148
format!("unwrap_or_else({closure_args} panic!({sugg}))"),
149149
applicability,
@@ -161,7 +161,7 @@ pub(super) fn check<'tcx>(
161161
cx,
162162
EXPECT_FUN_CALL,
163163
span_replace_word,
164-
format!("use of `{name}` followed by a function call"),
164+
format!("function call inside of `{name}`"),
165165
"try",
166166
format!("unwrap_or_else({closure_args} {{ panic!(\"{{}}\", {arg_root_snippet}) }})"),
167167
applicability,

clippy_lints/src/methods/or_fun_call.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ pub(super) fn check<'tcx>(
183183
cx,
184184
OR_FUN_CALL,
185185
span_replace_word,
186-
format!("use of `{name}` followed by a function call"),
186+
format!("function call inside of `{name}`"),
187187
"try",
188188
format!("{name}_{suffix}({sugg})"),
189189
app,

tests/ui/expect_fun_call.stderr

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: use of `expect` followed by a function call
1+
error: function call inside of `expect`
22
--> tests/ui/expect_fun_call.rs:35:26
33
|
44
LL | with_none_and_format.expect(&format!("Error {}: fake error", error_code));
@@ -7,85 +7,85 @@ LL | with_none_and_format.expect(&format!("Error {}: fake error", error_code
77
= note: `-D clippy::expect-fun-call` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::expect_fun_call)]`
99

10-
error: use of `expect` followed by a function call
10+
error: function call inside of `expect`
1111
--> tests/ui/expect_fun_call.rs:38:26
1212
|
1313
LL | with_none_and_as_str.expect(format!("Error {}: fake error", error_code).as_str());
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(|| panic!("Error {}: fake error", error_code))`
1515

16-
error: use of `expect` followed by a function call
16+
error: function call inside of `expect`
1717
--> tests/ui/expect_fun_call.rs:41:37
1818
|
1919
LL | with_none_and_format_with_macro.expect(format!("Error {}: fake error", one!()).as_str());
2020
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(|| panic!("Error {}: fake error", one!()))`
2121

22-
error: use of `expect` followed by a function call
22+
error: function call inside of `expect`
2323
--> tests/ui/expect_fun_call.rs:51:25
2424
|
2525
LL | with_err_and_format.expect(&format!("Error {}: fake error", error_code));
2626
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(|_| panic!("Error {}: fake error", error_code))`
2727

28-
error: use of `expect` followed by a function call
28+
error: function call inside of `expect`
2929
--> tests/ui/expect_fun_call.rs:54:25
3030
|
3131
LL | with_err_and_as_str.expect(format!("Error {}: fake error", error_code).as_str());
3232
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(|_| panic!("Error {}: fake error", error_code))`
3333

34-
error: use of `expect` followed by a function call
34+
error: function call inside of `expect`
3535
--> tests/ui/expect_fun_call.rs:66:17
3636
|
3737
LL | Some("foo").expect(format!("{} {}", 1, 2).as_ref());
3838
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(|| panic!("{} {}", 1, 2))`
3939

40-
error: use of `expect` followed by a function call
40+
error: function call inside of `expect`
4141
--> tests/ui/expect_fun_call.rs:87:21
4242
|
4343
LL | Some("foo").expect(&get_string());
4444
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(|| { panic!("{}", get_string()) })`
4545

46-
error: use of `expect` followed by a function call
46+
error: function call inside of `expect`
4747
--> tests/ui/expect_fun_call.rs:88:21
4848
|
4949
LL | Some("foo").expect(get_string().as_ref());
5050
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(|| { panic!("{}", get_string()) })`
5151

52-
error: use of `expect` followed by a function call
52+
error: function call inside of `expect`
5353
--> tests/ui/expect_fun_call.rs:89:21
5454
|
5555
LL | Some("foo").expect(get_string().as_str());
5656
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(|| { panic!("{}", get_string()) })`
5757

58-
error: use of `expect` followed by a function call
58+
error: function call inside of `expect`
5959
--> tests/ui/expect_fun_call.rs:91:21
6060
|
6161
LL | Some("foo").expect(get_static_str());
6262
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(|| { panic!("{}", get_static_str()) })`
6363

64-
error: use of `expect` followed by a function call
64+
error: function call inside of `expect`
6565
--> tests/ui/expect_fun_call.rs:92:21
6666
|
6767
LL | Some("foo").expect(get_non_static_str(&0));
6868
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(|| { panic!("{}", get_non_static_str(&0).to_string()) })`
6969

70-
error: use of `expect` followed by a function call
70+
error: function call inside of `expect`
7171
--> tests/ui/expect_fun_call.rs:96:16
7272
|
7373
LL | Some(true).expect(&format!("key {}, {}", 1, 2));
7474
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(|| panic!("key {}, {}", 1, 2))`
7575

76-
error: use of `expect` followed by a function call
76+
error: function call inside of `expect`
7777
--> tests/ui/expect_fun_call.rs:102:17
7878
|
7979
LL | opt_ref.expect(&format!("{:?}", opt_ref));
8080
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(|| panic!("{:?}", opt_ref))`
8181

82-
error: use of `expect` followed by a function call
82+
error: function call inside of `expect`
8383
--> tests/ui/expect_fun_call.rs:106:20
8484
|
8585
LL | format_capture.expect(&format!("{error_code}"));
8686
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(|| panic!("{error_code}"))`
8787

88-
error: use of `expect` followed by a function call
88+
error: function call inside of `expect`
8989
--> tests/ui/expect_fun_call.rs:109:30
9090
|
9191
LL | format_capture_and_value.expect(&format!("{error_code}, {}", 1));

tests/ui/or_fun_call.fixed

+5-5
Original file line numberDiff line numberDiff line change
@@ -341,18 +341,18 @@ fn fn_call_in_nested_expr() {
341341
}
342342
let opt: Option<i32> = Some(1);
343343

344-
//~v ERROR: use of `unwrap_or` followed by a function call
344+
//~v ERROR: function call inside of `unwrap_or`
345345
let _ = opt.unwrap_or_else(f); // suggest `.unwrap_or_else(f)`
346346
//
347-
//~v ERROR: use of `unwrap_or` followed by a function call
347+
//~v ERROR: function call inside of `unwrap_or`
348348
let _ = opt.unwrap_or_else(|| f() + 1); // suggest `.unwrap_or_else(|| f() + 1)`
349349
//
350-
//~v ERROR: use of `unwrap_or` followed by a function call
350+
//~v ERROR: function call inside of `unwrap_or`
351351
let _ = opt.unwrap_or_else(|| {
352352
let x = f();
353353
x + 1
354354
});
355-
//~v ERROR: use of `map_or` followed by a function call
355+
//~v ERROR: function call inside of `map_or`
356356
let _ = opt.map_or_else(|| f() + 1, |v| v); // suggest `.map_or_else(|| f() + 1, |v| v)`
357357
//
358358
//~v ERROR: use of `unwrap_or` to construct default value
@@ -361,7 +361,7 @@ fn fn_call_in_nested_expr() {
361361
let opt_foo = Some(Foo {
362362
val: String::from("123"),
363363
});
364-
//~v ERROR: use of `unwrap_or` followed by a function call
364+
//~v ERROR: function call inside of `unwrap_or`
365365
let _ = opt_foo.unwrap_or_else(|| Foo { val: String::default() });
366366
}
367367

tests/ui/or_fun_call.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -341,18 +341,18 @@ fn fn_call_in_nested_expr() {
341341
}
342342
let opt: Option<i32> = Some(1);
343343

344-
//~v ERROR: use of `unwrap_or` followed by a function call
344+
//~v ERROR: function call inside of `unwrap_or`
345345
let _ = opt.unwrap_or({ f() }); // suggest `.unwrap_or_else(f)`
346346
//
347-
//~v ERROR: use of `unwrap_or` followed by a function call
347+
//~v ERROR: function call inside of `unwrap_or`
348348
let _ = opt.unwrap_or(f() + 1); // suggest `.unwrap_or_else(|| f() + 1)`
349349
//
350-
//~v ERROR: use of `unwrap_or` followed by a function call
350+
//~v ERROR: function call inside of `unwrap_or`
351351
let _ = opt.unwrap_or({
352352
let x = f();
353353
x + 1
354354
});
355-
//~v ERROR: use of `map_or` followed by a function call
355+
//~v ERROR: function call inside of `map_or`
356356
let _ = opt.map_or(f() + 1, |v| v); // suggest `.map_or_else(|| f() + 1, |v| v)`
357357
//
358358
//~v ERROR: use of `unwrap_or` to construct default value
@@ -361,7 +361,7 @@ fn fn_call_in_nested_expr() {
361361
let opt_foo = Some(Foo {
362362
val: String::from("123"),
363363
});
364-
//~v ERROR: use of `unwrap_or` followed by a function call
364+
//~v ERROR: function call inside of `unwrap_or`
365365
let _ = opt_foo.unwrap_or(Foo { val: String::default() });
366366
}
367367

tests/ui/or_fun_call.stderr

+20-20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: use of `unwrap_or` followed by a function call
1+
error: function call inside of `unwrap_or`
22
--> tests/ui/or_fun_call.rs:52:22
33
|
44
LL | with_constructor.unwrap_or(make());
@@ -16,19 +16,19 @@ LL | with_new.unwrap_or(Vec::new());
1616
= note: `-D clippy::unwrap-or-default` implied by `-D warnings`
1717
= help: to override `-D warnings` add `#[allow(clippy::unwrap_or_default)]`
1818

19-
error: use of `unwrap_or` followed by a function call
19+
error: function call inside of `unwrap_or`
2020
--> tests/ui/or_fun_call.rs:58:21
2121
|
2222
LL | with_const_args.unwrap_or(Vec::with_capacity(12));
2323
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(|| Vec::with_capacity(12))`
2424

25-
error: use of `unwrap_or` followed by a function call
25+
error: function call inside of `unwrap_or`
2626
--> tests/ui/or_fun_call.rs:61:14
2727
|
2828
LL | with_err.unwrap_or(make());
2929
| ^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(|_| make())`
3030

31-
error: use of `unwrap_or` followed by a function call
31+
error: function call inside of `unwrap_or`
3232
--> tests/ui/or_fun_call.rs:64:19
3333
|
3434
LL | with_err_args.unwrap_or(Vec::with_capacity(12));
@@ -46,7 +46,7 @@ error: use of `unwrap_or` to construct default value
4646
LL | with_default_type.unwrap_or(u64::default());
4747
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
4848

49-
error: use of `unwrap_or` followed by a function call
49+
error: function call inside of `unwrap_or`
5050
--> tests/ui/or_fun_call.rs:73:18
5151
|
5252
LL | self_default.unwrap_or(<FakeDefault>::default());
@@ -64,7 +64,7 @@ error: use of `unwrap_or` to construct default value
6464
LL | with_vec.unwrap_or(vec![]);
6565
| ^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
6666

67-
error: use of `unwrap_or` followed by a function call
67+
error: function call inside of `unwrap_or`
6868
--> tests/ui/or_fun_call.rs:82:21
6969
|
7070
LL | without_default.unwrap_or(Foo::new());
@@ -100,55 +100,55 @@ error: use of `unwrap_or` to construct default value
100100
LL | let _ = stringy.unwrap_or(String::new());
101101
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
102102

103-
error: use of `ok_or` followed by a function call
103+
error: function call inside of `ok_or`
104104
--> tests/ui/or_fun_call.rs:101:17
105105
|
106106
LL | let _ = opt.ok_or(format!("{} world.", hello));
107107
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `ok_or_else(|| format!("{} world.", hello))`
108108

109-
error: use of `unwrap_or` followed by a function call
109+
error: function call inside of `unwrap_or`
110110
--> tests/ui/or_fun_call.rs:105:21
111111
|
112112
LL | let _ = Some(1).unwrap_or(map[&1]);
113113
| ^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(|| map[&1])`
114114

115-
error: use of `unwrap_or` followed by a function call
115+
error: function call inside of `unwrap_or`
116116
--> tests/ui/or_fun_call.rs:107:21
117117
|
118118
LL | let _ = Some(1).unwrap_or(map[&1]);
119119
| ^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(|| map[&1])`
120120

121-
error: use of `or` followed by a function call
121+
error: function call inside of `or`
122122
--> tests/ui/or_fun_call.rs:131:35
123123
|
124124
LL | let _ = Some("a".to_string()).or(Some("b".to_string()));
125125
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `or_else(|| Some("b".to_string()))`
126126

127-
error: use of `unwrap_or` followed by a function call
127+
error: function call inside of `unwrap_or`
128128
--> tests/ui/or_fun_call.rs:170:14
129129
|
130130
LL | None.unwrap_or(ptr_to_ref(s));
131131
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(|| ptr_to_ref(s))`
132132

133-
error: use of `unwrap_or` followed by a function call
133+
error: function call inside of `unwrap_or`
134134
--> tests/ui/or_fun_call.rs:176:14
135135
|
136136
LL | None.unwrap_or(unsafe { ptr_to_ref(s) });
137137
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(|| unsafe { ptr_to_ref(s) })`
138138

139-
error: use of `unwrap_or` followed by a function call
139+
error: function call inside of `unwrap_or`
140140
--> tests/ui/or_fun_call.rs:178:14
141141
|
142142
LL | None.unwrap_or( unsafe { ptr_to_ref(s) } );
143143
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(|| unsafe { ptr_to_ref(s) })`
144144

145-
error: use of `map_or` followed by a function call
145+
error: function call inside of `map_or`
146146
--> tests/ui/or_fun_call.rs:253:25
147147
|
148148
LL | let _ = Some(4).map_or(g(), |v| v);
149149
| ^^^^^^^^^^^^^^^^^^ help: try: `map_or_else(g, |v| v)`
150150

151-
error: use of `map_or` followed by a function call
151+
error: function call inside of `map_or`
152152
--> tests/ui/or_fun_call.rs:254:25
153153
|
154154
LL | let _ = Some(4).map_or(g(), f);
@@ -196,19 +196,19 @@ error: use of `unwrap_or_else` to construct default value
196196
LL | let _ = stringy.unwrap_or_else(String::new);
197197
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
198198

199-
error: use of `unwrap_or` followed by a function call
199+
error: function call inside of `unwrap_or`
200200
--> tests/ui/or_fun_call.rs:345:17
201201
|
202202
LL | let _ = opt.unwrap_or({ f() }); // suggest `.unwrap_or_else(f)`
203203
| ^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(f)`
204204

205-
error: use of `unwrap_or` followed by a function call
205+
error: function call inside of `unwrap_or`
206206
--> tests/ui/or_fun_call.rs:348:17
207207
|
208208
LL | let _ = opt.unwrap_or(f() + 1); // suggest `.unwrap_or_else(|| f() + 1)`
209209
| ^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(|| f() + 1)`
210210

211-
error: use of `unwrap_or` followed by a function call
211+
error: function call inside of `unwrap_or`
212212
--> tests/ui/or_fun_call.rs:351:17
213213
|
214214
LL | let _ = opt.unwrap_or({
@@ -226,7 +226,7 @@ LL + x + 1
226226
LL ~ });
227227
|
228228

229-
error: use of `map_or` followed by a function call
229+
error: function call inside of `map_or`
230230
--> tests/ui/or_fun_call.rs:356:17
231231
|
232232
LL | let _ = opt.map_or(f() + 1, |v| v); // suggest `.map_or_else(|| f() + 1, |v| v)`
@@ -238,7 +238,7 @@ error: use of `unwrap_or` to construct default value
238238
LL | let _ = opt.unwrap_or({ i32::default() });
239239
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
240240

241-
error: use of `unwrap_or` followed by a function call
241+
error: function call inside of `unwrap_or`
242242
--> tests/ui/or_fun_call.rs:365:21
243243
|
244244
LL | let _ = opt_foo.unwrap_or(Foo { val: String::default() });

tests/ui/unwrap_or.fixed

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
fn main() {
55
let s = Some(String::from("test string")).unwrap_or_else(|| "Fail".to_string()).len();
6-
//~^ ERROR: use of `unwrap_or` followed by a function call
6+
//~^ ERROR: function call inside of `unwrap_or`
77
//~| NOTE: `-D clippy::or-fun-call` implied by `-D warnings`
88
}
99

1010
fn new_lines() {
1111
let s = Some(String::from("test string")).unwrap_or_else(|| "Fail".to_string()).len();
12-
//~^ ERROR: use of `unwrap_or` followed by a function call
12+
//~^ ERROR: function call inside of `unwrap_or`
1313
}

tests/ui/unwrap_or.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
fn main() {
55
let s = Some(String::from("test string")).unwrap_or("Fail".to_string()).len();
6-
//~^ ERROR: use of `unwrap_or` followed by a function call
6+
//~^ ERROR: function call inside of `unwrap_or`
77
//~| NOTE: `-D clippy::or-fun-call` implied by `-D warnings`
88
}
99

1010
fn new_lines() {
1111
let s = Some(String::from("test string")).unwrap_or("Fail".to_string()).len();
12-
//~^ ERROR: use of `unwrap_or` followed by a function call
12+
//~^ ERROR: function call inside of `unwrap_or`
1313
}

tests/ui/unwrap_or.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: use of `unwrap_or` followed by a function call
1+
error: function call inside of `unwrap_or`
22
--> tests/ui/unwrap_or.rs:5:47
33
|
44
LL | let s = Some(String::from("test string")).unwrap_or("Fail".to_string()).len();
@@ -7,7 +7,7 @@ LL | let s = Some(String::from("test string")).unwrap_or("Fail".to_string())
77
= note: `-D clippy::or-fun-call` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::or_fun_call)]`
99

10-
error: use of `unwrap_or` followed by a function call
10+
error: function call inside of `unwrap_or`
1111
--> tests/ui/unwrap_or.rs:11:47
1212
|
1313
LL | let s = Some(String::from("test string")).unwrap_or("Fail".to_string()).len();

0 commit comments

Comments
 (0)