about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--clippy_lints/src/ref_option_ref.rs16
-rw-r--r--tests/ui/ref_option_ref.rs4
-rw-r--r--tests/ui/ref_option_ref.stderr20
3 files changed, 28 insertions, 12 deletions
diff --git a/clippy_lints/src/ref_option_ref.rs b/clippy_lints/src/ref_option_ref.rs
index 40ac2b3f4f1..4b5bc93d8a1 100644
--- a/clippy_lints/src/ref_option_ref.rs
+++ b/clippy_lints/src/ref_option_ref.rs
@@ -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 {
             }
         }
     }
-}
\ No newline at end of file
+}
diff --git a/tests/ui/ref_option_ref.rs b/tests/ui/ref_option_ref.rs
index 7f05990c0a0..24db42efb80 100644
--- a/tests/ui/ref_option_ref.rs
+++ b/tests/ui/ref_option_ref.rs
@@ -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;
 }
diff --git a/tests/ui/ref_option_ref.stderr b/tests/ui/ref_option_ref.stderr
index 90bcaef7570..26f4065d311 100644
--- a/tests/ui/ref_option_ref.stderr
+++ b/tests/ui/ref_option_ref.stderr
@@ -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