clippy_lint: Add 'ref_option_ref' move to check_ty and add type alias test

This commit is contained in:
dvermd 2020-10-13 22:13:54 +02:00
parent d1baa25f04
commit 469b2fc781
3 changed files with 28 additions and 12 deletions

View file

@ -33,11 +33,8 @@ declare_clippy_lint! {
declare_lint_pass!(RefOptionRef => [REF_OPTION_REF]);
impl<'tcx> LateLintPass<'tcx> for RefOptionRef {
fn check_local(&mut self, cx: &LateContext<'tcx>, local: &'tcx Local<'_>) {
if let Some(ref ty) = local.ty {
self.check_ref_option_ref(cx, ty);
}
fn check_ty(&mut self, cx: &LateContext<'tcx>, ty: &'tcx Ty<'tcx>) {
self.check_ref_option_ref(cx, ty);
}
}
@ -46,8 +43,11 @@ impl RefOptionRef {
if_chain! {
if let TyKind::Rptr(_, ref mut_ty) = ty.kind;
if mut_ty.mutbl == Mutability::Not;
if let TyKind::Path(ref qpath) = &mut_ty.ty.kind ;
if let Some(def_id) = cx.typeck_results().qpath_res(qpath, ty.hir_id).opt_def_id();
if let TyKind::Path(ref qpath) = &mut_ty.ty.kind;
let last = last_path_segment(qpath);
if let Some(res) = last.res;
if let Some(def_id) = res.opt_def_id();
if match_def_path(cx, def_id, &paths::OPTION);
if let Some(ref params) = last_path_segment(qpath).args ;
if !params.parenthesized;
@ -70,4 +70,4 @@ impl RefOptionRef {
}
}
}
}
}

View file

@ -1,5 +1,9 @@
#![allow(unused)]
#![warn(clippy::ref_option_ref)]
type OptRefU32<'a> = &'a Option<&'a u32>;
type OptRef<'a, T> = &'a Option<&'a T>;
fn main() {
let x: &Option<&u32> = &None;
}

View file

@ -1,10 +1,22 @@
error: since & implements Copy trait, &Option<&T> can be simplifyied into Option<&T>
--> $DIR/ref_option_ref.rs:4:12
--> $DIR/ref_option_ref.rs:4:22
|
LL | let x: &Option<&u32> = &None;
| ^^^^^^^^^^^^^ help: try: `Option<&u32>`
LL | type OptRefU32<'a> = &'a Option<&'a u32>;
| ^^^^^^^^^^^^^^^^^^^ help: try: `Option<&'a u32>`
|
= note: `-D clippy::ref-option-ref` implied by `-D warnings`
error: aborting due to previous error
error: since & implements Copy trait, &Option<&T> can be simplifyied into Option<&T>
--> $DIR/ref_option_ref.rs:5:22
|
LL | type OptRef<'a, T> = &'a Option<&'a T>;
| ^^^^^^^^^^^^^^^^^ help: try: `Option<&'a T>`
error: since & implements Copy trait, &Option<&T> can be simplifyied into Option<&T>
--> $DIR/ref_option_ref.rs:8:12
|
LL | let x: &Option<&u32> = &None;
| ^^^^^^^^^^^^^ help: try: `Option<&u32>`
error: aborting due to 3 previous errors