diff options
| author | Hirochika Matsumoto <matsujika@gmail.com> | 2020-09-20 02:03:14 +0900 |
|---|---|---|
| committer | Hirochika Matsumoto <matsujika@gmail.com> | 2020-11-18 01:28:37 +0900 |
| commit | a7ac441760ae034ff7401439b38da821f4e2df3a (patch) | |
| tree | 9b3bf79c31cb587159e0697f10502fcf37cc7a75 /tests | |
| parent | ad4f82997a94cc91723daae14889f21428e65472 (diff) | |
| download | rust-a7ac441760ae034ff7401439b38da821f4e2df3a.tar.gz rust-a7ac441760ae034ff7401439b38da821f4e2df3a.zip | |
Add new lint to detect unnecessarily wrapped value
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/ui/unnecessary_wrap.fixed | 47 | ||||
| -rw-r--r-- | tests/ui/unnecessary_wrap.rs | 47 | ||||
| -rw-r--r-- | tests/ui/unnecessary_wrap.stderr | 34 |
3 files changed, 128 insertions, 0 deletions
diff --git a/tests/ui/unnecessary_wrap.fixed b/tests/ui/unnecessary_wrap.fixed new file mode 100644 index 00000000000..1657a3173db --- /dev/null +++ b/tests/ui/unnecessary_wrap.fixed @@ -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); +} diff --git a/tests/ui/unnecessary_wrap.rs b/tests/ui/unnecessary_wrap.rs new file mode 100644 index 00000000000..edf41dad790 --- /dev/null +++ b/tests/ui/unnecessary_wrap.rs @@ -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); +} diff --git a/tests/ui/unnecessary_wrap.stderr b/tests/ui/unnecessary_wrap.stderr new file mode 100644 index 00000000000..8473bd81839 --- /dev/null +++ b/tests/ui/unnecessary_wrap.stderr @@ -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 + |
