|
| 1 | +use clippy_utils::diagnostics::{span_lint, span_lint_and_then}; |
| 2 | +use clippy_utils::source::snippet_with_applicability; |
| 3 | +use rustc_data_structures::fx::FxHashSet; |
| 4 | +use rustc_errors::{Applicability, SuggestionStyle}; |
| 5 | +use rustc_lint::LateContext; |
| 6 | +use rustc_span::{BytePos, Pos, Span}; |
| 7 | +use url::Url; |
| 8 | + |
| 9 | +use crate::doc::DOC_MARKDOWN; |
| 10 | + |
| 11 | +pub fn check(cx: &LateContext<'_>, valid_idents: &FxHashSet<String>, text: &str, span: Span) { |
| 12 | + for word in text.split(|c: char| c.is_whitespace() || c == '\'') { |
| 13 | + // Trim punctuation as in `some comment (see foo::bar).` |
| 14 | + // ^^ |
| 15 | + // Or even as in `_foo bar_` which is emphasized. Also preserve `::` as a prefix/suffix. |
| 16 | + let mut word = word.trim_matches(|c: char| !c.is_alphanumeric() && c != ':'); |
| 17 | + |
| 18 | + // Remove leading or trailing single `:` which may be part of a sentence. |
| 19 | + if word.starts_with(':') && !word.starts_with("::") { |
| 20 | + word = word.trim_start_matches(':'); |
| 21 | + } |
| 22 | + if word.ends_with(':') && !word.ends_with("::") { |
| 23 | + word = word.trim_end_matches(':'); |
| 24 | + } |
| 25 | + |
| 26 | + if valid_idents.contains(word) || word.chars().all(|c| c == ':') { |
| 27 | + continue; |
| 28 | + } |
| 29 | + |
| 30 | + // Adjust for the current word |
| 31 | + let offset = word.as_ptr() as usize - text.as_ptr() as usize; |
| 32 | + let span = Span::new( |
| 33 | + span.lo() + BytePos::from_usize(offset), |
| 34 | + span.lo() + BytePos::from_usize(offset + word.len()), |
| 35 | + span.ctxt(), |
| 36 | + span.parent(), |
| 37 | + ); |
| 38 | + |
| 39 | + check_word(cx, word, span); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +fn check_word(cx: &LateContext<'_>, word: &str, span: Span) { |
| 44 | + /// Checks if a string is upper-camel-case, i.e., starts with an uppercase and |
| 45 | + /// contains at least two uppercase letters (`Clippy` is ok) and one lower-case |
| 46 | + /// letter (`NASA` is ok). |
| 47 | + /// Plurals are also excluded (`IDs` is ok). |
| 48 | + fn is_camel_case(s: &str) -> bool { |
| 49 | + if s.starts_with(|c: char| c.is_ascii_digit() | c.is_ascii_lowercase()) { |
| 50 | + return false; |
| 51 | + } |
| 52 | + |
| 53 | + let s = s.strip_suffix('s').unwrap_or(s); |
| 54 | + |
| 55 | + s.chars().all(char::is_alphanumeric) |
| 56 | + && s.chars().filter(|&c| c.is_uppercase()).take(2).count() > 1 |
| 57 | + && s.chars().filter(|&c| c.is_lowercase()).take(1).count() > 0 |
| 58 | + } |
| 59 | + |
| 60 | + fn has_underscore(s: &str) -> bool { |
| 61 | + s != "_" && !s.contains("\\_") && s.contains('_') |
| 62 | + } |
| 63 | + |
| 64 | + fn has_hyphen(s: &str) -> bool { |
| 65 | + s != "-" && s.contains('-') |
| 66 | + } |
| 67 | + |
| 68 | + if let Ok(url) = Url::parse(word) { |
| 69 | + // try to get around the fact that `foo::bar` parses as a valid URL |
| 70 | + if !url.cannot_be_a_base() { |
| 71 | + span_lint( |
| 72 | + cx, |
| 73 | + DOC_MARKDOWN, |
| 74 | + span, |
| 75 | + "you should put bare URLs between `<`/`>` or make a proper Markdown link", |
| 76 | + ); |
| 77 | + |
| 78 | + return; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // We assume that mixed-case words are not meant to be put inside backticks. (Issue #2343) |
| 83 | + if has_underscore(word) && has_hyphen(word) { |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + if has_underscore(word) || word.contains("::") || is_camel_case(word) { |
| 88 | + let mut applicability = Applicability::MachineApplicable; |
| 89 | + |
| 90 | + span_lint_and_then( |
| 91 | + cx, |
| 92 | + DOC_MARKDOWN, |
| 93 | + span, |
| 94 | + "item in documentation is missing backticks", |
| 95 | + |diag| { |
| 96 | + let snippet = snippet_with_applicability(cx, span, "..", &mut applicability); |
| 97 | + diag.span_suggestion_with_style( |
| 98 | + span, |
| 99 | + "try", |
| 100 | + format!("`{snippet}`"), |
| 101 | + applicability, |
| 102 | + // always show the suggestion in a separate line, since the |
| 103 | + // inline presentation adds another pair of backticks |
| 104 | + SuggestionStyle::ShowAlways, |
| 105 | + ); |
| 106 | + }, |
| 107 | + ); |
| 108 | + } |
| 109 | +} |
0 commit comments