Skip to content

Commit 097df8f

Browse files
committed
new_ret_no_self correct false positive on raw pointer return types
1 parent 6e75050 commit 097df8f

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
966966
}
967967
}
968968

969+
// if return type is mutable pointer
970+
if let TyKind::RawPtr(ty::TypeAndMut{ty: ret_type, ..}) = ret_ty.sty {
971+
// then the pointer must point to Self
972+
if same_tys(cx, ty, ret_type) { return; }
973+
}
974+
969975
if name == "new" && !same_tys(cx, ret_ty, ty) {
970976
span_lint(cx,
971977
NEW_RET_NO_SELF,

tests/ui/new_ret_no_self.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,24 @@ impl TupleReturnerBad {
119119
// should trigger lint
120120
pub fn new() -> (u32, u32) { unimplemented!(); }
121121
}
122+
123+
struct MutPointerReturnerOk;
124+
125+
impl MutPointerReturnerOk {
126+
// should not trigger lint
127+
pub fn new() -> *mut Self { unimplemented!(); }
128+
}
129+
130+
struct MutPointerReturnerOk2;
131+
132+
impl MutPointerReturnerOk2 {
133+
// should not trigger lint
134+
pub fn new() -> *const Self { unimplemented!(); }
135+
}
136+
137+
struct MutPointerReturnerBad;
138+
139+
impl MutPointerReturnerBad {
140+
// should trigger lint
141+
pub fn new() -> *mut V { unimplemented!(); }
142+
}

tests/ui/new_ret_no_self.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,11 @@ error: methods called `new` usually return `Self`
3030
120 | pub fn new() -> (u32, u32) { unimplemented!(); }
3131
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3232

33-
error: aborting due to 4 previous errors
33+
error: methods called `new` usually return `Self`
34+
--> $DIR/new_ret_no_self.rs:141:5
35+
|
36+
141 | pub fn new() -> *mut V { unimplemented!(); }
37+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
38+
39+
error: aborting due to 5 previous errors
3440

0 commit comments

Comments
 (0)