Skip to content

Commit ea9c598

Browse files
giacomocavalierilpil
authored andcommitted
get rid of write! macro for appending to strings
1 parent 5184d2b commit ea9c598

12 files changed

Lines changed: 147 additions & 96 deletions

File tree

compiler-cli/src/add.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,12 @@ fn read_toml_edit(name: &Utf8Path) -> Result<toml_edit::DocumentMut, Error> {
114114

115115
fn version_to_string(version: &Version) -> String {
116116
let mut text = String::new();
117-
let _ = write!(
117+
write!(
118118
text,
119119
"{}.{}.{}",
120120
version.major, version.minor, version.patch
121-
);
121+
)
122+
.expect("write to a string");
122123

123124
if !version.pre.is_empty() {
124125
text.push('-');
@@ -133,7 +134,8 @@ fn version_to_string(version: &Version) -> String {
133134
}
134135
}
135136
if let Some(build) = version.build.as_ref() {
136-
let _ = write!(text, "+{build}");
137+
text.push('+');
138+
text.push_str(build);
137139
}
138140
text
139141
}

compiler-core/src/derivation_tree.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use petgraph::prelude::StableGraph;
1313
use pubgrub::External;
1414
use pubgrub::{DerivationTree, Derived, Ranges};
1515
use std::collections::HashMap;
16-
use std::fmt::Write as _;
1716
use std::hash::RandomState;
1817
use std::ops::Bound::{Excluded, Included, Unbounded};
1918
use std::sync::Arc;
@@ -172,11 +171,13 @@ impl DerivationTreePrinter {
172171
.ranges_between(previous, next)
173172
.expect("path edge is in the graph");
174173

175-
let _ = write!(
176-
message,
177-
"\n - {previous_name} requires {next_name} {}",
178-
pretty_range(next_range)
179-
);
174+
message.push_str("\n - ");
175+
message.push_str(previous_name);
176+
message.push_str(" requires ");
177+
message.push_str(next_name);
178+
message.push(' ');
179+
message.push_str(&pretty_range(next_range));
180+
180181
previous = next;
181182
}
182183
message

compiler-core/src/docs/tests.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
use std::{
55
collections::{HashMap, HashSet},
6-
fmt::Write as _,
76
time::SystemTime,
87
};
98

@@ -201,7 +200,11 @@ fn compile_documentation(
201200

202201
output.push_str("---- SOURCE CODE\n");
203202
for (_package, name, src) in modules {
204-
let _ = write!(output, "-- {name}.gleam\n{src}\n\n");
203+
output.push_str("-- ");
204+
output.push_str(name);
205+
output.push_str(".gleam\n");
206+
output.push_str(src);
207+
output.push_str("\n\n");
205208
}
206209
output.push_str("-- ");
207210
output.push_str(module_name);

compiler-core/src/error.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use crate::bit_array::UnsupportedOption;
77
use crate::build::{Origin, Outcome, Runtime, Target};
88
use crate::dependency::{PackageFetcher, ResolutionError};
99
use crate::diagnostic::{Diagnostic, ExtraLabel, Label, Location};
10-
use std::fmt::Write as _;
1110

1211
use crate::derivation_tree::DerivationTreePrinter;
1312
use crate::parse::error::ParseErrorDetails;
@@ -3536,7 +3535,11 @@ but no type in scope with that name."
35363535
};
35373536
text.push_str(message);
35383537
for module_name in possible_modules {
3539-
let _ = writeln!(text, " - {module_name}.{name}");
3538+
text.push_str(" - ");
3539+
text.push_str(module_name);
3540+
text.push('.');
3541+
text.push_str(name);
3542+
text.push('\n');
35403543
}
35413544
}
35423545

