Skip to content

Commit

Permalink
Add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
hkmatsumoto committed Oct 6, 2020
1 parent 6c698bb commit 5ae1e97
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 15 deletions.
28 changes: 20 additions & 8 deletions tests/ui/unnecessary_wrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,20 @@ fn func1(a: bool, b: bool) -> Option<i32> {
}
}

// should be linted
fn func2(a: bool, b: bool) -> Option<i32> {
if a && b {
return Some(10);
}
if a {
Some(20)
} else {
Some(30)
}
}

// public fns should not be linted
pub fn func2(a: bool) -> Option<i32> {
pub fn func3(a: bool) -> Option<i32> {
if a {
Some(1)
} else {
Expand All @@ -27,7 +39,7 @@ pub fn func2(a: bool) -> Option<i32> {
}

// should not be linted
fn func3(a: bool) -> Option<i32> {
fn func4(a: bool) -> Option<i32> {
if a {
Some(1)
} else {
Expand All @@ -36,22 +48,22 @@ fn func3(a: bool) -> Option<i32> {
}

// should be linted
fn func4() -> Option<i32> {
fn func5() -> Option<i32> {
Some(1)
}

// should not be linted
fn func5() -> Option<i32> {
fn func6() -> Option<i32> {
None
}

// should be linted
fn func6() -> Result<i32, ()> {
fn func7() -> Result<i32, ()> {
Ok(1)
}

// should not be linted
fn func7(a: bool) -> Result<i32, ()> {
fn func8(a: bool) -> Result<i32, ()> {
if a {
Ok(1)
} else {
Expand All @@ -60,12 +72,12 @@ fn func7(a: bool) -> Result<i32, ()> {
}

// should not be linted
fn func8(a: bool) -> Result<i32, ()> {
fn func9(a: bool) -> Result<i32, ()> {
Err(())
}

fn main() {
// method calls are not linted
func1(true, true);
func2(true);
func2(true, true);
}
40 changes: 33 additions & 7 deletions tests/ui/unnecessary_wrap.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -26,38 +26,64 @@ LL | } else {
...

error: this function returns unnecessarily wrapping data
--> $DIR/unnecessary_wrap.rs:39:1
--> $DIR/unnecessary_wrap.rs:21:1
|
LL | / fn func4() -> Option<i32> {
LL | / fn func2(a: bool, b: bool) -> Option<i32> {
LL | | if a && b {
LL | | return Some(10);
LL | | }
... |
LL | | }
LL | | }
| |_^
|
help: remove `Option` from the return type...
|
LL | fn func2(a: bool, b: bool) -> i32 {
| ^^^
help: ...and change the returning expressions
|
LL | return 10;
LL | }
LL | if a {
LL | 20
LL | } else {
LL | 30
|

error: this function returns unnecessarily wrapping data
--> $DIR/unnecessary_wrap.rs:51:1
|
LL | / fn func5() -> Option<i32> {
LL | | Some(1)
LL | | }
| |_^
|
help: remove `Option` from the return type...
|
LL | fn func4() -> i32 {
LL | fn func5() -> i32 {
| ^^^
help: ...and change the returning expressions
|
LL | 1
|

error: this function returns unnecessarily wrapping data
--> $DIR/unnecessary_wrap.rs:49:1
--> $DIR/unnecessary_wrap.rs:61:1
|
LL | / fn func6() -> Result<i32, ()> {
LL | / fn func7() -> Result<i32, ()> {
LL | | Ok(1)
LL | | }
| |_^
|
help: remove `Result` from the return type...
|
LL | fn func6() -> i32 {
LL | fn func7() -> i32 {
| ^^^
help: ...and change the returning expressions
|
LL | 1
|

error: aborting due to 3 previous errors
error: aborting due to 4 previous errors

0 comments on commit 5ae1e97

Please sign in to comment.