|
| 1 | +use clippy_utils::diagnostics::{multispan_sugg_with_applicability, span_lint_and_then}; |
| 2 | +use if_chain::if_chain; |
| 3 | +use rustc_ast::{token::CommentKind, AttrKind, AttrStyle, Attribute, Item}; |
| 4 | +use rustc_errors::Applicability; |
| 5 | +use rustc_lint::{EarlyContext, EarlyLintPass}; |
| 6 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 7 | +use rustc_span::Span; |
| 8 | + |
| 9 | +declare_clippy_lint! { |
| 10 | + /// ### What it does |
| 11 | + /// Detects the use of outer doc comments (`///`, `/**`) followed by a bang (`!`): `///!` |
| 12 | + /// |
| 13 | + /// ### Why is this bad? |
| 14 | + /// Triple-slash comments (known as "outer doc comments") apply to items that follow it. |
| 15 | + /// An outer doc comment followed by a bang (i.e. `///!`) has no specific meaning. |
| 16 | + /// |
| 17 | + /// The user most likely meant to write an inner doc comment (`//!`, `/*!`), which |
| 18 | + /// applies to the parent item (i.e. the item that the comment is contained in, |
| 19 | + /// usually a module or crate). |
| 20 | + /// |
| 21 | + /// ### Known problems |
| 22 | + /// Inner doc comments can only appear before items, so there are certain cases where the suggestion |
| 23 | + /// made by this lint is not valid code. For example: |
| 24 | + /// ```rs |
| 25 | + /// fn foo() {} |
| 26 | + /// ///! |
| 27 | + /// fn bar() {} |
| 28 | + /// ``` |
| 29 | + /// This lint detects the doc comment and suggests changing it to `//!`, but an inner doc comment |
| 30 | + /// is not valid at that position. |
| 31 | + /// |
| 32 | + /// ### Example |
| 33 | + /// In this example, the doc comment is attached to the *function*, rather than the *module*. |
| 34 | + /// ```rust |
| 35 | + /// pub mod util { |
| 36 | + /// ///! This module contains utility functions. |
| 37 | + /// |
| 38 | + /// pub fn dummy() {} |
| 39 | + /// } |
| 40 | + /// ``` |
| 41 | + /// |
| 42 | + /// Use instead: |
| 43 | + /// ```rust |
| 44 | + /// pub mod util { |
| 45 | + /// //! This module contains utility functions. |
| 46 | + /// |
| 47 | + /// pub fn dummy() {} |
| 48 | + /// } |
| 49 | + /// ``` |
| 50 | + #[clippy::version = "1.70.0"] |
| 51 | + pub SUSPICIOUS_DOC_COMMENTS, |
| 52 | + suspicious, |
| 53 | + "suspicious usage of (outer) doc comments" |
| 54 | +} |
| 55 | +declare_lint_pass!(SuspiciousDocComments => [SUSPICIOUS_DOC_COMMENTS]); |
| 56 | + |
| 57 | +const WARNING: &str = "this is an outer doc comment and does not apply to the parent module or crate"; |
| 58 | +const HELP: &str = "use an inner doc comment to document the parent module or crate"; |
| 59 | + |
| 60 | +impl EarlyLintPass for SuspiciousDocComments { |
| 61 | + fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) { |
| 62 | + let replacements = collect_doc_comment_replacements(&item.attrs); |
| 63 | + |
| 64 | + if let Some(((lo_span, _), (hi_span, _))) = replacements.first().zip(replacements.last()) { |
| 65 | + let span = lo_span.to(*hi_span); |
| 66 | + |
| 67 | + span_lint_and_then(cx, SUSPICIOUS_DOC_COMMENTS, span, WARNING, |diag| { |
| 68 | + multispan_sugg_with_applicability(diag, HELP, Applicability::MaybeIncorrect, replacements); |
| 69 | + }); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +fn collect_doc_comment_replacements(attrs: &[Attribute]) -> Vec<(Span, String)> { |
| 75 | + attrs |
| 76 | + .iter() |
| 77 | + .filter_map(|attr| { |
| 78 | + if_chain! { |
| 79 | + if let AttrKind::DocComment(com_kind, sym) = attr.kind; |
| 80 | + if let AttrStyle::Outer = attr.style; |
| 81 | + if let Some(com) = sym.as_str().strip_prefix('!'); |
| 82 | + then { |
| 83 | + let sugg = match com_kind { |
| 84 | + CommentKind::Line => format!("//!{com}"), |
| 85 | + CommentKind::Block => format!("/*!{com}*/") |
| 86 | + }; |
| 87 | + Some((attr.span, sugg)) |
| 88 | + } else { |
| 89 | + None |
| 90 | + } |
| 91 | + } |
| 92 | + }) |
| 93 | + .collect() |
| 94 | +} |
0 commit comments