about summary refs log tree commit diff
diff options
context:
space:
mode:
authorHamir Mahal <hamirmahal@gmail.com>2024-05-24 14:30:42 -0700
committerHamir Mahal <hamirmahal@gmail.com>2024-05-24 14:30:42 -0700
commit17cc0a3a7d286b36a5745aafbfff7ac662270b5b (patch)
tree502c4f55cbd9337c106bfb4c44ef5ccd045e7877
parentf16317e9cc2bd8308a1cc1fa57ec2b8deab0131f (diff)
downloadrust-17cc0a3a7d286b36a5745aafbfff7ac662270b5b.tar.gz
rust-17cc0a3a7d286b36a5745aafbfff7ac662270b5b.zip
feat: auto-fix for `bare URLs` in doc comments
-rw-r--r--clippy_lints/src/doc/markdown.rs8
-rw-r--r--tests/ui/doc/doc-fixable.fixed3
-rw-r--r--tests/ui/doc/doc-fixable.rs3
-rw-r--r--tests/ui/doc/doc-fixable.stderr8
4 files changed, 18 insertions, 4 deletions
diff --git a/clippy_lints/src/doc/markdown.rs b/clippy_lints/src/doc/markdown.rs
index 1add02af310..8a497b5ab0d 100644
--- a/clippy_lints/src/doc/markdown.rs
+++ b/clippy_lints/src/doc/markdown.rs
@@ -1,4 +1,4 @@
-use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
+use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
 use clippy_utils::source::snippet_with_applicability;
 use rustc_data_structures::fx::FxHashSet;
 use rustc_errors::{Applicability, SuggestionStyle};
@@ -92,13 +92,15 @@ fn check_word(cx: &LateContext<'_>, word: &str, span: Span, code_level: isize, b
     if let Ok(url) = Url::parse(word) {
         // try to get around the fact that `foo::bar` parses as a valid URL
         if !url.cannot_be_a_base() {
-            span_lint(
+            span_lint_and_sugg(
                 cx,
                 DOC_MARKDOWN,
                 span,
                 "you should put bare URLs between `<`/`>` or make a proper Markdown link",
+                "try",
+                format!("<{word}>"),
+                Applicability::MachineApplicable,
             );
-
             return;
         }
     }
diff --git a/tests/ui/doc/doc-fixable.fixed b/tests/ui/doc/doc-fixable.fixed
index 7e22c847b1b..84673f1f43f 100644
--- a/tests/ui/doc/doc-fixable.fixed
+++ b/tests/ui/doc/doc-fixable.fixed
@@ -240,3 +240,6 @@ extern {
     /// `foo()`
     fn in_extern();
 }
+
+/// <https://github.com/rust-lang/rust-clippy/pull/12836>
+fn check_autofix_for_base_urls() {}
diff --git a/tests/ui/doc/doc-fixable.rs b/tests/ui/doc/doc-fixable.rs
index 3e2cb0df54b..4d017a99e0f 100644
--- a/tests/ui/doc/doc-fixable.rs
+++ b/tests/ui/doc/doc-fixable.rs
@@ -240,3 +240,6 @@ extern {
     /// foo()
     fn in_extern();
 }
+
+/// https://github.com/rust-lang/rust-clippy/pull/12836
+fn check_autofix_for_base_urls() {}
diff --git a/tests/ui/doc/doc-fixable.stderr b/tests/ui/doc/doc-fixable.stderr
index cd2228c47e3..a9263f62d38 100644
--- a/tests/ui/doc/doc-fixable.stderr
+++ b/tests/ui/doc/doc-fixable.stderr
@@ -363,5 +363,11 @@ help: try
 LL |     /// `foo()`
    |         ~~~~~~~
 
-error: aborting due to 33 previous errors
+error: you should put bare URLs between `<`/`>` or make a proper Markdown link
+  --> tests/ui/doc/doc-fixable.rs:244:5
+   |
+LL | /// https://github.com/rust-lang/rust-clippy/pull/12836
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `<https://github.com/rust-lang/rust-clippy/pull/12836>`
+
+error: aborting due to 34 previous errors