|
| 1 | +use crate::utils::internal_lints::lint_without_lint_pass::is_lint_ref_type; |
| 2 | +use clippy_utils::diagnostics::span_lint_and_help; |
| 3 | +use regex::Regex; |
| 4 | +use rustc_ast as ast; |
| 5 | +use rustc_hir::{Item, ItemKind, Mutability}; |
| 6 | +use rustc_lint::{LateContext, LateLintPass}; |
| 7 | +use rustc_session::{declare_tool_lint, impl_lint_pass}; |
| 8 | + |
| 9 | +declare_clippy_lint! { |
| 10 | + /// ### What it does |
| 11 | + /// Checks if lint formulations have a standardized format. |
| 12 | + /// |
| 13 | + /// ### Why is this bad? |
| 14 | + /// It's not neccessarily bad, but we try to enforce a standard in Clippy. |
| 15 | + /// |
| 16 | + /// ### Example |
| 17 | + /// `Checks for use...` can be written as `Checks for usage...` . |
| 18 | + pub ALMOST_STANDARD_LINT_FORMULATION, |
| 19 | + internal, |
| 20 | + "lint formulations must have a standardized format." |
| 21 | +} |
| 22 | + |
| 23 | +impl_lint_pass!(AlmostStandardFormulation => [ALMOST_STANDARD_LINT_FORMULATION]); |
| 24 | + |
| 25 | +pub struct AlmostStandardFormulation { |
| 26 | + standard_formulations: Vec<StandardFormulations<'static>>, |
| 27 | +} |
| 28 | + |
| 29 | +#[derive(Debug)] |
| 30 | +struct StandardFormulations<'a> { |
| 31 | + wrong_pattern: Regex, |
| 32 | + correction: &'a str, |
| 33 | +} |
| 34 | + |
| 35 | +impl AlmostStandardFormulation { |
| 36 | + pub fn new() -> Self { |
| 37 | + let standard_formulations = vec![StandardFormulations { |
| 38 | + wrong_pattern: Regex::new(r"^(Check for|Detects? uses?)").unwrap(), |
| 39 | + correction: "Checks for", |
| 40 | + }]; |
| 41 | + Self { standard_formulations } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +impl<'tcx> LateLintPass<'tcx> for AlmostStandardFormulation { |
| 46 | + fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) { |
| 47 | + let mut check_next = false; |
| 48 | + if let ItemKind::Static(ty, Mutability::Not, _) = item.kind { |
| 49 | + let lines = cx |
| 50 | + .tcx |
| 51 | + .hir() |
| 52 | + .attrs(item.hir_id()) |
| 53 | + .iter() |
| 54 | + .filter_map(|attr| ast::Attribute::doc_str(attr).map(|sym| (sym, attr))); |
| 55 | + if is_lint_ref_type(cx, ty) { |
| 56 | + for (line, attr) in lines { |
| 57 | + let cur_line = line.as_str().trim(); |
| 58 | + if check_next && !cur_line.is_empty() { |
| 59 | + for formulation in &self.standard_formulations { |
| 60 | + let starts_with_correct_formulation = cur_line.starts_with(formulation.correction); |
| 61 | + if !starts_with_correct_formulation && formulation.wrong_pattern.is_match(cur_line) { |
| 62 | + if let Some(ident) = attr.ident() { |
| 63 | + span_lint_and_help( |
| 64 | + cx, |
| 65 | + ALMOST_STANDARD_LINT_FORMULATION, |
| 66 | + ident.span, |
| 67 | + "non-standard lint formulation", |
| 68 | + None, |
| 69 | + &format!("try using `{}` instead", formulation.correction), |
| 70 | + ); |
| 71 | + } |
| 72 | + return; |
| 73 | + } |
| 74 | + } |
| 75 | + return; |
| 76 | + } else if cur_line.contains("What it does") { |
| 77 | + check_next = true; |
| 78 | + } else if cur_line.contains("Why is this bad") { |
| 79 | + // Formulation documentation is done. Can add check to ensure that missing formulation is added |
| 80 | + // and add a check if it matches no accepted formulation |
| 81 | + return; |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments