]>
Commit | Line | Data |
---|---|---|
cdc7bbd5 XL |
1 | use clippy_utils::diagnostics::span_lint_and_sugg; |
2 | use clippy_utils::last_path_segment; | |
3 | use clippy_utils::source::snippet; | |
4 | use if_chain::if_chain; | |
5 | use rustc_errors::Applicability; | |
353b0b11 | 6 | use rustc_hir::{GenericArg, GenericArgsParentheses, Mutability, Ty, TyKind}; |
f20569fa XL |
7 | use rustc_lint::{LateContext, LateLintPass}; |
8 | use rustc_session::{declare_lint_pass, declare_tool_lint}; | |
9 | use rustc_span::symbol::sym; | |
10 | ||
f20569fa | 11 | declare_clippy_lint! { |
94222f64 XL |
12 | /// ### What it does |
13 | /// Checks for usage of `&Option<&T>`. | |
f20569fa | 14 | /// |
94222f64 XL |
15 | /// ### Why is this bad? |
16 | /// Since `&` is Copy, it's useless to have a | |
f20569fa XL |
17 | /// reference on `Option<&T>`. |
18 | /// | |
94222f64 XL |
19 | /// ### Known problems |
20 | /// It may be irrelevant to use this lint on | |
f20569fa XL |
21 | /// public API code as it will make a breaking change to apply it. |
22 | /// | |
94222f64 | 23 | /// ### Example |
f20569fa XL |
24 | /// ```rust,ignore |
25 | /// let x: &Option<&u32> = &Some(&0u32); | |
26 | /// ``` | |
27 | /// Use instead: | |
28 | /// ```rust,ignore | |
29 | /// let x: Option<&u32> = Some(&0u32); | |
30 | /// ``` | |
a2a8927a | 31 | #[clippy::version = "1.49.0"] |
f20569fa XL |
32 | pub REF_OPTION_REF, |
33 | pedantic, | |
34 | "use `Option<&T>` instead of `&Option<&T>`" | |
35 | } | |
36 | ||
37 | declare_lint_pass!(RefOptionRef => [REF_OPTION_REF]); | |
38 | ||
39 | impl<'tcx> LateLintPass<'tcx> for RefOptionRef { | |
40 | fn check_ty(&mut self, cx: &LateContext<'tcx>, ty: &'tcx Ty<'tcx>) { | |
41 | if_chain! { | |
9c376795 | 42 | if let TyKind::Ref(_, ref mut_ty) = ty.kind; |
f20569fa XL |
43 | if mut_ty.mutbl == Mutability::Not; |
44 | if let TyKind::Path(ref qpath) = &mut_ty.ty.kind; | |
45 | let last = last_path_segment(qpath); | |
f2b60f7d | 46 | if let Some(def_id) = last.res.opt_def_id(); |
f20569fa | 47 | |
c295e0f8 | 48 | if cx.tcx.is_diagnostic_item(sym::Option, def_id); |
cdc7bbd5 | 49 | if let Some(params) = last_path_segment(qpath).args ; |
353b0b11 | 50 | if params.parenthesized == GenericArgsParentheses::No; |
f20569fa XL |
51 | if let Some(inner_ty) = params.args.iter().find_map(|arg| match arg { |
52 | GenericArg::Type(inner_ty) => Some(inner_ty), | |
53 | _ => None, | |
54 | }); | |
9c376795 | 55 | if let TyKind::Ref(_, ref inner_mut_ty) = inner_ty.kind; |
2b03887a | 56 | if inner_mut_ty.mutbl == Mutability::Not; |
f20569fa XL |
57 | |
58 | then { | |
59 | span_lint_and_sugg( | |
60 | cx, | |
61 | REF_OPTION_REF, | |
62 | ty.span, | |
63 | "since `&` implements the `Copy` trait, `&Option<&T>` can be simplified to `Option<&T>`", | |
64 | "try", | |
65 | format!("Option<{}>", &snippet(cx, inner_ty.span, "..")), | |
66 | Applicability::MaybeIncorrect, | |
67 | ); | |
68 | } | |
69 | } | |
70 | } | |
71 | } |