Skip to content

Commit 469b2fc

Browse files
committed
clippy_lint: Add 'ref_option_ref' move to check_ty and add type alias test
1 parent d1baa25 commit 469b2fc

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

clippy_lints/src/ref_option_ref.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,8 @@ declare_clippy_lint! {
3333
declare_lint_pass!(RefOptionRef => [REF_OPTION_REF]);
3434

3535
impl<'tcx> LateLintPass<'tcx> for RefOptionRef {
36-
fn check_local(&mut self, cx: &LateContext<'tcx>, local: &'tcx Local<'_>) {
37-
38-
if let Some(ref ty) = local.ty {
39-
self.check_ref_option_ref(cx, ty);
40-
}
36+
fn check_ty(&mut self, cx: &LateContext<'tcx>, ty: &'tcx Ty<'tcx>) {
37+
self.check_ref_option_ref(cx, ty);
4138
}
4239
}
4340

@@ -46,8 +43,11 @@ impl RefOptionRef {
4643
if_chain! {
4744
if let TyKind::Rptr(_, ref mut_ty) = ty.kind;
4845
if mut_ty.mutbl == Mutability::Not;
49-
if let TyKind::Path(ref qpath) = &mut_ty.ty.kind ;
50-
if let Some(def_id) = cx.typeck_results().qpath_res(qpath, ty.hir_id).opt_def_id();
46+
if let TyKind::Path(ref qpath) = &mut_ty.ty.kind;
47+
let last = last_path_segment(qpath);
48+
if let Some(res) = last.res;
49+
if let Some(def_id) = res.opt_def_id();
50+
5151
if match_def_path(cx, def_id, &paths::OPTION);
5252
if let Some(ref params) = last_path_segment(qpath).args ;
5353
if !params.parenthesized;
@@ -70,4 +70,4 @@ impl RefOptionRef {
7070
}
7171
}
7272
}
73-
}
73+
}

tests/ui/ref_option_ref.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
#![allow(unused)]
12
#![warn(clippy::ref_option_ref)]
23

4+
type OptRefU32<'a> = &'a Option<&'a u32>;
5+
type OptRef<'a, T> = &'a Option<&'a T>;
6+
37
fn main() {
48
let x: &Option<&u32> = &None;
59
}

tests/ui/ref_option_ref.stderr

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
error: since & implements Copy trait, &Option<&T> can be simplifyied into Option<&T>
2-
--> $DIR/ref_option_ref.rs:4:12
2+
--> $DIR/ref_option_ref.rs:4:22
33
|
4-
LL | let x: &Option<&u32> = &None;
5-
| ^^^^^^^^^^^^^ help: try: `Option<&u32>`
4+
LL | type OptRefU32<'a> = &'a Option<&'a u32>;
5+
| ^^^^^^^^^^^^^^^^^^^ help: try: `Option<&'a u32>`
66
|
77
= note: `-D clippy::ref-option-ref` implied by `-D warnings`
88

9-
error: aborting due to previous error
9+
error: since & implements Copy trait, &Option<&T> can be simplifyied into Option<&T>
10+
--> $DIR/ref_option_ref.rs:5:22
11+
|
12+
LL | type OptRef<'a, T> = &'a Option<&'a T>;
13+
| ^^^^^^^^^^^^^^^^^ help: try: `Option<&'a T>`
14+
15+
error: since & implements Copy trait, &Option<&T> can be simplifyied into Option<&T>
16+
--> $DIR/ref_option_ref.rs:8:12
17+
|
18+
LL | let x: &Option<&u32> = &None;
19+
| ^^^^^^^^^^^^^ help: try: `Option<&u32>`
20+
21+
error: aborting due to 3 previous errors
1022

0 commit comments

Comments
 (0)