Skip to content

Commit 8a62b6f

Browse files
committed
Auto merge of #9144 - alex-semenyuk:or_fun_call_span_fix, r=giraffate
Fix span for or_fun_call Closes #9033 changelog: [`or_fun_call`]: span points to the `unwrap_or` only instead of through the entire method chain expression
2 parents 7ea4592 + c3c4cda commit 8a62b6f

File tree

3 files changed

+36
-104
lines changed

3 files changed

+36
-104
lines changed

clippy_lints/src/methods/or_fun_call.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::eager_or_lazy::switch_to_lazy_eval;
3-
use clippy_utils::source::{snippet, snippet_with_applicability, snippet_with_macro_callsite};
3+
use clippy_utils::source::{snippet, snippet_with_macro_callsite};
44
use clippy_utils::ty::{implements_trait, match_type};
55
use clippy_utils::{contains_return, is_trait_item, last_path_segment, paths};
66
use if_chain::if_chain;
@@ -28,10 +28,10 @@ pub(super) fn check<'tcx>(
2828
cx: &LateContext<'_>,
2929
name: &str,
3030
fun: &hir::Expr<'_>,
31-
self_expr: &hir::Expr<'_>,
3231
arg: &hir::Expr<'_>,
3332
or_has_args: bool,
3433
span: Span,
34+
method_span: Span,
3535
) -> bool {
3636
let is_default_default = || is_trait_item(cx, fun, sym::Default);
3737

@@ -52,24 +52,14 @@ pub(super) fn check<'tcx>(
5252
|| (matches!(path, sym::new) && implements_default(arg, default_trait_id));
5353

5454
then {
55-
let mut applicability = Applicability::MachineApplicable;
56-
let hint = "unwrap_or_default()";
57-
let sugg_span = span;
58-
59-
let sugg: String = format!(
60-
"{}.{}",
61-
snippet_with_applicability(cx, self_expr.span, "..", &mut applicability),
62-
hint
63-
);
64-
6555
span_lint_and_sugg(
6656
cx,
6757
OR_FUN_CALL,
68-
sugg_span,
58+
method_span.with_hi(span.hi()),
6959
&format!("use of `{}` followed by a call to `{}`", name, path),
7060
"try this",
71-
sugg,
72-
applicability,
61+
"unwrap_or_default()".to_string(),
62+
Applicability::MachineApplicable,
7363
);
7464

7565
true
@@ -171,7 +161,7 @@ pub(super) fn check<'tcx>(
171161
match inner_arg.kind {
172162
hir::ExprKind::Call(fun, or_args) => {
173163
let or_has_args = !or_args.is_empty();
174-
if !check_unwrap_or_default(cx, name, fun, self_arg, arg, or_has_args, expr.span) {
164+
if !check_unwrap_or_default(cx, name, fun, arg, or_has_args, expr.span, method_span) {
175165
let fun_span = if or_has_args { None } else { Some(fun.span) };
176166
check_general_case(cx, name, method_span, self_arg, arg, expr.span, fun_span);
177167
}

tests/ui/or_fun_call.fixed

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ mod issue8239 {
185185
.reduce(|mut acc, f| {
186186
acc.push_str(&f);
187187
acc
188-
}).unwrap_or_default();
188+
})
189+
.unwrap_or_default();
189190
}
190191

191192
fn more_to_max_suggestion_highest_lines_1() {
@@ -197,7 +198,8 @@ mod issue8239 {
197198
let _ = "";
198199
acc.push_str(&f);
199200
acc
200-
}).unwrap_or_default();
201+
})
202+
.unwrap_or_default();
201203
}
202204

203205
fn equal_to_max_suggestion_highest_lines() {
@@ -208,7 +210,8 @@ mod issue8239 {
208210
let _ = "";
209211
acc.push_str(&f);
210212
acc
211-
}).unwrap_or_default();
213+
})
214+
.unwrap_or_default();
212215
}
213216

214217
fn less_than_max_suggestion_highest_lines() {
@@ -218,7 +221,8 @@ mod issue8239 {
218221
map.reduce(|mut acc, f| {
219222
acc.push_str(&f);
220223
acc
221-
}).unwrap_or_default();
224+
})
225+
.unwrap_or_default();
222226
}
223227
}
224228

tests/ui/or_fun_call.stderr

Lines changed: 22 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ LL | with_constructor.unwrap_or(make());
77
= note: `-D clippy::or-fun-call` implied by `-D warnings`
88

99
error: use of `unwrap_or` followed by a call to `new`
10-
--> $DIR/or_fun_call.rs:52:5
10+
--> $DIR/or_fun_call.rs:52:14
1111
|
1212
LL | with_new.unwrap_or(Vec::new());
13-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_new.unwrap_or_default()`
13+
| ^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_default()`
1414

