From cb52d19ce106f2ba2c4e1cca860757170c1db2ed Mon Sep 17 00:00:00 2001 From: Centri3 <114838443+Centri3@users.noreply.github.com> Date: Fri, 9 Jun 2023 21:40:09 -0500 Subject: [PATCH] don't lint `needless_raw_string_hashes` when it's unnecessary --- CHANGELOG.md | 1 + book/src/lint_configuration.md | 10 +++++++++ clippy_lints/src/raw_strings.rs | 26 ++++++++++++---------- tests/ui/needless_raw_string_hashes.fixed | 6 ++--- tests/ui/needless_raw_string_hashes.stderr | 24 +++----------------- 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 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 @@ fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) { 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 @@ fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) { 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 @@ -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