@@ -4649,11 +4652,9 @@ here takes {expected_string}.\n"
46494652
the `use` callback function.\n",
46504653
);
46514654
} else {
4652-
let _ = writeln!(
4653-
text,
4654-
"You supplied {supplied_arguments_string} \
4655-
and the final one is the `use` callback function."
4656-
);
4655+
text.push_str("You supplied ");
4656+
text.push_str(&supplied_arguments_string);
4657+
text.push_str(" and the final one is the `use` callback function.\n");
46574658
}
46584659
} else {
46594660
text.push_str(

compiler-core/src/javascript/tests.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,21 @@ pub static CURRENT_PACKAGE: &str = "thepackage";
4747
#[macro_export]
4848
macro_rules! assert_js {
4949
($(($name:literal, $module_src:literal)),+, $src:literal $(,)?) => {
50-
use std::fmt::Write as _;
5150
let compiled =
5251
$crate::javascript::tests::compile_js($src, vec![$(($crate::javascript::tests::CURRENT_PACKAGE, $name, $module_src)),*]);
5352
let mut output = String::from("----- SOURCE CODE\n");
5453
for (name, src) in [$(($name, $module_src)),*] {
55-
let _ = write!(output, "-- {name}.gleam\n{src}\n\n");
54+
output.push_str("-- ");
55+
output.push_str(name);
56+
output.push_str(".gleam\n");
57+
output.push_str(src);
58+
output.push_str("\n\n");
5659
}
57-
let _ = write!(output, "-- main.gleam\n{}\n\n----- COMPILED JAVASCRIPT\n{compiled}", $src);
60+
output.push_str("-- main.gleam\n");
61+
output.push_str($src);
62+
output.push_str("\n\n----- COMPILED JAVASCRIPT\n");
63+
output.push_str(&compiled);
64+
5865
insta::assert_snapshot!(insta::internals::AutoName, output, $src);
5966
};
6067

compiler-core/src/type_/tests.rs

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,6 @@ macro_rules! assert_module_error {
9090
};
9191

9292
($(($name:expr, $module_src:literal)),+, $src:literal $(,)?) => {
93-
use std::fmt::Write as _;
94-
9593
let error = $crate::type_::tests::module_error(
9694
$src,
9795
vec![
@@ -101,15 +99,20 @@ macro_rules! assert_module_error {
10199

102100
let mut output = String::from("----- SOURCE CODE\n");
103101
for (name, src) in [$(($name, $module_src)),*] {
104-
let _ = write!(output, "-- {name}.gleam\n{src}\n\n");
102+
output.push_str("-- ");
103+
output.push_str(name);
104+
output.push_str(".gleam\n");
105+
output.push_str(src);
106+
output.push_str("\n\n");
105107
}
106-
let _ = write!(output, "-- main.gleam\n{}\n\n----- ERROR\n{error}", $src);
108+
output.push_str("-- main.gleam\n");
109+
output.push_str($src);
110+
output.push_str("\n\n----- ERROR\n");
111+
output.push_str(&error);
107112
insta::assert_snapshot!(insta::internals::AutoName, output, $src);
108113
};
109114

110115
($(($package:literal, $name:expr, $module_src:literal)),+, $src:literal $(,)?) => {
111-
use std::fmt::Write as _;
112-
113116
let error = $crate::type_::tests::module_error(
114117
$src,
115118
vec![
@@ -119,9 +122,16 @@ macro_rules! assert_module_error {
119122

120123
let mut output = String::from("----- SOURCE CODE\n");
121124
for (name, src) in [$(($name, $module_src)),*] {
122-
let _ = write!(output, "-- {name}.gleam\n{src}\n\n");
125+
output.push_str("-- ");
126+
output.push_str(name);
127+
output.push_str(".gleam\n");
128+
output.push_str(src);
129+
output.push_str("\n\n");
123130
}
124-
let _ = write!(output, "-- main.gleam\n{}\n\n----- ERROR\n{error}", $src);
131+
output.push_str("-- main.gleam\n");
132+
output.push_str($src);
133+
output.push_str("\n\n----- ERROR\n");
134+
output.push_str(&error);
125135
insta::assert_snapshot!(insta::internals::AutoName, output, $src);
126136
};
127137
}
@@ -235,8 +245,6 @@ macro_rules! assert_warning {
235245
};
236246

237247
($(($name:expr, $module_src:literal)),+, $src:literal $(,)?) => {
238-
use std::fmt::Write as _;
239-
240248
let warning = $crate::type_::tests::get_printed_warnings(
241249
$src,
242250
vec![
@@ -249,15 +257,21 @@ macro_rules! assert_warning {
249257

250258
let mut output = String::from("----- SOURCE CODE\n");
251259
for (name, src) in [$(($name, $module_src)),*] {
252-
let _ = write!(output, "-- {name}.gleam\n{src}\n\n");
260+
output.push_str("-- ");
261+
output.push_str(name);
262+
output.push_str(".gleam\n");
263+
output.push_str(src);
264+
output.push_str("\n\n");
253265
}
254-
let _ = write!(output, "-- main.gleam\n{}\n\n----- WARNING\n{warning}", $src);
266+
267+
output.push_str("-- main.gleam\n");
268+
output.push_str($src);
269+
output.push_str("\n\n----- WARNING\n");
270+
output.push_str(&warning);
255271
insta::assert_snapshot!(insta::internals::AutoName, output, $src);
256272
};
257273

258274
($(($package:expr, $name:expr, $module_src:literal)),+, $src:expr) => {
259-
use std::fmt::Write as _;
260-
261275
let warning = $crate::type_::tests::get_printed_warnings(
262276
$src,
263277
vec![$(($package, $name, $module_src)),*],
@@ -268,9 +282,17 @@ macro_rules! assert_warning {
268282

269283
let mut output = String::from("----- SOURCE CODE\n");
270284
for (name, src) in [$(($name, $module_src)),*] {
271-
let _ = write!(output, "-- {name}.gleam\n{src}\n\n");
285+
output.push_str("-- ");
286+
output.push_str(name);
287+
output.push_str(".gleam\n");
288+
output.push_str(src);
289+
output.push_str("\n\n");
272290
}
273-
let _ = write!(output, "-- main.gleam\n{}\n\n----- WARNING\n{warning}", $src);
291+
292+
output.push_str("-- main.gleam\n");
293+
output.push_str($src);
294+
output.push_str("\n\n----- WARNING\n");
295+
output.push_str(&warning);
274296
insta::assert_snapshot!(insta::internals::AutoName, output, $src);
275297
};
276298
}

compiler-core/src/warning.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ use debug_ignore::DebugIgnore;
2222
use ecow::EcoString;
2323
use itertools::Itertools;
2424
use std::{
25-
fmt::Write as _,
2625
io::Write,
2726
sync::{Arc, atomic::Ordering},
2827
};
@@ -1102,10 +1101,11 @@ Either change the pattern or use `panic` to unconditionally fail.",
11021101
let mut text = format!("`{name}` is not a function");
11031102
match arguments {
11041103
0 => {
1105-
let _ = write!(
1106-
text,
1107-
", you can just write `{name}` instead of `{name}()`."
1108-
);
1104+
text.push_str(", you can just write `");
1105+
text.push_str(name);
1106+
text.push_str("` instead of `");
1107+
text.push_str(name);
1108+
text.push_str("()`.");
11091109
}
11101110
1 => text.push_str(
11111111
" and will crash before it can do anything with this argument.",

language-server/src/tests/action.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use lsp_types::{
66
CodeActionContext, CodeActionParams, DocumentChange, PartialResultParams, Position, Uri as Url,
77
WorkDoneProgressParams,
88
};
9-
use std::fmt::Write as _;
109

1110
use super::*;
1211
use crate::path;
@@ -249,7 +248,8 @@ macro_rules! assert_code_action {
249248
);
250249

251250
if !file_operations.is_empty() {
252-
let _ = write!(output, "\n----- FILE OPERATIONS -----\n{file_operations}");
251+
output.push_str("\n----- FILE OPERATIONS -----\n");
252+
output.push_str(&file_operations);
253253
}
254254

255255
insta::assert_snapshot!(insta::internals::AutoName, output, src);

language-server/src/tests/document_highlight.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,6 @@ macro_rules! assert_highlights {
8686
};
8787

8888
($project:expr, $position:expr $(,)?) => {
89-
use std::fmt::Write as _;
90-
9189
let project = $project;
9290
let src = project.src;
9391
let position = $position.find_position(src);
@@ -101,22 +99,24 @@ macro_rules! assert_highlights {
10199
} else {
102100
&Vec::new()
103101
};
104-
let _ = write!(
105-
output,
106-
"-- {name}.gleam\n{}\n\n",
107-
show_highlights(src, None, highlights_in_module)
108-
);
102+
103+
output.push_str("-- ");
104+
output.push_str(name);
105+
output.push_str(".gleam\n");
106+
output.push_str(&show_highlights(src, None, highlights_in_module));
107+
output.push_str("\n\n");
109108
}
110109
let highlights_in_app_module = if module_name == "app" {
111110
&result
112111
} else {
113112
&Vec::new()
114113
};
115-
let _ = write!(
116-
output,
117-
"-- app.gleam\n{}",
118-
show_highlights(src, Some(position), highlights_in_app_module)
119-
);
114+
output.push_str("-- app.gleam\n");
115+
output.push_str(&show_highlights(
116+
src,
117+
Some(position),
118+
highlights_in_app_module,
119+
));
120120

121121
insta::assert_snapshot!(insta::internals::AutoName, output, src);
122122
};

language-server/src/tests/reference.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -94,30 +94,29 @@ macro_rules! assert_references {
9494
};
9595

9696
($project:expr, $position:expr $(,)?) => {
97-
use std::fmt::Write as _;
98-
9997
let project = $project;
10098
let src = project.src;
10199
let position = $position.find_position(src);
102100
let result = find_references(&project, position).expect("References not found");
103101

104102
let mut output = String::new();
105103
for (name, src) in project.root_package_modules.iter() {
106-
let _ = write!(
107-
output,
108-
"-- {name}.gleam\n{}\n\n",
109-
show_references(src, None, result.get(*name).unwrap_or(&Vec::new()))
110-
);
111-
}
112-
let _ = write!(
113-
output,
114-
"-- app.gleam\n{}",
115-
show_references(
104+
output.push_str("-- ");
105+
output.push_str(name);
106+
output.push_str(".gleam\n");
107+
output.push_str(&show_references(
116108
src,
117-
Some(position),
118-
result.get("app").unwrap_or(&Vec::new())
119-
)
120-
);
109+
None,
110+
result.get(*name).unwrap_or(&Vec::new()),
111+
));
112+
output.push_str("\n\n");
113+
}
114+
output.push_str("-- app.gleam\n");
115+
output.push_str(&show_references(
116+
src,
117+
Some(position),
118+
result.get("app").unwrap_or(&Vec::new()),
119+
));
121120

122121
insta::assert_snapshot!(insta::internals::AutoName, output, src);
123122
};

0 commit comments

Comments
 (0)