Auto merge of #11505 - Alexendoo:metadata-collector-truncate, r=Manishearth

Truncate files when opening in metadata-collector

Fixes the issue seen here https://github.com/rust-lang/rust-clippy/pull/11483#discussion_r1324687136 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
This commit is contained in:
bors 2023-09-14 23:05:24 +00:00
commit 6734e96ba4

View File

@ -31,7 +31,7 @@ use serde::{Serialize, Serializer};
use std::collections::{BTreeSet, BinaryHeap}; use std::collections::{BTreeSet, BinaryHeap};
use std::fmt; use std::fmt;
use std::fmt::Write as _; use std::fmt::Write as _;
use std::fs::{self, OpenOptions}; use std::fs::{self, File};
use std::io::prelude::*; use std::io::prelude::*;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::process::Command; use std::process::Command;
@ -229,25 +229,10 @@ impl Drop for MetadataCollector {
collect_renames(&mut lints); collect_renames(&mut lints);
// Outputting json // Outputting json
if Path::new(JSON_OUTPUT_FILE).exists() { fs::write(JSON_OUTPUT_FILE, serde_json::to_string_pretty(&lints).unwrap()).unwrap();
fs::remove_file(JSON_OUTPUT_FILE).unwrap();
}
let mut file = OpenOptions::new()
.write(true)
.create(true)
.open(JSON_OUTPUT_FILE)
.unwrap();
writeln!(file, "{}", serde_json::to_string_pretty(&lints).unwrap()).unwrap();
// Outputting markdown // Outputting markdown
if Path::new(MARKDOWN_OUTPUT_FILE).exists() { let mut file = File::create(MARKDOWN_OUTPUT_FILE).unwrap();
fs::remove_file(MARKDOWN_OUTPUT_FILE).unwrap();
}
let mut file = OpenOptions::new()
.write(true)
.create(true)
.open(MARKDOWN_OUTPUT_FILE)
.unwrap();
writeln!( writeln!(
file, file,
"<!-- "<!--
@ -261,17 +246,15 @@ Please use that command to update the file and do not edit it by hand.
.unwrap(); .unwrap();
// Write configuration links to CHANGELOG.md // Write configuration links to CHANGELOG.md
let mut changelog = std::fs::read_to_string(CHANGELOG_PATH).unwrap(); let changelog = std::fs::read_to_string(CHANGELOG_PATH).unwrap();
let mut changelog_file = OpenOptions::new().read(true).write(true).open(CHANGELOG_PATH).unwrap(); let mut changelog_file = File::create(CHANGELOG_PATH).unwrap();
let position = changelog
if let Some(position) = changelog.find("<!-- begin autogenerated links to configuration documentation -->") { .find("<!-- begin autogenerated links to configuration documentation -->")
// I know this is kinda wasteful, we just don't have regex on `clippy_lints` so... this is the best .unwrap();
// we can do AFAIK.
changelog = changelog[..position].to_string();
}
writeln!( writeln!(
changelog_file, changelog_file,
"{changelog}<!-- begin autogenerated links to configuration documentation -->\n{}\n<!-- end autogenerated links to configuration documentation -->", "{}<!-- begin autogenerated links to configuration documentation -->\n{}\n<!-- end autogenerated links to configuration documentation -->",
&changelog[..position],
self.configs_to_markdown(ClippyConfiguration::to_markdown_link) self.configs_to_markdown(ClippyConfiguration::to_markdown_link)
) )
.unwrap(); .unwrap();