Skip to content

Commit

Permalink
Auto merge of #11505 - Alexendoo:metadata-collector-truncate, r=Manis…
Browse files Browse the repository at this point in the history
…hearth

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
  • Loading branch information
bors committed Sep 14, 2023
2 parents b27fc10 + 3c0fc15 commit 6734e96
Showing 1 changed file with 10 additions and 27 deletions.
37 changes: 10 additions & 27 deletions clippy_lints/src/utils/internal_lints/metadata_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use serde::{Serialize, Serializer};
use std::collections::{BTreeSet, BinaryHeap};
use std::fmt;
use std::fmt::Write as _;
use std::fs::{self, OpenOptions};
use std::fs::{self, File};
use std::io::prelude::*;
use std::path::{Path, PathBuf};
use std::process::Command;
Expand Down Expand Up @@ -229,25 +229,10 @@ impl Drop for MetadataCollector {
collect_renames(&mut lints);

// Outputting json
if Path::new(JSON_OUTPUT_FILE).exists() {
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();
fs::write(JSON_OUTPUT_FILE, serde_json::to_string_pretty(&lints).unwrap()).unwrap();

// Outputting markdown
if Path::new(MARKDOWN_OUTPUT_FILE).exists() {
fs::remove_file(MARKDOWN_OUTPUT_FILE).unwrap();
}
let mut file = OpenOptions::new()
.write(true)
.create(true)
.open(MARKDOWN_OUTPUT_FILE)
.unwrap();
let mut file = File::create(MARKDOWN_OUTPUT_FILE).unwrap();
writeln!(
file,
"<!--
Expand All @@ -261,17 +246,15 @@ Please use that command to update the file and do not edit it by hand.
.unwrap();

// Write configuration links to CHANGELOG.md
let mut changelog = std::fs::read_to_string(CHANGELOG_PATH).unwrap();
let mut changelog_file = OpenOptions::new().read(true).write(true).open(CHANGELOG_PATH).unwrap();

if let Some(position) = changelog.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
// we can do AFAIK.
changelog = changelog[..position].to_string();
}
let changelog = std::fs::read_to_string(CHANGELOG_PATH).unwrap();
let mut changelog_file = File::create(CHANGELOG_PATH).unwrap();
let position = changelog
.find("<!-- begin autogenerated links to configuration documentation -->")
.unwrap();
writeln!(
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)
)
.unwrap();
Expand Down

0 comments on commit 6734e96

Please sign in to comment.