1 use clippy_utils::diagnostics::span_lint_and_help; 2 use rustc_ast::ast::{BindingAnnotation, Pat, PatKind}; 3 use rustc_lint::{EarlyContext, EarlyLintPass}; 4 use rustc_session::{declare_lint_pass, declare_tool_lint}; 5 6 declare_clippy_lint! { 7 /// ### What it does 8 /// Checks for usages of the `ref` keyword. 9 /// ### Why is this bad? 10 /// The `ref` keyword can be confusing for people unfamiliar with it, and often 11 /// it is more concise to use `&` instead. 12 /// ### Example 13 /// ```rust 14 /// let opt = Some(5); 15 /// if let Some(ref foo) = opt {} 16 /// ``` 17 /// Use instead: 18 /// ```rust 19 /// let opt = Some(5); 20 /// if let Some(foo) = &opt {} 21 /// ``` 22 #[clippy::version = "1.71.0"] 23 pub REF_PATTERNS, 24 restriction, 25 "use of a ref pattern, e.g. Some(ref value)" 26 } 27 declare_lint_pass!(RefPatterns => [REF_PATTERNS]); 28 29 impl EarlyLintPass for RefPatterns { check_pat(&mut self, cx: &EarlyContext<'_>, pat: &Pat)30 fn check_pat(&mut self, cx: &EarlyContext<'_>, pat: &Pat) { 31 if let PatKind::Ident(BindingAnnotation::REF, _, _) = pat.kind 32 && !pat.span.from_expansion() 33 { 34 span_lint_and_help( 35 cx, 36 REF_PATTERNS, 37 pat.span, 38 "usage of ref pattern", 39 None, 40 "consider using `&` for clarity instead", 41 ); 42 } 43 } 44 } 45