diff --git a/src/librustdoc/passes/lint/bare_urls.rs b/src/librustdoc/passes/lint/bare_urls.rs index ed9fcdfaaa4..251d13fbfd1 100644 --- a/src/librustdoc/passes/lint/bare_urls.rs +++ b/src/librustdoc/passes/lint/bare_urls.rs @@ -98,41 +98,50 @@ fn find_raw_urls( // For now, we only check "full" URLs (meaning, starting with "http://" or "https://"). for match_ in URL_SCHEME_HOST_REGEX.find_iter(text) { let mut url_range = match_.range(); - // We found the scheme and host. Find the path, now. + // We found the scheme and host. Find the path, query, or fragment. // We want to check for matching, balanced parens, // but regex isn't powerful enough for that. let mut paren_stack = Vec::with_capacity(3); - while let Some(&c) = text.as_bytes().get(url_range.end) { - if c == b'(' { - paren_stack.push(url_range.end); - } else if c == b')' { - if paren_stack.pop().is_none() { - break; - } - } else if !matches!( - c, - b'-' - | b'a'..=b'z' - | b'A'..=b'Z' - | b'0'..=b'9' - | b'@' - | b':' - | b'%' - | b'_' - | b'\\' - | b'+' - | b'.' - | b'~' - | b'#' - | b'?' - | b'&' - | b'/' - | b'=' - ) { + 'parts: while let Some(&sep) = text.as_bytes().get(url_range.end) { + // The hostname must be immediately followed by a path, query, + // or fragment-declaring separator. + if !matches!(sep, b'/' | b'?' | b'#') { break; } url_range.end += 1; + while let Some(&c) = text.as_bytes().get(url_range.end) { + if c == b'(' { + paren_stack.push(url_range.end); + } else if c == b')' { + // We assume the first unmatched parenthesis marks the end of the url, + // as urls rarely contain unbalanced parenthesis in practice. + if paren_stack.pop().is_none() { + break 'parts; + } + } else if !matches!( + c, + b'-' + | b'a'..=b'z' + | b'A'..=b'Z' + | b'0'..=b'9' + | b'@' + | b':' + | b'%' + | b'_' + | b'\\' + | b'+' + | b'.' + | b'~' + | b'&' + | b'=' + ) { + break; + } + url_range.end += 1; + } } + // We assume the first unmatched parenthesis marks the end of the url, + // as urls rarely contain unbalanced parenthesis in practice. if let Some(&end) = paren_stack.first() { url_range.end = end; } diff --git a/tests/rustdoc-ui/lints/bare-urls.fixed b/tests/rustdoc-ui/lints/bare-urls.fixed index 20d04a8a972..7a5c5635c01 100644 --- a/tests/rustdoc-ui/lints/bare-urls.fixed +++ b/tests/rustdoc-ui/lints/bare-urls.fixed @@ -143,3 +143,15 @@ pub fn nested_parens_unbalanced_5() {} /// See http://ex(amp)le.com/ pub fn nested_parens_cannot_appear_in_host() {} + +/// The path component of a URL must be separated from the rest by a `/`, +/// so the `=a` isn't part of the URL. +/// =a +//~^ ERROR this URL is not a hyperlink +pub fn equal_sign_after_host() {} + +/// A URL can, however, have a fragment or query without a slash. +/// or +//~^ ERROR this URL is not a hyperlink +//~| ERROR this URL is not a hyperlink +pub fn hash_or_question_after_host() {} diff --git a/tests/rustdoc-ui/lints/bare-urls.rs b/tests/rustdoc-ui/lints/bare-urls.rs index 12ab7b32e8b..3f493c60ee0 100644 --- a/tests/rustdoc-ui/lints/bare-urls.rs +++ b/tests/rustdoc-ui/lints/bare-urls.rs @@ -143,3 +143,15 @@ pub fn nested_parens_unbalanced_5() {} /// See http://ex(amp)le.com/ pub fn nested_parens_cannot_appear_in_host() {} + +/// The path component of a URL must be separated from the rest by a `/`, +/// so the `=a` isn't part of the URL. +/// https://foo.bar=a +//~^ ERROR this URL is not a hyperlink +pub fn equal_sign_after_host() {} + +/// A URL can, however, have a fragment or query without a slash. +/// https://foo.bar#a or https://foo.bar?a +//~^ ERROR this URL is not a hyperlink +//~| ERROR this URL is not a hyperlink +pub fn hash_or_question_after_host() {} diff --git a/tests/rustdoc-ui/lints/bare-urls.stderr b/tests/rustdoc-ui/lints/bare-urls.stderr index 1f39f90106f..954f95f1a0f 100644 --- a/tests/rustdoc-ui/lints/bare-urls.stderr +++ b/tests/rustdoc-ui/lints/bare-urls.stderr @@ -508,5 +508,41 @@ help: use an automatic link instead LL | /// See .) | + + -error: aborting due to 42 previous errors +error: this URL is not a hyperlink + --> $DIR/bare-urls.rs:149:5 + | +LL | /// https://foo.bar=a + | ^^^^^^^^^^^^^^^ + | + = note: bare URLs are not automatically turned into clickable links +help: use an automatic link instead + | +LL | /// =a + | + + + +error: this URL is not a hyperlink + --> $DIR/bare-urls.rs:154:5 + | +LL | /// https://foo.bar#a or https://foo.bar?a + | ^^^^^^^^^^^^^^^^^ + | + = note: bare URLs are not automatically turned into clickable links +help: use an automatic link instead + | +LL | /// or https://foo.bar?a + | + + + +error: this URL is not a hyperlink + --> $DIR/bare-urls.rs:154:26 + | +LL | /// https://foo.bar#a or https://foo.bar?a + | ^^^^^^^^^^^^^^^^^ + | + = note: bare URLs are not automatically turned into clickable links +help: use an automatic link instead + | +LL | /// https://foo.bar#a or + | + + + +error: aborting due to 45 previous errors