Add new lint to detect unnecessarily wrapped value
This commit is contained in:
parent
ad4f82997a
commit
a7ac441760
7 changed files with 318 additions and 0 deletions
47
tests/ui/unnecessary_wrap.fixed
Normal file
47
tests/ui/unnecessary_wrap.fixed
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
// run-rustfix
|
||||
#![warn(clippy::unnecessary_wrap)]
|
||||
#![allow(clippy::no_effect)]
|
||||
#![allow(clippy::needless_return)]
|
||||
#![allow(clippy::if_same_then_else)]
|
||||
|
||||
// should be linted
|
||||
fn func1(a: bool, b: bool) -> Option<i32> {
|
||||
if a && b {
|
||||
return Some(42);
|
||||
}
|
||||
if a {
|
||||
Some(-1);
|
||||
Some(2)
|
||||
} else {
|
||||
return Some(1337);
|
||||
}
|
||||
}
|
||||
|
||||
// public fns should not be linted
|
||||
pub fn func2(a: bool) -> Option<i32> {
|
||||
if a {
|
||||
Some(1)
|
||||
} else {
|
||||
Some(1)
|
||||
}
|
||||
}
|
||||
|
||||
// should not be linted
|
||||
fn func3(a: bool) -> Option<i32> {
|
||||
if a {
|
||||
Some(1)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
// should be linted
|
||||
fn func4() -> Option<i32> {
|
||||
1
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// method calls are not linted
|
||||
func1(true, true);
|
||||
func2(true);
|
||||
}
|
||||
47
tests/ui/unnecessary_wrap.rs
Normal file
47
tests/ui/unnecessary_wrap.rs
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
// run-rustfix
|
||||
#![warn(clippy::unnecessary_wrap)]
|
||||
#![allow(clippy::no_effect)]
|
||||
#![allow(clippy::needless_return)]
|
||||
#![allow(clippy::if_same_then_else)]
|
||||
|
||||
// should be linted
|
||||
fn func1(a: bool, b: bool) -> Option<i32> {
|
||||
if a && b {
|
||||
return Some(42);
|
||||
}
|
||||
if a {
|
||||
Some(-1);
|
||||
Some(2)
|
||||
} else {
|
||||
return Some(1337);
|
||||
}
|
||||
}
|
||||
|
||||
// public fns should not be linted
|
||||
pub fn func2(a: bool) -> Option<i32> {
|
||||
if a {
|
||||
Some(1)
|
||||
} else {
|
||||
Some(1)
|
||||
}
|
||||
}
|
||||
|
||||
// should not be linted
|
||||
fn func3(a: bool) -> Option<i32> {
|
||||
if a {
|
||||
Some(1)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
// should be linted
|
||||
fn func4() -> Option<i32> {
|
||||
Some(1)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// method calls are not linted
|
||||
func1(true, true);
|
||||
func2(true);
|
||||
}
|
||||
34
tests/ui/unnecessary_wrap.stderr
Normal file
34
tests/ui/unnecessary_wrap.stderr
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
error: this function unnecessarily wrapping data
|
||||
--> $DIR/unnecessary_wrap.rs:8:1
|
||||
|
|
||||
LL | / fn func1(a: bool, b: bool) -> Option<i32> {
|
||||
LL | | if a && b {
|
||||
LL | | return Some(42);
|
||||
LL | | }
|
||||
... |
|
||||
LL | | }
|
||||
LL | | }
|
||||
| |_^
|
||||
|
|
||||
= note: `-D clippy::unnecessary-wrap` implied by `-D warnings`
|
||||
help: factor this out to
|
||||
|
|
||||
LL | return 42;
|
||||
LL | }
|
||||
LL | if a {
|
||||
LL | Some(-1);
|
||||
LL | 2
|
||||
LL | } else {
|
||||
...
|
||||
|
||||
error: this function unnecessarily wrapping data
|
||||
--> $DIR/unnecessary_wrap.rs:39:1
|
||||
|
|
||||
LL | / fn func4() -> Option<i32> {
|
||||
LL | | Some(1)
|
||||
| | ------- help: factor this out to: `1`
|
||||
LL | | }
|
||||
| |_^
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue