Changed fn body suggestion msg

This commit is contained in:
Pierre-Andre Gagnon 2021-02-18 17:32:55 -05:00
parent 6165cccf7e
commit a78271b861
3 changed files with 31 additions and 15 deletions

View File

@ -131,11 +131,12 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryWraps {
});
if can_sugg && !suggs.is_empty() {
let (lint_msg, return_type_suggestion_msg, return_type_suggestion) = if inner_type.is_unit() {
let (lint_msg, return_type_sugg_msg, return_type_sugg, body_sugg_msg) = if inner_type.is_unit() {
(
"this function's return value is unnecessary".to_string(),
"remove the return type...".to_string(),
snippet(cx, fn_decl.output.span(), "..").to_string(),
"...and then remove returned values",
)
} else {
(
@ -145,21 +146,18 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryWraps {
),
format!("remove `{}` from the return type...", return_type_label),
inner_type.to_string(),
"...and then change returning expressions",
)
};
span_lint_and_then(cx, UNNECESSARY_WRAPS, span, lint_msg.as_str(), |diag| {
diag.span_suggestion(
fn_decl.output.span(),
return_type_suggestion_msg.as_str(),
return_type_suggestion,
Applicability::MaybeIncorrect,
);
diag.multipart_suggestion(
"...and then change the returning expressions",
suggs,
return_type_sugg_msg.as_str(),
return_type_sugg,
Applicability::MaybeIncorrect,
);
diag.multipart_suggestion(body_sugg_msg, suggs, Applicability::MaybeIncorrect);
});
}
}

View File

@ -141,6 +141,24 @@ fn issue_6640_2(a: bool, b: bool) -> Result<(), i32> {
}
}
// should not be linted
fn issue_6640_3() -> Option<()> {
if true {
Some(())
} else {
None
}
}
// should not be linted
fn issue_6640_4() -> Result<(), ()> {
if true {
Ok(())
} else {
Err(())
}
}
fn main() {
// method calls are not linted
func1(true, true);

View File

@ -15,7 +15,7 @@ help: remove `Option` from the return type...
|
LL | fn func1(a: bool, b: bool) -> i32 {
| ^^^
help: ...and then change the returning expressions
help: ...and then change returning expressions
|
LL | return 42;
LL | }
@ -41,7 +41,7 @@ help: remove `Option` from the return type...
|
LL | fn func2(a: bool, b: bool) -> i32 {
| ^^^
help: ...and then change the returning expressions
help: ...and then change returning expressions
|
LL | return 10;
LL | }
@ -63,7 +63,7 @@ help: remove `Option` from the return type...
|
LL | fn func5() -> i32 {
| ^^^
help: ...and then change the returning expressions
help: ...and then change returning expressions
|
LL | 1
|
@ -80,7 +80,7 @@ help: remove `Result` from the return type...
|
LL | fn func7() -> i32 {
| ^^^
help: ...and then change the returning expressions
help: ...and then change returning expressions
|
LL | 1
|
@ -97,7 +97,7 @@ help: remove `Option` from the return type...
|
LL | fn func12() -> i32 {
| ^^^
help: ...and then change the returning expressions
help: ...and then change returning expressions
|
LL | 1
|
@ -118,7 +118,7 @@ help: remove the return type...
|
LL | fn issue_6640_1(a: bool, b: bool) -> Option<()> {
| ^^^^^^^^^^
help: ...and then change the returning expressions
help: ...and then remove returned values
|
LL | return ;
LL | }
@ -144,7 +144,7 @@ help: remove the return type...
|
LL | fn issue_6640_2(a: bool, b: bool) -> Result<(), i32> {
| ^^^^^^^^^^^^^^^
help: ...and then change the returning expressions
help: ...and then remove returned values
|
LL | return ;
LL | }