]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/clippy_lints/src/ref_option_ref.rs
New upstream version 1.52.1+dfsg1
[rustc.git] / src / tools / clippy / clippy_lints / src / ref_option_ref.rs
CommitLineData
f20569fa
XL
1use crate::utils::{last_path_segment, snippet, span_lint_and_sugg};
2use rustc_hir::{GenericArg, Mutability, Ty, TyKind};
3use rustc_lint::{LateContext, LateLintPass};
4use rustc_session::{declare_lint_pass, declare_tool_lint};
5use rustc_span::symbol::sym;
6
7use if_chain::if_chain;
8use rustc_errors::Applicability;
9
10declare_clippy_lint! {
11 /// **What it does:** Checks for usage of `&Option<&T>`.
12 ///
13 /// **Why is this bad?** Since `&` is Copy, it's useless to have a
14 /// reference on `Option<&T>`.
15 ///
16 /// **Known problems:** It may be irrelevant to use this lint on
17 /// public API code as it will make a breaking change to apply it.
18 ///
19 /// **Example:**
20 ///
21 /// ```rust,ignore
22 /// let x: &Option<&u32> = &Some(&0u32);
23 /// ```
24 /// Use instead:
25 /// ```rust,ignore
26 /// let x: Option<&u32> = Some(&0u32);
27 /// ```
28 pub REF_OPTION_REF,
29 pedantic,
30 "use `Option<&T>` instead of `&Option<&T>`"
31}
32
33declare_lint_pass!(RefOptionRef => [REF_OPTION_REF]);
34
35impl<'tcx> LateLintPass<'tcx> for RefOptionRef {
36 fn check_ty(&mut self, cx: &LateContext<'tcx>, ty: &'tcx Ty<'tcx>) {
37 if_chain! {
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();
44
45 if cx.tcx.is_diagnostic_item(sym::option_type, def_id);
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),
50 _ => None,
51 });
52 if let TyKind::Rptr(_, _) = inner_ty.kind;
53
54 then {
55 span_lint_and_sugg(
56 cx,
57 REF_OPTION_REF,
58 ty.span,
59 "since `&` implements the `Copy` trait, `&Option<&T>` can be simplified to `Option<&T>`",
60 "try",
61 format!("Option<{}>", &snippet(cx, inner_ty.span, "..")),
62 Applicability::MaybeIncorrect,
63 );
64 }
65 }
66 }
67}