1 use crate::utils::{last_path_segment, match_def_path, paths, snippet, span_lint_and_sugg};
2 use rustc_hir::{GenericArg, Mutability, Ty, TyKind};
3 use rustc_lint::{LateContext, LateLintPass};
4 use rustc_session::{declare_lint_pass, declare_tool_lint};
6 use if_chain::if_chain;
7 use rustc_errors::Applicability;
10 /// **What it does:** Checks for usage of `&Option<&T>`.
12 /// **Why is this bad?** Since `&` is Copy, it's useless to have a
13 /// reference on `Option<&T>`.
15 /// **Known problems:** None.
20 /// // example code where clippy issues a warning
21 /// let x: &Option<&u32> = &Some(&0u32);
25 /// // example code which does not raise clippy warning
26 /// let x: Option<&u32> = Some(&0u32);
30 "use `Option<&T>` instead of `&Option<&T>`"
33 declare_lint_pass!(RefOptionRef => [REF_OPTION_REF]);
35 impl<'tcx> LateLintPass<'tcx> for RefOptionRef {
36 fn check_ty(&mut self, cx: &LateContext<'tcx>, ty: &'tcx Ty<'tcx>) {
38 if let TyKind::Rptr(_, ref mut_ty) = ty.kind;
39 if mut_ty.mutbl == Mutability::Not;
40 if let TyKind::Path(ref qpath) = &mut_ty.ty.kind;
41 let last = last_path_segment(qpath);
42 if let Some(res) = last.res;
43 if let Some(def_id) = res.opt_def_id();
45 if match_def_path(cx, def_id, &paths::OPTION);
46 if let Some(ref params) = last_path_segment(qpath).args ;
47 if !params.parenthesized;
48 if let Some(inner_ty) = params.args.iter().find_map(|arg| match arg {
49 GenericArg::Type(inner_ty) => Some(inner_ty),
52 if let TyKind::Rptr(_, _) = inner_ty.kind;
59 "since & implements Copy trait, &Option<&T> can be simplifyied into Option<&T>",
61 format!("Option<{}>", &snippet(cx, inner_ty.span, "..")),
62 Applicability::Unspecified,