Skip to content

Commit 6c2ef3d

Browse files
committed
don't lint needless_raw_string_hashes when it's unnecessary
1 parent c6759d9 commit 6c2ef3d

File tree

4 files changed

+30
-36
lines changed

4 files changed

+30
-36
lines changed

book/src/lint_configuration.md

+10
Original file line numberDiff line numberDiff line change
@@ -643,3 +643,13 @@ The byte size a `T` in `Box<T>` can have, below which it triggers the `clippy::u
643643
* [`unnecessary_box_returns`](https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_box_returns)
644644

645645

646+
## `allow-one-hash-in-raw-string`
647+
Whether to allow `r#""#` when `r""` can be used
648+
649+
**Default Value:** `false` (`bool`)
650+
651+
---
652+
**Affected lints:**
653+
* [`unnecessary_raw_string_hashes`](https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_raw_string_hashes)
654+
655+

clippy_lints/src/raw_strings.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,20 @@ impl EarlyLintPass for RawStrings {
7171
return;
7272
}
7373

74+
if !lit.symbol.as_str().contains(['\\', '"']) {
75+
span_lint_and_sugg(
76+
cx,
77+
NEEDLESS_RAW_STRING,
78+
expr.span,
79+
"unnecessary raw string literal",
80+
"try",
81+
format!("{}\"{}\"", prefix.replace('r', ""), lit.symbol),
82+
Applicability::MachineApplicable,
83+
);
84+
85+
return;
86+
}
87+
7488
#[expect(clippy::cast_possible_truncation)]
7589
let req = lit.symbol.as_str().as_bytes()
7690
.split(|&b| b == b'"')
@@ -92,18 +106,6 @@ impl EarlyLintPass for RawStrings {
92106
Applicability::MachineApplicable,
93107
);
94108
}
95-
96-
if !lit.symbol.as_str().contains(['\\', '"']) {
97-
span_lint_and_sugg(
98-
cx,
99-
NEEDLESS_RAW_STRING,
100-
expr.span,
101-
"unnecessary raw string literal",
102-
"try",
103-
format!("{}\"{}\"", prefix.replace('r', ""), lit.symbol),
104-
Applicability::MachineApplicable,
105-
);
106-
}
107109
}
108110
}
109111
}

tests/ui/needless_raw_string_hashes.fixed

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
#![feature(c_str_literals)]
55

66
fn main() {
7-
r"aaa";
7+
r#"aaa"#;
88
r#"Hello "world"!"#;
99
r####" "### "## "# "####;
1010
r###" "aa" "# "## "###;
11-
br"aaa";
11+
br#"aaa"#;
1212
br#"Hello "world"!"#;
1313
br####" "### "## "# "####;
1414
br###" "aa" "# "## "###;
15-
cr"aaa";
15+
cr#"aaa"#;
1616
cr#"Hello "world"!"#;
1717
cr####" "### "## "# "####;
1818
cr###" "aa" "# "## "###;

tests/ui/needless_raw_string_hashes.stderr

+3-21
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
error: unnecessary hashes around raw string literal
2-
--> $DIR/needless_raw_string_hashes.rs:7:5
3-
|
4-
LL | r#"aaa"#;
5-
| ^^^^^^^^ help: try: `r"aaa"`
6-
|
7-
= note: `-D clippy::needless-raw-string-hashes` implied by `-D warnings`
8-
91
error: unnecessary hashes around raw string literal
102
--> $DIR/needless_raw_string_hashes.rs:8:5
113
|
124
LL | r##"Hello "world"!"##;
135
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `r#"Hello "world"!"#`
6+
|
7+
= note: `-D clippy::needless-raw-string-hashes` implied by `-D warnings`
148

159
error: unnecessary hashes around raw string literal
1610
--> $DIR/needless_raw_string_hashes.rs:9:5
@@ -24,12 +18,6 @@ error: unnecessary hashes around raw string literal
2418
LL | r######" "aa" "# "## "######;
2519
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `r###" "aa" "# "## "###`
2620

27-
error: unnecessary hashes around raw string literal
28-
--> $DIR/needless_raw_string_hashes.rs:11:5
29-
|
30-
LL | br#"aaa"#;
31-
| ^^^^^^^^^ help: try: `br"aaa"`
32-
3321
error: unnecessary hashes around raw string literal
3422
--> $DIR/needless_raw_string_hashes.rs:12:5
3523
|
@@ -48,12 +36,6 @@ error: unnecessary hashes around raw string literal
4836
LL | br######" "aa" "# "## "######;
4937
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `br###" "aa" "# "## "###`
5038

51-
error: unnecessary hashes around raw string literal
52-
--> $DIR/needless_raw_string_hashes.rs:15:5
53-
|
54-
LL | cr#"aaa"#;
55-
| ^^^^^^^^^ help: try: `cr"aaa"`
56-
5739
error: unnecessary hashes around raw string literal
5840
--> $DIR/needless_raw_string_hashes.rs:16:5
5941
|
@@ -72,5 +54,5 @@ error: unnecessary hashes around raw string literal
7254
LL | cr######" "aa" "# "## "######;
7355
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `cr###" "aa" "# "## "###`
7456

75-
error: aborting due to 12 previous errors
57+
error: aborting due to 9 previous errors
7658

0 commit comments

Comments
 (0)