Skip to content

Commit a1931dd

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 a1931dd

File tree

2 files changed

+30
-58
lines changed

2 files changed

+30
-58
lines changed

clippy_lints/src/unnecessary_wraps.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::borrow::Cow;
2+
13
use clippy_config::Conf;
24
use clippy_utils::diagnostics::span_lint_and_then;
35
use clippy_utils::source::snippet;
@@ -78,7 +80,7 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryWraps {
7880
fn_kind: FnKind<'tcx>,
7981
fn_decl: &FnDecl<'tcx>,
8082
body: &Body<'tcx>,
81-
span: Span,
83+
_span: Span,
8284
def_id: LocalDefId,
8385
) {
8486
// Abort if public function/method or closure.
@@ -147,19 +149,22 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryWraps {
147149
"remove the return type...".to_string(),
148150
// FIXME: we should instead get the span including the `->` and suggest an
149151
// empty string for this case.
150-
"()".to_string(),
151-
"...and then remove returned values",
152+
Cow::Borrowed("()"),
153+
Cow::Borrowed("...and then remove returned values"),
152154
)
153155
} else {
156+
let wrapper = if lang_item == OptionSome { "Some" } else { "Ok" };
154157
(
155158
format!("this function's return value is unnecessarily wrapped by `{return_type_label}`"),
156159
format!("remove `{return_type_label}` from the return type..."),
157-
inner_type.to_string(),
158-
"...and then change returning expressions",
160+
Cow::Owned(inner_type.to_string()),
161+
Cow::Owned(format!(
162+
"...and then remove the surrounding `{wrapper}()` from returning expressions"
163+
)),
159164
)
160165
};
161166

162-
span_lint_and_then(cx, UNNECESSARY_WRAPS, span, lint_msg, |diag| {
167+
span_lint_and_then(cx, UNNECESSARY_WRAPS, cx.tcx.def_span(def_id), lint_msg, |diag| {
163168
diag.span_suggestion(
164169
fn_decl.output.span(),
165170
return_type_sugg_msg,

tests/ui/unnecessary_wraps.stderr

+19-52
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)]`
@@ -16,7 +11,7 @@ help: remove `Option` from the return type...
1611
LL - fn func1(a: bool, b: bool) -> Option<i32> {
1712
LL + fn func1(a: bool, b: bool) -> i32 {
1813
|
19-
help: ...and then change returning expressions
14+
help: ...and then remove the surrounding `Some()` from returning expressions
2015
|
2116
LL ~ return 42;
2217
LL | }
@@ -30,21 +25,15 @@ 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
|
4433
LL - fn func2(a: bool, b: bool) -> Option<i32> {
4534
LL + fn func2(a: bool, b: bool) -> i32 {
4635
|
47-
help: ...and then change returning expressions
36+
help: ...and then remove the surrounding `Some()` from returning expressions
4837
|
4938
LL ~ return 10;
5039
LL | }
@@ -54,19 +43,15 @@ 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
|
6651
LL - fn func5() -> Option<i32> {
6752
LL + fn func5() -> i32 {
6853
|
69-
help: ...and then change returning expressions
54+
help: ...and then remove the surrounding `Some()` from returning expressions
7055
|
7156
LL - Some(1)
7257
LL + 1
@@ -75,19 +60,15 @@ 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
|
8768
LL - fn func7() -> Result<i32, ()> {
8869
LL + fn func7() -> i32 {
8970
|
90-
help: ...and then change returning expressions
71+
help: ...and then remove the surrounding `Ok()` from returning expressions
9172
|
9273
LL - Ok(1)
9374
LL + 1
@@ -96,19 +77,15 @@ 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
|
10885
LL - fn func12() -> Option<i32> {
10986
LL + fn func12() -> i32 {
11087
|
111-
help: ...and then change returning expressions
88+
help: ...and then remove the surrounding `Some()` from returning expressions
11289
|
11390
LL - Some(1)
11491
LL + 1
@@ -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)