Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 6e75050

Browse files
committed
new_ret_no_self correct linting of tuple return types
1 parent b1d0343 commit 6e75050

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -936,6 +936,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
936936
if let hir::ImplItemKind::Method(_, _) = implitem.node {
937937
let ret_ty = return_ty(cx, implitem.id);
938938

939+
// println!("ret_ty: {:?}", ret_ty);
940+
// println!("ret_ty.sty {:?}", ret_ty.sty);
941+
939942
// if return type is impl trait
940943
if let TyKind::Opaque(def_id, _) = ret_ty.sty {
941944

@@ -955,6 +958,14 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
955958
}
956959
}
957960

961+
// if return type is tuple
962+
if let TyKind::Tuple(list) = ret_ty.sty {
963+
// then at least one of the types in the tuple must be Self
964+
for ret_type in list {
965+
if same_tys(cx, ty, ret_type) { return; }
966+
}
967+
}
968+
958969
if name == "new" && !same_tys(cx, ret_ty, ty) {
959970
span_lint(cx,
960971
NEW_RET_NO_SELF,

tests/ui/new_ret_no_self.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,31 @@ impl V {
9191
unimplemented!();
9292
}
9393
}
94+
95+
struct TupleReturnerOk;
96+
97+
impl TupleReturnerOk {
98+
// should not trigger lint
99+
pub fn new() -> (Self, u32) { unimplemented!(); }
100+
}
101+
102+
struct TupleReturnerOk2;
103+
104+
impl TupleReturnerOk2 {
105+
// should not trigger lint (it doesn't matter which element in the tuple is Self)
106+
pub fn new() -> (u32, Self) { unimplemented!(); }
107+
}
108+
109+
struct TupleReturnerOk3;
110+
111+
impl TupleReturnerOk3 {
112+
// should not trigger lint (tuple can contain multiple Self)
113+
pub fn new() -> (Self, Self) { unimplemented!(); }
114+
}
115+
116+
struct TupleReturnerBad;
117+
118+
impl TupleReturnerBad {
119+
// should trigger lint
120+
pub fn new() -> (u32, u32) { unimplemented!(); }
121+
}

tests/ui/new_ret_no_self.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,11 @@ error: methods called `new` usually return `Self`
2424
92 | | }
2525
| |_____^
2626

27-
error: aborting due to 3 previous errors
27+
error: methods called `new` usually return `Self`
28+
--> $DIR/new_ret_no_self.rs:120:5
29+
|
30+
120 | pub fn new() -> (u32, u32) { unimplemented!(); }
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
32+
33+
error: aborting due to 4 previous errors
2834

0 commit comments

Comments
 (0)