diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b17072df..e81e87bf0 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 diff --git a/book/src/lint_configuration.md b/book/src/lint_configuration.md index 9bf7b1949..efb481315 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 81afe18eb..b465c7566 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 1bfb57371..8d443dabc 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 44b878cec..dff47a2d0 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 @@ -24,12 +18,6 @@ error: unnecessary hashes around raw string literal 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 | @@ -48,12 +36,6 @@ error: unnecessary hashes around raw string literal 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 | @@ -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