From 7966ae0f3352894834a316bea9fd45068f94d937 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 10 Sep 2025 18:54:38 +0200 Subject: Simplify code for `find_raw_urls` --- src/librustdoc/passes/lint/bare_urls.rs | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/librustdoc/passes/lint/bare_urls.rs b/src/librustdoc/passes/lint/bare_urls.rs index ffa160beae3..06256d61151 100644 --- a/src/librustdoc/passes/lint/bare_urls.rs +++ b/src/librustdoc/passes/lint/bare_urls.rs @@ -85,7 +85,7 @@ static URL_REGEX: LazyLock = LazyLock::new(|| { fn find_raw_urls( cx: &DocContext<'_>, - whole_text: &str, + dox: &str, text: &str, range: Range, f: &impl Fn(&DocContext<'_>, &'static str, Range, Option<&str>), @@ -93,26 +93,20 @@ fn find_raw_urls( trace!("looking for raw urls in {text}"); // For now, we only check "full" URLs (meaning, starting with "http://" or "https://"). for match_ in URL_REGEX.find_iter(text) { - let url_range = match_.range(); + let mut url_range = match_.range(); + url_range.start += range.start; + url_range.end += range.start; let mut without_brackets = None; - let mut extra_range = 0; - // If the whole text is contained inside `[]`, then we need to replace the brackets and + // If the link is contained inside `[]`, then we need to replace the brackets and // not just add `<>`. - if whole_text[..range.start + url_range.start].ends_with('[') - && range.start + url_range.end <= whole_text.len() - && whole_text[range.start + url_range.end..].starts_with(']') + if dox[..url_range.start].ends_with('[') + && url_range.end <= dox.len() + && dox[url_range.end..].starts_with(']') { - extra_range = 1; + url_range.start -= 1; + url_range.end += 1; without_brackets = Some(match_.as_str()); } - f( - cx, - "this URL is not a hyperlink", - Range { - start: range.start + url_range.start - extra_range, - end: range.start + url_range.end + extra_range, - }, - without_brackets, - ); + f(cx, "this URL is not a hyperlink", url_range, without_brackets); } } -- cgit 1.4.1-3-g733a5