Skip to content

Commit 3008cd5

Browse files
committed
Add support for methods
1 parent ccfd7e2 commit 3008cd5

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

clippy_lints/src/unnecessary_wrap.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryWrap {
6464
hir_id: HirId,
6565
) {
6666
match fn_kind {
67-
FnKind::ItemFn(.., visibility, _) if visibility.node.is_pub() => return,
67+
FnKind::ItemFn(.., visibility, _) | FnKind::Method(.., Some(visibility), _) => {
68+
if visibility.node.is_pub() {
69+
return;
70+
}
71+
},
6872
FnKind::Closure(..) => return,
6973
_ => (),
7074
}

tests/ui/unnecessary_wrap.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,20 @@ fn func9(a: bool) -> Result<i32, ()> {
7676
Err(())
7777
}
7878

79+
struct A;
80+
81+
impl A {
82+
// should not be linted
83+
pub fn func10() -> Option<i32> {
84+
Some(1)
85+
}
86+
87+
// should be linted
88+
fn func11() -> Option<i32> {
89+
Some(1)
90+
}
91+
}
92+
7993
fn main() {
8094
// method calls are not linted
8195
func1(true, true);

tests/ui/unnecessary_wrap.stderr

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,22 @@ help: ...and change the returning expressions
8585
LL | 1
8686
|
8787

88-
error: aborting due to 4 previous errors
88+
error: this function's return value is unnecessarily wrapped by `Option`
89+
--> $DIR/unnecessary_wrap.rs:88:5
90+
|
91+
LL | / fn func11() -> Option<i32> {
92+
LL | | Some(1)
93+
LL | | }
94+
| |_____^
95+
|
96+
help: remove `Option` from the return type...
97+
|
98+
LL | fn func11() -> i32 {
99+
| ^^^
100+
help: ...and change the returning expressions
101+
|
102+
LL | 1
103+
|
104+
105+
error: aborting due to 5 previous errors
89106

0 commit comments

Comments
 (0)