Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

too_long_first_doc_paragraph considers rendered output link #13363

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions clippy_lints/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0", optional = true }
tempfile = { version = "3.3.0", optional = true }
toml = "0.7.3"
regex = { version = "1.5", optional = true }
regex = "1.5"
unicode-normalization = "0.1"
unicode-script = { version = "0.5", default-features = false }
semver = "1.0"
Expand All @@ -32,7 +32,7 @@ walkdir = "2.3"

[features]
# build clippy with internal lints enabled, off by default
internal = ["serde_json", "tempfile", "regex"]
internal = ["serde_json", "tempfile"]

[package.metadata.rust-analyzer]
# This crate uses #[feature(rustc_private)]
Expand Down
14 changes: 13 additions & 1 deletion clippy_lints/src/doc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use pulldown_cmark::Event::{
};
use pulldown_cmark::Tag::{BlockQuote, CodeBlock, FootnoteDefinition, Heading, Item, Link, Paragraph};
use pulldown_cmark::{BrokenLink, CodeBlockKind, CowStr, Options, TagEnd};
use regex::Regex;
use rustc_ast::ast::Attribute;
use rustc_data_structures::fx::FxHashSet;
use rustc_hir::intravisit::{self, Visitor};
Expand Down Expand Up @@ -845,7 +846,9 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
ticks_unbalanced = false;
paragraph_range = range;
if is_first_paragraph {
headers.first_paragraph_len = doc[paragraph_range.clone()].chars().count();
let paragraph_string: String = doc[paragraph_range.clone()].chars().collect();
let markdown_without_links = replace_markdown_links(&paragraph_string);
headers.first_paragraph_len = markdown_without_links.len();
Comment on lines +849 to +851
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're in the context of a markdown parser we should aim to use that rather than ad hoc processing, possibly by moving the first_paragraph_len part into the Text event as a +=

is_first_paragraph = false;
}
},
Expand Down Expand Up @@ -946,6 +949,15 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
headers
}

fn replace_markdown_links(text: &str) -> String {
let re = Regex::new(r"\[([^\]]+)]\([^\)]+\)").unwrap();

re.replace_all(text, |caps: &regex::Captures<'_>| {
caps.get(1).map_or("", |m| m.as_str()).to_string()
})
.into_owned()
}

struct FindPanicUnwrap<'a, 'tcx> {
cx: &'a LateContext<'tcx>,
is_const: bool,
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/too_long_first_doc_paragraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ pub union Union2 {
/// gravida non lacinia at, rhoncus eu lacus.
fn f() {}

/// Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc turpis nunc, lacinia
/// a dolor in, pellentesque aliquet enim. Cras nec maximus sem.
/// [this link](https://fooooooooooooooooooooooooooooooooooooooooooooooooooooooooo.com)
pub fn foo() {}

fn main() {
// test code goes here
}