don't lint needless_raw_string_hashes when it's unnecessary

This commit is contained in:
Centri3 2023-06-09 21:40:09 -05:00 committed by Catherine
parent ec765d9516
commit cb52d19ce1
5 changed files with 31 additions and 36 deletions

View File

@ -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 [`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-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 [`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 --> <!-- end autogenerated links to configuration documentation -->

View File

@ -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) * [`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)

View File

@ -71,6 +71,20 @@ fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
return; 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)] #[expect(clippy::cast_possible_truncation)]
let req = lit.symbol.as_str().as_bytes() let req = lit.symbol.as_str().as_bytes()
.split(|&b| b == b'"') .split(|&b| b == b'"')
@ -92,18 +106,6 @@ fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
Applicability::MachineApplicable, 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,
);
}
} }
} }
} }

View File

@ -4,15 +4,15 @@
#![feature(c_str_literals)] #![feature(c_str_literals)]
fn main() { fn main() {
r"aaa"; r#"aaa"#;
r#"Hello "world"!"#; r#"Hello "world"!"#;
r####" "### "## "# "####; r####" "### "## "# "####;
r###" "aa" "# "## "###; r###" "aa" "# "## "###;
br"aaa"; br#"aaa"#;
br#"Hello "world"!"#; br#"Hello "world"!"#;
br####" "### "## "# "####; br####" "### "## "# "####;
br###" "aa" "# "## "###; br###" "aa" "# "## "###;
cr"aaa"; cr#"aaa"#;
cr#"Hello "world"!"#; cr#"Hello "world"!"#;
cr####" "### "## "# "####; cr####" "### "## "# "####;
cr###" "aa" "# "## "###; cr###" "aa" "# "## "###;

View File

@ -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 error: unnecessary hashes around raw string literal
--> $DIR/needless_raw_string_hashes.rs:8:5 --> $DIR/needless_raw_string_hashes.rs:8:5
| |
LL | r##"Hello "world"!"##; LL | r##"Hello "world"!"##;
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `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 error: unnecessary hashes around raw string literal
--> $DIR/needless_raw_string_hashes.rs:9:5 --> $DIR/needless_raw_string_hashes.rs:9:5
@ -24,12 +18,6 @@ error: unnecessary hashes around raw string literal
LL | r######" "aa" "# "## "######; LL | r######" "aa" "# "## "######;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `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 error: unnecessary hashes around raw string literal
--> $DIR/needless_raw_string_hashes.rs:12:5 --> $DIR/needless_raw_string_hashes.rs:12:5
| |
@ -48,12 +36,6 @@ error: unnecessary hashes around raw string literal
LL | br######" "aa" "# "## "######; LL | br######" "aa" "# "## "######;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `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 error: unnecessary hashes around raw string literal
--> $DIR/needless_raw_string_hashes.rs:16:5 --> $DIR/needless_raw_string_hashes.rs:16:5
| |
@ -72,5 +54,5 @@ error: unnecessary hashes around raw string literal
LL | cr######" "aa" "# "## "######; LL | cr######" "aa" "# "## "######;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `cr###" "aa" "# "## "###` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `cr###" "aa" "# "## "###`
error: aborting due to 12 previous errors error: aborting due to 9 previous errors