Skip to content

Commit 521812c

Browse files
max-sixtyclaude
andauthored
Support clippy::needless_raw_strings lint (#828)
this would fix #827 @mitsuhiko do you have thoughts? I'm mildly positive on it but will leave it open for a while because I think you voiced weak skepticism in the past. Claude comment below --- ## Changes - Modified `to_inline()` to only use raw strings when content contains backslashes or quotes - Removed newlines from raw string trigger condition - Added `clippy::needless_raw_strings` lint to the project - Force-updated all snapshots to use the new logic - Added allow attribute for vendored YAML code ## Test Results ✅ All tests pass ✅ Zero clippy warnings in production code ✅ 44+ needless raw strings converted to regular strings 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude <[email protected]>
1 parent 5822a95 commit 521812c

File tree

17 files changed

+276
-111
lines changed

17 files changed

+276
-111
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ target
44
.aider*
55
.env
66
.idea
7+
*.rlib

cargo-insta/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![warn(clippy::doc_markdown)]
2+
#![warn(clippy::needless_raw_strings)]
23
#![warn(rustdoc::all)]
34

45
//! <div align="center">

cargo-insta/tests/functional/binary.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fn test_binary_snapshot() {
2323

2424
assert!(!&output.status.success());
2525

26-
assert_snapshot!(test_project.file_tree_diff(), @r"
26+
assert_snapshot!(test_project.file_tree_diff(), @"
2727
--- Original file tree
2828
+++ Updated file tree
2929
@@ -1,3 +1,7 @@
@@ -62,7 +62,7 @@ fn test_binary_snapshot() {
6262

6363
assert!(&output.status.success());
6464

65-
assert_snapshot!(test_project.file_tree_diff(), @r"
65+
assert_snapshot!(test_project.file_tree_diff(), @"
6666
--- Original file tree
6767
+++ Updated file tree
6868
@@ -1,3 +1,7 @@
@@ -118,7 +118,7 @@ fn test_binary_snapshot() {
118118

119119
assert!(!&output.status.success());
120120

121-
assert_snapshot!(test_project.file_tree_diff(), @r"
121+
assert_snapshot!(test_project.file_tree_diff(), @"
122122
--- Original file tree
123123
+++ Updated file tree
124124
@@ -1,3 +1,9 @@
@@ -141,7 +141,7 @@ fn test_binary_snapshot() {
141141

142142
assert!(&output.status.success());
143143

144-
assert_snapshot!(test_project.file_tree_diff(), @r"
144+
assert_snapshot!(test_project.file_tree_diff(), @"
145145
--- Original file tree
146146
+++ Updated file tree
147147
@@ -1,3 +1,7 @@
@@ -192,7 +192,7 @@ fn test_binary_snapshot() {
192192
.status
193193
.success());
194194

195-
assert_snapshot!(test_project.file_tree_diff(), @r"
195+
assert_snapshot!(test_project.file_tree_diff(), @"
196196
--- Original file tree
197197
+++ Updated file tree
198198
@@ -1,3 +1,5 @@
@@ -229,7 +229,7 @@ fn test() {
229229
.unwrap();
230230

231231
assert!(&output.status.success());
232-
assert_snapshot!(test_project.file_tree_diff(), @r"
232+
assert_snapshot!(test_project.file_tree_diff(), @"
233233
--- Original file tree
234234
+++ Updated file tree
235235
@@ -1,3 +1,6 @@
@@ -259,7 +259,7 @@ fn test() {
259259
.unwrap();
260260

261261
assert!(&output.status.success());
262-
assert_snapshot!(test_project.file_tree_diff(), @r"
262+
assert_snapshot!(test_project.file_tree_diff(), @"
263263
--- Original file tree
264264
+++ Updated file tree
265265
@@ -1,3 +1,7 @@
@@ -298,7 +298,7 @@ fn test() {
298298
.unwrap();
299299

300300
assert!(&output.status.success());
301-
assert_snapshot!(test_project.file_tree_diff(), @r"
301+
assert_snapshot!(test_project.file_tree_diff(), @"
302302
--- Original file tree
303303
+++ Updated file tree
304304
@@ -1,3 +1,7 @@
@@ -329,7 +329,7 @@ fn test() {
329329
.unwrap();
330330

331331
assert!(&output.status.success());
332-
assert_snapshot!(test_project.file_tree_diff(), @r"
332+
assert_snapshot!(test_project.file_tree_diff(), @"
333333
--- Original file tree
334334
+++ Updated file tree
335335
@@ -1,3 +1,6 @@
@@ -369,7 +369,7 @@ fn test_snapshot() {
369369

370370
test_project.update_file("src/lib.rs", "".to_string());
371371

372-
assert_snapshot!(test_project.file_tree_diff(), @r"
372+
assert_snapshot!(test_project.file_tree_diff(), @"
373373
--- Original file tree
374374
+++ Updated file tree
375375
@@ -1,3 +1,7 @@
@@ -398,7 +398,7 @@ fn test_snapshot() {
398398
assert!(&output.status.success());
399399

400400
// We should now see the unreferenced snapshot deleted
401-
assert_snapshot!(test_project.file_tree_diff(), @r"
401+
assert_snapshot!(test_project.file_tree_diff(), @"
402402
--- Original file tree
403403
+++ Updated file tree
404404
@@ -1,3 +1,5 @@

cargo-insta/tests/functional/delete_pending.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ fn test_snapshot_file() {
4242
.status
4343
.success());
4444

45-
assert_snapshot!(test_project.file_tree_diff(), @r"
45+
assert_snapshot!(test_project.file_tree_diff(), @"
4646
--- Original file tree
4747
+++ Updated file tree
4848
@@ -1,3 +1,6 @@
@@ -66,7 +66,7 @@ fn test_snapshot_file() {
6666
.status
6767
.success());
6868

69-
assert_snapshot!(test_project.file_tree_diff(), @r"
69+
assert_snapshot!(test_project.file_tree_diff(), @"
7070
--- Original file tree
7171
+++ Updated file tree
7272
@@ -1,3 +1,5 @@
@@ -111,7 +111,7 @@ fn test_file_snapshot() {
111111
.success());
112112

113113
// Verify the file tree has a `.snap` file
114-
assert_snapshot!(test_project.file_tree_diff(), @r"
114+
assert_snapshot!(test_project.file_tree_diff(), @"
115115
--- Original file tree
116116
+++ Updated file tree
117117
@@ -1,3 +1,6 @@
@@ -152,7 +152,7 @@ fn test_file_snapshot() {
152152
.success());
153153

154154
// Verify pending snapshots exist
155-
assert_snapshot!(test_project.file_tree_diff(), @r"
155+
assert_snapshot!(test_project.file_tree_diff(), @"
156156
--- Original file tree
157157
+++ Updated file tree
158158
@@ -1,3 +1,8 @@
@@ -176,7 +176,7 @@ fn test_file_snapshot() {
176176
.success());
177177

178178
// Pending snapshots should be deleted
179-
assert_snapshot!(test_project.file_tree_diff(), @r"
179+
assert_snapshot!(test_project.file_tree_diff(), @"
180180
--- Original file tree
181181
+++ Updated file tree
182182
@@ -1,3 +1,6 @@
@@ -198,7 +198,7 @@ fn test_file_snapshot() {
198198
.success());
199199

200200
// They should be back...
201-
assert_snapshot!(test_project.file_tree_diff(), @r"
201+
assert_snapshot!(test_project.file_tree_diff(), @"
202202
--- Original file tree
203203
+++ Updated file tree
204204
@@ -1,3 +1,8 @@
@@ -241,7 +241,7 @@ fn test_file_snapshot() {
241241
.success());
242242

243243
// Verify the pending snapshots are deleted
244-
assert_snapshot!(test_project.file_tree_diff(), @r"
244+
assert_snapshot!(test_project.file_tree_diff(), @"
245245
--- Original file tree
246246
+++ Updated file tree
247247
@@ -1,3 +1,6 @@

cargo-insta/tests/functional/glob_filter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ fn test_glob_snapshots() {
6060
names.join("\n")
6161
};
6262

63-
assert_snapshot!(get_snapshot_list(), @r###"
63+
assert_snapshot!(get_snapshot_list(), @"
6464
6565
6666
67-
"###);
67+
");
6868

6969
// Clean and run with INSTA_GLOB_FILTER to filter to single file
7070
std::fs::remove_dir_all(&snapshot_dir).unwrap();

cargo-insta/tests/functional/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ mod glob_filter;
7575
mod inline;
7676
mod inline_snapshot_trimming;
7777
mod nextest_doctest;
78+
mod raw_strings;
7879
mod test_runner_fallback;
7980
mod test_workspace_source_path;
8081
mod unreferenced;
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
use insta::assert_snapshot;
2+
3+
use crate::TestFiles;
4+
5+
/// Test that needless raw strings work as inputs and are converted to regular strings in outputs
6+
#[test]
7+
fn test_needless_raw_strings_conversion() {
8+
let test_project = TestFiles::new()
9+
.add_cargo_toml("test_needless_raw_strings")
10+
.add_file(
11+
"src/lib.rs",
12+
r#####"
13+
#[test]
14+
fn test_single_line() {
15+
// These raw strings don't contain backslashes or quotes, so they're needless
16+
insta::assert_snapshot!(r#"single line should fit on a single line"#, @"");
17+
insta::assert_snapshot!(r##"single line should fit on a single line, even if it's really really really really really really really really really long"##, @"");
18+
}
19+
20+
#[test]
21+
fn test_multiline_only() {
22+
// Multiline content without quotes or backslashes
23+
insta::assert_snapshot!(r#"multiline content starting on first line
24+
25+
final line
26+
"#, @"");
27+
}
28+
29+
#[test]
30+
fn test_with_quotes_needs_raw() {
31+
// This one needs raw strings because it contains quotes
32+
insta::assert_snapshot!(r#"content with "quotes""#, @"");
33+
}
34+
35+
#[test]
36+
fn test_with_backslash_needs_raw() {
37+
// This one needs raw strings because it contains backslashes
38+
insta::assert_snapshot!(r"content with \backslash", @"");
39+
}
40+
"#####
41+
.to_string(),
42+
)
43+
.create_project();
44+
45+
let output = test_project
46+
.insta_cmd()
47+
.args(["test", "--accept"])
48+
.output()
49+
.unwrap();
50+
51+
assert!(&output.status.success());
52+
53+
// Verify that needless raw strings are converted to regular strings,
54+
// but necessary raw strings are preserved
55+
assert_snapshot!(test_project.diff("src/lib.rs"), @r###"
56+
--- Original: src/lib.rs
57+
+++ Updated: src/lib.rs
58+
@@ -2,8 +2,8 @@
59+
#[test]
60+
fn test_single_line() {
61+
// These raw strings don't contain backslashes or quotes, so they're needless
62+
- insta::assert_snapshot!(r#"single line should fit on a single line"#, @"");
63+
- insta::assert_snapshot!(r##"single line should fit on a single line, even if it's really really really really really really really really really long"##, @"");
64+
+ insta::assert_snapshot!(r#"single line should fit on a single line"#, @"single line should fit on a single line");
65+
+ insta::assert_snapshot!(r##"single line should fit on a single line, even if it's really really really really really really really really really long"##, @"single line should fit on a single line, even if it's really really really really really really really really really long");
66+
}
67+
68+
#[test]
69+
@@ -12,17 +12,21 @@
70+
insta::assert_snapshot!(r#"multiline content starting on first line
71+
72+
final line
73+
- "#, @"");
74+
+ "#, @"
75+
+ multiline content starting on first line
76+
+
77+
+ final line
78+
+ ");
79+
}
80+
81+
#[test]
82+
fn test_with_quotes_needs_raw() {
83+
// This one needs raw strings because it contains quotes
84+
- insta::assert_snapshot!(r#"content with "quotes""#, @"");
85+
+ insta::assert_snapshot!(r#"content with "quotes""#, @r#"content with "quotes""#);
86+
}
87+
88+
#[test]
89+
fn test_with_backslash_needs_raw() {
90+
// This one needs raw strings because it contains backslashes
91+
- insta::assert_snapshot!(r"content with \backslash", @"");
92+
+ insta::assert_snapshot!(r"content with \backslash", @r"content with \backslash");
93+
}
94+
"###);
95+
}
96+
97+
/// Test YAML format with multiline content (no quotes or backslashes)
98+
#[test]
99+
fn test_yaml_multiline_needless_raw() {
100+
let test_project = TestFiles::new()
101+
.add_file(
102+
"Cargo.toml",
103+
r#"
104+
[package]
105+
name = "test_yaml_needless_raw"
106+
version = "0.1.0"
107+
edition = "2021"
108+
109+
[dependencies]
110+
insta = { path = '$PROJECT_PATH', features=["yaml"] }
111+
"#
112+
.to_string(),
113+
)
114+
.add_file(
115+
"src/lib.rs",
116+
r#####"
117+
#[test]
118+
fn test_yaml_output() {
119+
// Input uses needless raw string, output should be regular string
120+
insta::assert_snapshot!(r#"---
121+
This is invalid yaml:
122+
{
123+
{
124+
---
125+
"#, @"");
126+
}
127+
"#####
128+
.to_string(),
129+
)
130+
.create_project();
131+
132+
let output = test_project
133+
.insta_cmd()
134+
.args(["test", "--accept"])
135+
.output()
136+
.unwrap();
137+
138+
assert!(&output.status.success());
139+
140+
// The output should use regular strings (not raw) since it doesn't contain quotes or backslashes
141+
assert_snapshot!(test_project.diff("src/lib.rs"), @r##"
142+
--- Original: src/lib.rs
143+
+++ Updated: src/lib.rs
144+
@@ -7,5 +7,11 @@
145+
{
146+
{
147+
---
148+
- "#, @"");
149+
+ "#, @"
150+
+ ---
151+
+ This is invalid yaml:
152+
+ {
153+
+ {
154+
+ ---
155+
+ ");
156+
}
157+
"##);
158+
}

cargo-insta/tests/functional/unreferenced.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ Unused snapshot
174174
)
175175
.unwrap();
176176

177-
insta::assert_snapshot!(test_project.file_tree_diff(), @r"
177+
insta::assert_snapshot!(test_project.file_tree_diff(), @"
178178
--- Original file tree
179179
+++ Updated file tree
180180
@@ -1,3 +1,7 @@
@@ -203,7 +203,7 @@ Unused snapshot
203203
assert!(&output.status.success());
204204

205205
// We should now see the unreferenced snapshot deleted
206-
insta::assert_snapshot!(test_project.file_tree_diff(), @r"
206+
insta::assert_snapshot!(test_project.file_tree_diff(), @"
207207
--- Original file tree
208208
+++ Updated file tree
209209
@@ -1,3 +1,6 @@

0 commit comments

Comments
 (0)