Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new lint unnecessary_wrap #6070

Merged
merged 29 commits into from
Nov 17, 2020
Merged
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
a7ac441
Add new lint to detect unnecessarily wrapped value
hkmatsumoto Sep 19, 2020
750c118
Add suggestion on type signatures
hkmatsumoto Sep 20, 2020
0335b8d
Fix lint example
hkmatsumoto Sep 20, 2020
0e9d227
Add test cases
hkmatsumoto Sep 20, 2020
ebdd4e2
Refactor code according to reivews
hkmatsumoto Sep 22, 2020
6a62390
Optout rustfix test
hkmatsumoto Sep 22, 2020
3ed8902
Fix typo
hkmatsumoto Sep 22, 2020
c775856
Call `diag.multipart_suggestion` instead
hkmatsumoto Sep 22, 2020
a433d46
Run rustfmt
hkmatsumoto Sep 22, 2020
cdb72df
Split lint suggestion into two
hkmatsumoto Sep 26, 2020
6b55f3f
Add test case
hkmatsumoto Sep 26, 2020
df0d565
Move `find_all_ret_expressions` into `utils`
hkmatsumoto Oct 2, 2020
eec7f5c
Update clippy_lints/src/unnecessary_wrap.rs
hkmatsumoto Oct 12, 2020
1bdac87
Improve lint message
hkmatsumoto Oct 12, 2020
8392bc7
Run `cargo dev fmt`
hkmatsumoto Oct 12, 2020
2f85aa7
Make lint skip closures
hkmatsumoto Oct 18, 2020
12474c6
Add support for methods
hkmatsumoto Oct 18, 2020
c5447eb
Make lint skip macros
hkmatsumoto Oct 18, 2020
c7692cf
Skip function with no exprs contained
hkmatsumoto Oct 18, 2020
30632fb
Allow this lint on lint tests
hkmatsumoto Oct 18, 2020
e998d61
Downgrade applicability to MaybeIncorrect
hkmatsumoto Oct 18, 2020
9d96311
Remove wildcard use
hkmatsumoto Oct 18, 2020
532d205
Skip functions in PartialOrd
hkmatsumoto Oct 18, 2020
bf46f78
Fix clippy error
hkmatsumoto Oct 18, 2020
86331a4
Update stderr files
hkmatsumoto Nov 2, 2020
4c8d248
Update stderr files
hkmatsumoto Nov 14, 2020
4e5c02e
Ignore trait implementations
hkmatsumoto Nov 14, 2020
1f577c0
Fix embarrassing grammatical error
hkmatsumoto Nov 14, 2020
c7445d7
Pluralize lint name
hkmatsumoto Nov 17, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Split lint suggestion into two
  • Loading branch information
hkmatsumoto committed Nov 17, 2020
commit cdb72df6f9f9d6946b0614b01e70bc9c46edfe89
41 changes: 22 additions & 19 deletions clippy_lints/src/unnecessary_wrap.rs
Original file line number Diff line number Diff line change
@@ -68,10 +68,10 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryWrap {
}
}

let path = if is_type_diagnostic_item(cx, return_ty(cx, hir_id), sym!(option_type)) {
&paths::OPTION_SOME
let (return_type, path) = if is_type_diagnostic_item(cx, return_ty(cx, hir_id), sym!(option_type)) {
hkmatsumoto marked this conversation as resolved.
Show resolved Hide resolved
("Option", &paths::OPTION_SOME)
} else if is_type_diagnostic_item(cx, return_ty(cx, hir_id), sym!(result_type)) {
&paths::RESULT_OK
("Result", &paths::RESULT_OK)
} else {
return;
};
@@ -98,23 +98,26 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryWrap {
UNNECESSARY_WRAP,
span,
"this function returns unnecessarily wrapping data",
hkmatsumoto marked this conversation as resolved.
Show resolved Hide resolved
move |diag| {
|diag| {
let inner_ty = return_ty(cx, hir_id)
.walk()
.skip(1) // skip `std::option::Option` or `std::result::Result`
.take(1) // take the first outermost inner type
.filter_map(|inner| match inner.unpack() {
GenericArgKind::Type(inner_ty) => Some(inner_ty.to_string()),
_ => None,
});
inner_ty.for_each(|inner_ty| {
diag.span_suggestion(
fn_decl.output.span(),
format!("remove `{}` from the return type...", return_type).as_str(),
inner_ty,
Applicability::MachineApplicable,
);
});
diag.multipart_suggestion(
"factor this out to",
suggs
.into_iter()
.chain({
let inner_ty = return_ty(cx, hir_id)
.walk()
.skip(1) // skip `std::option::Option` or `std::result::Result`
.take(1) // take the first outermost inner type
.filter_map(|inner| match inner.unpack() {
GenericArgKind::Type(inner_ty) => Some(inner_ty.to_string()),
_ => None,
});
inner_ty.map(|inner_ty| (fn_decl.output.span(), inner_ty))
})
.collect(),
"...and change the returning expressions",
suggs,
Applicability::MachineApplicable,
);
},
18 changes: 14 additions & 4 deletions tests/ui/unnecessary_wrap.stderr
Original file line number Diff line number Diff line change
@@ -11,14 +11,18 @@ LL | | }
| |_^
|
= note: `-D clippy::unnecessary-wrap` implied by `-D warnings`
help: factor this out to
help: remove `Option` from the return type...
|
LL | fn func1(a: bool, b: bool) -> i32 {
LL | if a && b {
| ^^^
help: ...and change the returning expressions
|
LL | return 42;
LL | }
LL | if a {
LL | Some(-1);
LL | 2
LL | } else {
...

error: this function returns unnecessarily wrapping data
@@ -29,9 +33,12 @@ LL | | Some(1)
LL | | }
| |_^
|
help: factor this out to
help: remove `Option` from the return type...
|
LL | fn func4() -> i32 {
| ^^^
help: ...and change the returning expressions
|
LL | 1
|

@@ -43,9 +50,12 @@ LL | | Ok(1)
LL | | }
| |_^
|
help: factor this out to
help: remove `Result` from the return type...
|
LL | fn func6() -> i32 {
| ^^^
help: ...and change the returning expressions
|
LL | 1
|