1515
error: use of `unwrap_or` followed by a function call
1616
--> $DIR/or_fun_call.rs:55:21
@@ -31,16 +31,16 @@ LL | with_err_args.unwrap_or(Vec::with_capacity(12));
3131
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|_| Vec::with_capacity(12))`
3232

3333
error: use of `unwrap_or` followed by a call to `default`
34-
--> $DIR/or_fun_call.rs:64:5
34+
--> $DIR/or_fun_call.rs:64:24
3535
|
3636
LL | with_default_trait.unwrap_or(Default::default());
37-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_default_trait.unwrap_or_default()`
37+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_default()`
3838

3939
error: use of `unwrap_or` followed by a call to `default`
40-
--> $DIR/or_fun_call.rs:67:5
40+
--> $DIR/or_fun_call.rs:67:23
4141
|
4242
LL | with_default_type.unwrap_or(u64::default());
43-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_default_type.unwrap_or_default()`
43+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_default()`
4444

4545
error: use of `unwrap_or` followed by a function call
4646
--> $DIR/or_fun_call.rs:70:18
@@ -49,16 +49,16 @@ LL | self_default.unwrap_or(<FakeDefault>::default());
4949
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(<FakeDefault>::default)`
5050

5151
error: use of `unwrap_or` followed by a call to `default`
52-
--> $DIR/or_fun_call.rs:73:5
52+
--> $DIR/or_fun_call.rs:73:18
5353
|
5454
LL | real_default.unwrap_or(<FakeDefault as Default>::default());
55-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `real_default.unwrap_or_default()`
55+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_default()`
5656

5757
error: use of `unwrap_or` followed by a call to `new`
58-
--> $DIR/or_fun_call.rs:76:5
58+
--> $DIR/or_fun_call.rs:76:14
5959
|
6060
LL | with_vec.unwrap_or(vec![]);
61-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_vec.unwrap_or_default()`
61+
| ^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_default()`
6262

6363
error: use of `unwrap_or` followed by a function call
6464
--> $DIR/or_fun_call.rs:79:21
@@ -109,90 +109,28 @@ LL | None.unwrap_or( unsafe { ptr_to_ref(s) } );
109109
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| unsafe { ptr_to_ref(s) })`
110110

111111
error: use of `unwrap_or` followed by a call to `new`
112-
--> $DIR/or_fun_call.rs:182:9
113-
|
114-
LL | / frames
115-
LL | | .iter()
116-
LL | | .map(|f: &String| f.to_lowercase())
117-
LL | | .reduce(|mut acc, f| {
118-
... |
119-
LL | | })
120-
LL | | .unwrap_or(String::new());
121-
| |_____________________________________^
122-
|
123-
help: try this
124-
|
125-
LL ~ frames
126-
LL + .iter()
127-
LL + .map(|f: &String| f.to_lowercase())
128-
LL + .reduce(|mut acc, f| {
129-
LL + acc.push_str(&f);
130-
LL + acc
131-
LL ~ }).unwrap_or_default();
112+
--> $DIR/or_fun_call.rs:189:14
132113
|
114+
LL | .unwrap_or(String::new());
115+
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_default()`
133116

134117
error: use of `unwrap_or` followed by a call to `new`
135-
--> $DIR/or_fun_call.rs:195:9
136-
|
137-
LL | / iter.map(|f: &String| f.to_lowercase())
138-
LL | | .reduce(|mut acc, f| {
139-
LL | | let _ = "";
140-
LL | | let _ = "";
141-
... |
142-
LL | | })
143-
LL | | .unwrap_or(String::new());
144-
| |_____________________________________^
145-
|
146-
help: try this
147-
|
148-
LL ~ iter.map(|f: &String| f.to_lowercase())
149-
LL + .reduce(|mut acc, f| {
150-
LL + let _ = "";
151-
LL + let _ = "";
152-
LL + acc.push_str(&f);
153-
LL + acc
154-
LL ~ }).unwrap_or_default();
118+
--> $DIR/or_fun_call.rs:202:14
155119
|
120+
LL | .unwrap_or(String::new());
121+
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_default()`
156122

157123
error: use of `unwrap_or` followed by a call to `new`
158-
--> $DIR/or_fun_call.rs:208:9
159-
|
160-
LL | / iter.map(|f: &String| f.to_lowercase())
161-
LL | | .reduce(|mut acc, f| {
162-
LL | | let _ = "";
163-
LL | | acc.push_str(&f);
164-
LL | | acc
165-
LL | | })
166-
LL | | .unwrap_or(String::new());
167-
| |_____________________________________^
168-
|
169-
help: try this
170-
|
171-
LL ~ iter.map(|f: &String| f.to_lowercase())
172-
LL + .reduce(|mut acc, f| {
173-
LL + let _ = "";
174-
LL + acc.push_str(&f);
175-
LL + acc
176-
LL ~ }).unwrap_or_default();
124+
--> $DIR/or_fun_call.rs:214:14
177125
|
126+
LL | .unwrap_or(String::new());
127+
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_default()`
178128

179129
error: use of `unwrap_or` followed by a call to `new`
180-
--> $DIR/or_fun_call.rs:221:9
181-
|
182-
LL | / map.reduce(|mut acc, f| {
183-
LL | | acc.push_str(&f);
184-
LL | | acc
185-
LL | | })
186-
LL | | .unwrap_or(String::new());
187-
| |_________________________________^
188-
|
189-
help: try this
190-
|
191-
LL ~ map.reduce(|mut acc, f| {
192-
LL + acc.push_str(&f);
193-
LL + acc
194-
LL ~ }).unwrap_or_default();
130+
--> $DIR/or_fun_call.rs:225:10
195131
|
132+
LL | .unwrap_or(String::new());
133+
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_default()`
196134

197135
error: aborting due to 22 previous errors
198136

0 commit comments

Comments
 (0)