about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md1
-rw-r--r--book/src/lint_configuration.md10
-rw-r--r--clippy_lints/src/raw_strings.rs26
-rw-r--r--tests/ui/needless_raw_string_hashes.fixed6
-rw-r--r--tests/ui/needless_raw_string_hashes.stderr24
5 files changed, 31 insertions, 36 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5b17072df40..e81e87bf077 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5414,4 +5414,5 @@ Released 2018-09-13
 [`min-ident-chars-threshold`]: https://doc.rust-lang.org/clippy/lint_configuration.html#min-ident-chars-threshold
 [`accept-comment-above-statement`]: https://doc.rust-lang.org/clippy/lint_configuration.html#accept-comment-above-statement
 [`accept-comment-above-attributes`]: https://doc.rust-lang.org/clippy/lint_configuration.html#accept-comment-above-attributes
+[`allow-one-hash-in-raw-string`]: https://doc.rust-lang.org/clippy/lint_configuration.html#allow-one-hash-in-raw-string
 <!-- end autogenerated links to configuration documentation -->
diff --git a/book/src/lint_configuration.md b/book/src/lint_configuration.md
index 9bf7b1949c4..efb48131502 100644
--- a/book/src/lint_configuration.md
+++ b/book/src/lint_configuration.md
@@ -717,3 +717,13 @@ Whether to accept a safety comment to be placed above the attributes for the `un
 * [`undocumented_unsafe_blocks`](https://rust-lang.github.io/rust-clippy/master/index.html#undocumented_unsafe_blocks)
 
 
+## `allow-one-hash-in-raw-string`
+Whether to allow `r#""#` when `r""` can be used
+
+**Default Value:** `false` (`bool`)
+
+---
+**Affected lints:**
+* [`unnecessary_raw_string_hashes`](https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_raw_string_hashes)
+
+
diff --git a/clippy_lints/src/raw_strings.rs b/clippy_lints/src/raw_strings.rs
index 81afe18ebc6..b465c7566f0 100644
--- a/clippy_lints/src/raw_strings.rs
+++ b/clippy_lints/src/raw_strings.rs
@@ -71,6 +71,20 @@ impl EarlyLintPass for RawStrings {
                 return;
             }
 
+            if !lit.symbol.as_str().contains(['\\', '"']) {
+                span_lint_and_sugg(
+                    cx,
+                    NEEDLESS_RAW_STRING,
+                    expr.span,
+                    "unnecessary raw string literal",
+                    "try",
+                    format!("{}\"{}\"", prefix.replace('r', ""), lit.symbol),
+                    Applicability::MachineApplicable,
+                );
+
+                return;
+            }
+
             #[expect(clippy::cast_possible_truncation)]
             let req = lit.symbol.as_str().as_bytes()
                 .split(|&b| b == b'"')
@@ -92,18 +106,6 @@ impl EarlyLintPass for RawStrings {
                     Applicability::MachineApplicable,
                 );
             }
-
-            if !lit.symbol.as_str().contains(['\\', '"']) {
-                span_lint_and_sugg(
-                    cx,
-                    NEEDLESS_RAW_STRING,
-                    expr.span,
-                    "unnecessary raw string literal",
-                    "try",
-                    format!("{}\"{}\"", prefix.replace('r', ""), lit.symbol),
-                    Applicability::MachineApplicable,
-                );
-            }
         }
     }
 }
diff --git a/tests/ui/needless_raw_string_hashes.fixed b/tests/ui/needless_raw_string_hashes.fixed
index 1bfb573719d..8d443dabcb7 100644
--- a/tests/ui/needless_raw_string_hashes.fixed
+++ b/tests/ui/needless_raw_string_hashes.fixed
@@ -4,15 +4,15 @@
 #![feature(c_str_literals)]
 
 fn main() {
-    r"aaa";
+    r#"aaa"#;
     r#"Hello "world"!"#;
     r####" "### "## "# "####;
     r###" "aa" "# "## "###;
-    br"aaa";
+    br#"aaa"#;
     br#"Hello "world"!"#;
     br####" "### "## "# "####;
     br###" "aa" "# "## "###;
-    cr"aaa";
+    cr#"aaa"#;
     cr#"Hello "world"!"#;
     cr####" "### "## "# "####;
     cr###" "aa" "# "## "###;
diff --git a/tests/ui/needless_raw_string_hashes.stderr b/tests/ui/needless_raw_string_hashes.stderr
index 44b878cec40..dff47a2d042 100644
--- a/tests/ui/needless_raw_string_hashes.stderr
+++ b/tests/ui/needless_raw_string_hashes.stderr
@@ -1,16 +1,10 @@
 error: unnecessary hashes around raw string literal
-  --> $DIR/needless_raw_string_hashes.rs:7:5
-   |
-LL |     r#"aaa"#;
-   |     ^^^^^^^^ help: try: `r"aaa"`
-   |
-   = note: `-D clippy::needless-raw-string-hashes` implied by `-D warnings`
-
-error: unnecessary hashes around raw string literal
   --> $DIR/needless_raw_string_hashes.rs:8:5
    |
 LL |     r##"Hello "world"!"##;
    |     ^^^^^^^^^^^^^^^^^^^^^ help: try: `r#"Hello "world"!"#`
+   |
+   = note: `-D clippy::needless-raw-string-hashes` implied by `-D warnings`
 
 error: unnecessary hashes around raw string literal
   --> $DIR/needless_raw_string_hashes.rs:9:5
@@ -25,12 +19,6 @@ LL |     r######" "aa" "# "## "######;
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `r###" "aa" "# "## "###`
 
 error: unnecessary hashes around raw string literal
-  --> $DIR/needless_raw_string_hashes.rs:11:5
-   |
-LL |     br#"aaa"#;
-   |     ^^^^^^^^^ help: try: `br"aaa"`
-
-error: unnecessary hashes around raw string literal
   --> $DIR/needless_raw_string_hashes.rs:12:5
    |
 LL |     br##"Hello "world"!"##;
@@ -49,12 +37,6 @@ LL |     br######" "aa" "# "## "######;
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `br###" "aa" "# "## "###`
 
 error: unnecessary hashes around raw string literal
-  --> $DIR/needless_raw_string_hashes.rs:15:5
-   |
-LL |     cr#"aaa"#;
-   |     ^^^^^^^^^ help: try: `cr"aaa"`
-
-error: unnecessary hashes around raw string literal
   --> $DIR/needless_raw_string_hashes.rs:16:5
    |
 LL |     cr##"Hello "world"!"##;
@@ -72,5 +54,5 @@ error: unnecessary hashes around raw string literal
 LL |     cr######" "aa" "# "## "######;
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `cr###" "aa" "# "## "###`
 
-error: aborting due to 12 previous errors
+error: aborting due to 9 previous errors