new_ret_no_self correct false positive on raw pointer return types

This commit is contained in:
Josh Mcguigan 2018-10-19 05:20:33 -07:00
parent 6e75050be0
commit 097df8f223
3 changed files with 34 additions and 1 deletions

View File

@ -966,6 +966,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
}
}
// if return type is mutable pointer
if let TyKind::RawPtr(ty::TypeAndMut{ty: ret_type, ..}) = ret_ty.sty {
// then the pointer must point to Self
if same_tys(cx, ty, ret_type) { return; }
}
if name == "new" && !same_tys(cx, ret_ty, ty) {
span_lint(cx,
NEW_RET_NO_SELF,

View File

@ -119,3 +119,24 @@ impl TupleReturnerBad {
// should trigger lint
pub fn new() -> (u32, u32) { unimplemented!(); }
}
struct MutPointerReturnerOk;
impl MutPointerReturnerOk {
// should not trigger lint
pub fn new() -> *mut Self { unimplemented!(); }
}
struct MutPointerReturnerOk2;
impl MutPointerReturnerOk2 {
// should not trigger lint
pub fn new() -> *const Self { unimplemented!(); }
}
struct MutPointerReturnerBad;
impl MutPointerReturnerBad {
// should trigger lint
pub fn new() -> *mut V { unimplemented!(); }
}

View File

@ -30,5 +30,11 @@ error: methods called `new` usually return `Self`
120 | pub fn new() -> (u32, u32) { unimplemented!(); }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 4 previous errors
error: methods called `new` usually return `Self`
--> $DIR/new_ret_no_self.rs:141:5
|
141 | pub fn new() -> *mut V { unimplemented!(); }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 5 previous errors