Skip to content

Commit 6734e96

Browse files
committed
Auto merge of #11505 - Alexendoo:metadata-collector-truncate, r=Manishearth
Truncate files when opening in metadata-collector Fixes the issue seen here #11483 (comment) and in a couple other PRs The changelog file was opened without truncating it, so if the new version is shorter than the old one stray contents would remain at the end of the file The other two files first removed the file so didn't have this problem, but in all cases we now use `fs::write`/`File::create` which is write + create + truncate changelog: none
2 parents b27fc10 + 3c0fc15 commit 6734e96

File tree

1 file changed

+10
-27
lines changed

1 file changed

+10
-27
lines changed

clippy_lints/src/utils/internal_lints/metadata_collector.rs

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use serde::{Serialize, Serializer};
3131
use std::collections::{BTreeSet, BinaryHeap};
3232
use std::fmt;
3333
use std::fmt::Write as _;
34-
use std::fs::{self, OpenOptions};
34+
use std::fs::{self, File};
3535
use std::io::prelude::*;
3636
use std::path::{Path, PathBuf};
3737
use std::process::Command;
@@ -229,25 +229,10 @@ impl Drop for MetadataCollector {
229229
collect_renames(&mut lints);
230230

231231
// Outputting json
232-
if Path::new(JSON_OUTPUT_FILE).exists() {
233-
fs::remove_file(JSON_OUTPUT_FILE).unwrap();
234-
}
235-
let mut file = OpenOptions::new()
236-
.write(true)
237-
.create(true)
238-
.open(JSON_OUTPUT_FILE)
239-
.unwrap();
240-
writeln!(file, "{}", serde_json::to_string_pretty(&lints).unwrap()).unwrap();
232+
fs::write(JSON_OUTPUT_FILE, serde_json::to_string_pretty(&lints).unwrap()).unwrap();
241233

242234
// Outputting markdown
243-
if Path::new(MARKDOWN_OUTPUT_FILE).exists() {
244-
fs::remove_file(MARKDOWN_OUTPUT_FILE).unwrap();
245-
}
246-
let mut file = OpenOptions::new()
247-
.write(true)
248-
.create(true)
249-
.open(MARKDOWN_OUTPUT_FILE)
250-
.unwrap();
235+
let mut file = File::create(MARKDOWN_OUTPUT_FILE).unwrap();
251236
writeln!(
252237
file,
253238
"<!--
@@ -261,17 +246,15 @@ Please use that command to update the file and do not edit it by hand.
261246
.unwrap();
262247

263248
// Write configuration links to CHANGELOG.md
264-
let mut changelog = std::fs::read_to_string(CHANGELOG_PATH).unwrap();
265-
let mut changelog_file = OpenOptions::new().read(true).write(true).open(CHANGELOG_PATH).unwrap();
266-
267-
if let Some(position) = changelog.find("<!-- begin autogenerated links to configuration documentation -->") {
268-
// I know this is kinda wasteful, we just don't have regex on `clippy_lints` so... this is the best
269-
// we can do AFAIK.
270-
changelog = changelog[..position].to_string();
271-
}
249+
let changelog = std::fs::read_to_string(CHANGELOG_PATH).unwrap();
250+
let mut changelog_file = File::create(CHANGELOG_PATH).unwrap();
251+
let position = changelog
252+
.find("<!-- begin autogenerated links to configuration documentation -->")
253+
.unwrap();
272254
writeln!(
273255
changelog_file,
274-
"{changelog}<!-- begin autogenerated links to configuration documentation -->\n{}\n<!-- end autogenerated links to configuration documentation -->",
256+
"{}<!-- begin autogenerated links to configuration documentation -->\n{}\n<!-- end autogenerated links to configuration documentation -->",
257+
&changelog[..position],
275258
self.configs_to_markdown(ClippyConfiguration::to_markdown_link)
276259
)
277260
.unwrap();

0 commit comments

Comments
 (0)