Skip to content

Commit 4494b69

Browse files
committed
Auto merge of #11569 - Alexendoo:needless-raw-string-descr, r=llogiq
Describe the type of string in raw_strings lints changelog: none
2 parents 493ab53 + 6cdff10 commit 4494b69

7 files changed

+100
-73
lines changed

clippy_lints/src/raw_strings.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ impl EarlyLintPass for RawStrings {
7575
if !snippet(cx, expr.span, prefix).trim().starts_with(prefix) {
7676
return;
7777
}
78+
let descr = lit.kind.descr();
7879

7980
if !str.contains(['\\', '"']) {
8081
span_lint_and_then(
@@ -89,20 +90,17 @@ impl EarlyLintPass for RawStrings {
8990
let r_pos = expr.span.lo() + BytePos::from_usize(prefix.len() - 1);
9091
let start = start.with_lo(r_pos);
9192

92-
if end.is_empty() {
93-
diag.span_suggestion(
94-
start,
95-
"use a string literal instead",
96-
format!("\"{str}\""),
97-
Applicability::MachineApplicable,
98-
);
99-
} else {
100-
diag.multipart_suggestion(
101-
"try",
102-
vec![(start, String::new()), (end, String::new())],
103-
Applicability::MachineApplicable,
104-
);
93+
let mut remove = vec![(start, String::new())];
94+
// avoid debug ICE from empty suggestions
95+
if !end.is_empty() {
96+
remove.push((end, String::new()));
10597
}
98+
99+
diag.multipart_suggestion_verbose(
100+
format!("use a plain {descr} literal instead"),
101+
remove,
102+
Applicability::MachineApplicable,
103+
);
106104
},
107105
);
108106
if !matches!(cx.get_lint_level(NEEDLESS_RAW_STRINGS), rustc_lint::Allow) {
@@ -149,9 +147,9 @@ impl EarlyLintPass for RawStrings {
149147
let (start, end) = hash_spans(expr.span, prefix, req, max);
150148

151149
let message = match max - req {
152-
_ if req == 0 => "remove all the hashes around the literal".to_string(),
153-
1 => "remove one hash from both sides of the literal".to_string(),
154-
n => format!("remove {n} hashes from both sides of the literal"),
150+
_ if req == 0 => format!("remove all the hashes around the {descr} literal"),
151+
1 => format!("remove one hash from both sides of the {descr} literal"),
152+
n => format!("remove {n} hashes from both sides of the {descr} literal"),
155153
};
156154

157155
diag.multipart_suggestion(

tests/ui/needless_raw_string.fixed

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@ fn main() {
1818
multiline
1919
string
2020
";
21+
22+
"no hashes";
23+
b"no hashes";
24+
c"no hashes";
2125
}

tests/ui/needless_raw_string.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@ fn main() {
1818
multiline
1919
string
2020
"#;
21+
22+
r"no hashes";
23+
br"no hashes";
24+
cr"no hashes";
2125
}

tests/ui/needless_raw_string.stderr

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | r#"aaa"#;
66
|
77
= note: `-D clippy::needless-raw-strings` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::needless_raw_strings)]`
9-
help: try
9+
help: use a plain string literal instead
1010
|
1111
LL - r#"aaa"#;
1212
LL + "aaa";
@@ -18,7 +18,7 @@ error: unnecessary raw string literal
1818
LL | br#"aaa"#;
1919
| ^^^^^^^^^
2020
|
21-
help: try
21+
help: use a plain byte string literal instead
2222
|
2323
LL - br#"aaa"#;
2424
LL + b"aaa";
@@ -30,7 +30,7 @@ error: unnecessary raw string literal
3030
LL | cr#"aaa"#;
3131
| ^^^^^^^^^
3232
|
33-
help: try
33+
help: use a plain C string literal instead
3434
|
3535
LL - cr#"aaa"#;
3636
LL + c"aaa";
@@ -46,7 +46,7 @@ LL | | string
4646
LL | | "#;
4747
| |______^
4848
|
49-
help: try
49+
help: use a plain string literal instead
5050
|
5151
LL ~ "
5252
LL | a
@@ -55,5 +55,41 @@ LL | string
5555
LL ~ ";
5656
|
5757

58-
error: aborting due to 4 previous errors
58+
error: unnecessary raw string literal
59+
--> $DIR/needless_raw_string.rs:22:5
60+
|
61+
LL | r"no hashes";
62+
| ^^^^^^^^^^^^
63+
|
64+
help: use a plain string literal instead
65+
|
66+
LL - r"no hashes";
67+
LL + "no hashes";
68+
|
69+
70+
error: unnecessary raw string literal
71+
--> $DIR/needless_raw_string.rs:23:5
72+
|
73+
LL | br"no hashes";
74+
| ^^^^^^^^^^^^^
75+
|
76+
help: use a plain byte string literal instead
77+
|
78+
LL - br"no hashes";
79+
LL + b"no hashes";
80+
|
81+
82+
error: unnecessary raw string literal
83+
--> $DIR/needless_raw_string.rs:24:5
84+
|
85+
LL | cr"no hashes";
86+
| ^^^^^^^^^^^^^
87+
|
88+
help: use a plain C string literal instead
89+
|
90+
LL - cr"no hashes";
91+
LL + c"no hashes";
92+
|
93+
94+
error: aborting due to 7 previous errors
5995

tests/ui/needless_raw_string_hashes.stderr

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | r#"\aaa"#;
66
|
77
= note: `-D clippy::needless-raw-string-hashes` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::needless_raw_string_hashes)]`
9-
help: remove all the hashes around the literal
9+
help: remove all the hashes around the string literal
1010
|
1111
LL - r#"\aaa"#;
1212
LL + r"\aaa";
@@ -18,7 +18,7 @@ error: unnecessary hashes around raw string literal
1818
LL | r##"Hello "world"!"##;
1919
| ^^^^^^^^^^^^^^^^^^^^^
2020
|
21-
help: remove one hash from both sides of the literal
21+
help: remove one hash from both sides of the string literal
2222
|
2323
LL - r##"Hello "world"!"##;
2424
LL + r#"Hello "world"!"#;
@@ -30,7 +30,7 @@ error: unnecessary hashes around raw string literal
3030
LL | r######" "### "## "# "######;
3131
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3232
|
33-
help: remove 2 hashes from both sides of the literal
33+
help: remove 2 hashes from both sides of the string literal
3434
|
3535
LL - r######" "### "## "# "######;
3636
LL + r####" "### "## "# "####;
@@ -42,7 +42,7 @@ error: unnecessary hashes around raw string literal
4242
LL | r######" "aa" "# "## "######;
4343
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4444
|
45-
help: remove 3 hashes from both sides of the literal
45+
help: remove 3 hashes from both sides of the string literal
4646
|
4747
LL - r######" "aa" "# "## "######;
4848
LL + r###" "aa" "# "## "###;
@@ -54,7 +54,7 @@ error: unnecessary hashes around raw string literal
5454
LL | br#"\aaa"#;
5555
| ^^^^^^^^^^
5656
|
57-
help: remove all the hashes around the literal
57+
help: remove all the hashes around the byte string literal
5858
|
5959
LL - br#"\aaa"#;
6060
LL + br"\aaa";
@@ -66,7 +66,7 @@ error: unnecessary hashes around raw string literal
6666
LL | br##"Hello "world"!"##;
6767
| ^^^^^^^^^^^^^^^^^^^^^^
6868
|
69-
help: remove one hash from both sides of the literal
69+
help: remove one hash from both sides of the byte string literal
7070
|
7171
LL - br##"Hello "world"!"##;
7272
LL + br#"Hello "world"!"#;
@@ -78,7 +78,7 @@ error: unnecessary hashes around raw string literal
7878
LL | br######" "### "## "# "######;
7979
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8080
|
81-
help: remove 2 hashes from both sides of the literal
81+
help: remove 2 hashes from both sides of the byte string literal
8282
|
8383
LL - br######" "### "## "# "######;
8484
LL + br####" "### "## "# "####;
@@ -90,7 +90,7 @@ error: unnecessary hashes around raw string literal
9090
LL | br######" "aa" "# "## "######;
9191
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9292
|
93-
help: remove 3 hashes from both sides of the literal
93+
help: remove 3 hashes from both sides of the byte string literal
9494
|
9595
LL - br######" "aa" "# "## "######;
9696
LL + br###" "aa" "# "## "###;
@@ -102,7 +102,7 @@ error: unnecessary hashes around raw string literal
102102
LL | cr#"\aaa"#;
103103
| ^^^^^^^^^^
104104
|
105-
help: remove all the hashes around the literal
105+
help: remove all the hashes around the C string literal
106106
|
107107
LL - cr#"\aaa"#;
108108
LL + cr"\aaa";
@@ -114,7 +114,7 @@ error: unnecessary hashes around raw string literal
114114
LL | cr##"Hello "world"!"##;
115115
| ^^^^^^^^^^^^^^^^^^^^^^
116116
|
117-
help: remove one hash from both sides of the literal
117+
help: remove one hash from both sides of the C string literal
118118
|
119119
LL - cr##"Hello "world"!"##;
120120
LL + cr#"Hello "world"!"#;
@@ -126,7 +126,7 @@ error: unnecessary hashes around raw string literal
126126
LL | cr######" "### "## "# "######;
127127
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
128128
|
129-
help: remove 2 hashes from both sides of the literal
129+
help: remove 2 hashes from both sides of the C string literal
130130
|
131131
LL - cr######" "### "## "# "######;
132132
LL + cr####" "### "## "# "####;
@@ -138,7 +138,7 @@ error: unnecessary hashes around raw string literal
138138
LL | cr######" "aa" "# "## "######;
139139
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
140140
|
141-
help: remove 3 hashes from both sides of the literal
141+
help: remove 3 hashes from both sides of the C string literal
142142
|
143143
LL - cr######" "aa" "# "## "######;
144144
LL + cr###" "aa" "# "## "###;
@@ -154,7 +154,7 @@ LL | | string
154154
LL | | "#;
155155
| |______^
156156
|
157-
help: remove all the hashes around the literal
157+
help: remove all the hashes around the string literal
158158
|
159159
LL ~ r"
160160
LL | \a
@@ -169,7 +169,7 @@ error: unnecessary hashes around raw string literal
169169
LL | r###"rust"###;
170170
| ^^^^^^^^^^^^^
171171
|
172-
help: remove all the hashes around the literal
172+
help: remove all the hashes around the string literal
173173
|
174174
LL - r###"rust"###;
175175
LL + r"rust";
@@ -181,7 +181,7 @@ error: unnecessary hashes around raw string literal
181181
LL | r#"hello world"#;
182182
| ^^^^^^^^^^^^^^^^
183183
|
184-
help: remove all the hashes around the literal
184+
help: remove all the hashes around the string literal
185185
|
186186
LL - r#"hello world"#;
187187
LL + r"hello world";

tests/ui/write_literal_2.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@no-rustfix: overlapping suggestions
22
#![allow(unused_must_use)]
3-
#![warn(clippy::needless_raw_strings, clippy::write_literal)]
3+
#![warn(clippy::write_literal)]
44

55
use std::io::Write;
66

@@ -11,9 +11,7 @@ fn main() {
1111
//~^ ERROR: literal with an empty format string
1212
//~| NOTE: `-D clippy::write-literal` implied by `-D warnings`
1313
writeln!(v, r"{}", r"{hello}");
14-
//~^ ERROR: unnecessary raw string literal
15-
//~| NOTE: `-D clippy::needless-raw-strings` implied by `-D warnings`
16-
//~| ERROR: literal with an empty format string
14+
//~^ ERROR: literal with an empty format string
1715
writeln!(v, "{}", '\'');
1816
//~^ ERROR: literal with an empty format string
1917
writeln!(v, "{}", '"');
@@ -26,8 +24,8 @@ fn main() {
2624
v,
2725
"some {}",
2826
"hello \
29-
//~^ ERROR: literal with an empty format string
30-
world!"
27+
world!",
28+
//~^^ ERROR: literal with an empty format string
3129
);
3230
writeln!(
3331
v,

0 commit comments

Comments
 (0)