Skip to content

Commit 7800fc7

Browse files
committed
unnecessary_wraps: do not include the whole body in the lint span
Using the function declaration, by stripping the body, is enough to convey the lint message.
1 parent f60807d commit 7800fc7

File tree

2 files changed

+31
-57
lines changed

2 files changed

+31
-57
lines changed

clippy_lints/src/unnecessary_wraps.rs

+17-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_config::Conf;
22
use clippy_utils::diagnostics::span_lint_and_then;
3-
use clippy_utils::source::snippet;
3+
use clippy_utils::source::{HasSession, snippet, trim_span};
44
use clippy_utils::visitors::find_all_ret_expressions;
55
use clippy_utils::{contains_return, is_res_lang_ctor, path_res, return_ty};
66
use rustc_errors::Applicability;
@@ -159,15 +159,22 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryWraps {
159159
)
160160
};
161161

162-
span_lint_and_then(cx, UNNECESSARY_WRAPS, span, lint_msg, |diag| {
163-
diag.span_suggestion(
164-
fn_decl.output.span(),
165-
return_type_sugg_msg,
166-
return_type_sugg,
167-
Applicability::MaybeIncorrect,
168-
);
169-
diag.multipart_suggestion(body_sugg_msg, suggs, Applicability::MaybeIncorrect);
170-
});
162+
span_lint_and_then(
163+
cx,
164+
UNNECESSARY_WRAPS,
165+
// Do not include the whole body in the lint span
166+
trim_span(cx.sess().source_map(), span.until(body.value.span)),
167+
lint_msg,
168+
|diag| {
169+
diag.span_suggestion(
170+
fn_decl.output.span(),
171+
return_type_sugg_msg,
172+
return_type_sugg,
173+
Applicability::MaybeIncorrect,
174+
);
175+
diag.multipart_suggestion(body_sugg_msg, suggs, Applicability::MaybeIncorrect);
176+
},
177+
);
171178
}
172179
}
173180
}

tests/ui/unnecessary_wraps.stderr

+14-47
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
error: this function's return value is unnecessarily wrapped by `Option`
22
--> tests/ui/unnecessary_wraps.rs:9:1
33
|
4-
LL | / fn func1(a: bool, b: bool) -> Option<i32> {
5-
LL | |
6-
LL | |
7-
LL | | if a && b {
8-
... |
9-
LL | | }
10-
| |_^
4+
LL | fn func1(a: bool, b: bool) -> Option<i32> {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
116
|
127
= note: `-D clippy::unnecessary-wraps` implied by `-D warnings`
138
= help: to override `-D warnings` add `#[allow(clippy::unnecessary_wraps)]`
@@ -30,14 +25,8 @@ LL ~ return 1337;
3025
error: this function's return value is unnecessarily wrapped by `Option`
3126
--> tests/ui/unnecessary_wraps.rs:24:1
3227
|
33-
LL | / fn func2(a: bool, b: bool) -> Option<i32> {
34-
LL | |
35-
LL | |
36-
LL | | if a && b {
37-
... |
38-
LL | | if a { Some(20) } else { Some(30) }
39-
LL | | }
40-
| |_^
28+
LL | fn func2(a: bool, b: bool) -> Option<i32> {
29+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4130
|
4231
help: remove `Option` from the return type...
4332
|
@@ -54,12 +43,8 @@ LL ~ if a { 20 } else { 30 }
5443
error: this function's return value is unnecessarily wrapped by `Option`
5544
--> tests/ui/unnecessary_wraps.rs:44:1
5645
|
57-
LL | / fn func5() -> Option<i32> {
58-
LL | |
59-
LL | |
60-
LL | | Some(1)
61-
LL | | }
62-
| |_^
46+
LL | fn func5() -> Option<i32> {
47+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
6348
|
6449
help: remove `Option` from the return type...
6550
|
@@ -75,12 +60,8 @@ LL + 1
7560
error: this function's return value is unnecessarily wrapped by `Result`
7661
--> tests/ui/unnecessary_wraps.rs:56:1
7762
|
78-
LL | / fn func7() -> Result<i32, ()> {
79-
LL | |
80-
LL | |
81-
LL | | Ok(1)
82-
LL | | }
83-
| |_^
63+
LL | fn func7() -> Result<i32, ()> {
64+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8465
|
8566
help: remove `Result` from the return type...
8667
|
@@ -96,12 +77,8 @@ LL + 1
9677
error: this function's return value is unnecessarily wrapped by `Option`
9778
--> tests/ui/unnecessary_wraps.rs:86:5
9879
|
99-
LL | / fn func12() -> Option<i32> {
100-
LL | |
101-
LL | |
102-
LL | | Some(1)
103-
LL | | }
104-
| |_____^
80+
LL | fn func12() -> Option<i32> {
81+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
10582
|
10683
help: remove `Option` from the return type...
10784
|
@@ -117,13 +94,8 @@ LL + 1
11794
error: this function's return value is unnecessary
11895
--> tests/ui/unnecessary_wraps.rs:115:1
11996
|
120-
LL | / fn issue_6640_1(a: bool, b: bool) -> Option<()> {
121-
LL | |
122-
LL | |
123-
LL | | if a && b {
124-
... |
125-
LL | | }
126-
| |_^
97+
LL | fn issue_6640_1(a: bool, b: bool) -> Option<()> {
98+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12799
|
128100
help: remove the return type...
129101
|
@@ -144,13 +116,8 @@ LL ~ return ;
144116
error: this function's return value is unnecessary
145117
--> tests/ui/unnecessary_wraps.rs:130:1
146118
|
147-
LL | / fn issue_6640_2(a: bool, b: bool) -> Result<(), i32> {
148-
LL | |
149-
LL | |
150-
LL | | if a && b {
151-
... |
152-
LL | | }
153-
| |_^
119+
LL | fn issue_6640_2(a: bool, b: bool) -> Result<(), i32> {
120+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
154121
|
155122
help: remove the return type...
156123
|

0 commit comments

Comments
 (0)