Skip to content

Commit a373e73

Browse files
authored
Merge pull request rust-lang#3469 from topecongiro/cargo-fmt
Fix cargo fmt inconsistency
2 parents f910afd + b4d4b57 commit a373e73

File tree

1 file changed

+36
-26
lines changed

1 file changed

+36
-26
lines changed

src/cargo-fmt/main.rs

+36-26
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
use cargo_metadata;
77
use getopts;
88

9-
use std::collections::{HashMap, HashSet};
9+
use std::cmp::Ordering;
10+
use std::collections::{BTreeMap, BTreeSet};
1011
use std::env;
1112
use std::fs;
1213
use std::hash::{Hash, Hasher};
@@ -122,7 +123,7 @@ fn handle_command_status(status: Result<i32, io::Error>, opts: &getopts::Options
122123
}
123124

124125
fn get_version(verbosity: Verbosity) -> Result<i32, io::Error> {
125-
run_rustfmt(&HashSet::new(), &[String::from("--version")], verbosity)
126+
run_rustfmt(&BTreeSet::new(), &[String::from("--version")], verbosity)
126127
}
127128

128129
fn format_crate(verbosity: Verbosity, strategy: &CargoFmtStrategy) -> Result<i32, io::Error> {
@@ -131,7 +132,7 @@ fn format_crate(verbosity: Verbosity, strategy: &CargoFmtStrategy) -> Result<i32
131132
.iter()
132133
.any(|s| ["--print-config", "-h", "--help", "-V", "--version"].contains(&s.as_str()))
133134
{
134-
HashSet::new()
135+
BTreeSet::new()
135136
} else {
136137
get_targets(strategy)?
137138
};
@@ -175,6 +176,18 @@ impl PartialEq for Target {
175176
}
176177
}
177178

179+
impl PartialOrd for Target {
180+
fn partial_cmp(&self, other: &Target) -> Option<Ordering> {
181+
Some(self.path.cmp(&other.path))
182+
}
183+
}
184+
185+
impl Ord for Target {
186+
fn cmp(&self, other: &Target) -> Ordering {
187+
self.path.cmp(&other.path)
188+
}
189+
}
190+
178191
impl Eq for Target {}
179192

180193
impl Hash for Target {
@@ -204,12 +217,12 @@ impl CargoFmtStrategy {
204217
}
205218

206219
/// Based on the specified `CargoFmtStrategy`, returns a set of main source files.
207-
fn get_targets(strategy: &CargoFmtStrategy) -> Result<HashSet<Target>, io::Error> {
208-
let mut targets = HashSet::new();
220+
fn get_targets(strategy: &CargoFmtStrategy) -> Result<BTreeSet<Target>, io::Error> {
221+
let mut targets = BTreeSet::new();
209222

210223
match *strategy {
211224
CargoFmtStrategy::Root => get_targets_root_only(&mut targets)?,
212-
CargoFmtStrategy::All => get_targets_recursive(None, &mut targets, &mut HashSet::new())?,
225+
CargoFmtStrategy::All => get_targets_recursive(None, &mut targets, &mut BTreeSet::new())?,
213226
CargoFmtStrategy::Some(ref hitlist) => get_targets_with_hitlist(hitlist, &mut targets)?,
214227
}
215228

@@ -223,7 +236,7 @@ fn get_targets(strategy: &CargoFmtStrategy) -> Result<HashSet<Target>, io::Error
223236
}
224237
}
225238

226-
fn get_targets_root_only(targets: &mut HashSet<Target>) -> Result<(), io::Error> {
239+
fn get_targets_root_only(targets: &mut BTreeSet<Target>) -> Result<(), io::Error> {
227240
let metadata = get_cargo_metadata(None)?;
228241
let current_dir = env::current_dir()?.canonicalize()?;
229242
let current_dir_manifest = current_dir.join("Cargo.toml");
@@ -243,8 +256,8 @@ fn get_targets_root_only(targets: &mut HashSet<Target>) -> Result<(), io::Error>
243256

244257
fn get_targets_recursive(
245258
manifest_path: Option<&Path>,
246-
mut targets: &mut HashSet<Target>,
247-
visited: &mut HashSet<String>,
259+
mut targets: &mut BTreeSet<Target>,
260+
visited: &mut BTreeSet<String>,
248261
) -> Result<(), io::Error> {
249262
let metadata = get_cargo_metadata(manifest_path)?;
250263

@@ -275,11 +288,11 @@ fn get_targets_recursive(
275288

276289
fn get_targets_with_hitlist(
277290
hitlist: &[String],
278-
targets: &mut HashSet<Target>,
291+
targets: &mut BTreeSet<Target>,
279292
) -> Result<(), io::Error> {
280293
let metadata = get_cargo_metadata(None)?;
281294

282-
let mut workspace_hitlist: HashSet<&String> = HashSet::from_iter(hitlist);
295+
let mut workspace_hitlist: BTreeSet<&String> = BTreeSet::from_iter(hitlist);
283296

284297
for package in metadata.packages {
285298
if workspace_hitlist.remove(&package.name) {
@@ -300,34 +313,30 @@ fn get_targets_with_hitlist(
300313
}
301314
}
302315

303-
fn add_targets(target_paths: &[cargo_metadata::Target], targets: &mut HashSet<Target>) {
316+
fn add_targets(target_paths: &[cargo_metadata::Target], targets: &mut BTreeSet<Target>) {
304317
for target in target_paths {
305318
targets.insert(Target::from_target(target));
306319
}
307320
}
308321

309322
fn run_rustfmt(
310-
targets: &HashSet<Target>,
323+
targets: &BTreeSet<Target>,
311324
fmt_args: &[String],
312325
verbosity: Verbosity,
313326
) -> Result<i32, io::Error> {
314-
let default_edition = String::from("2015");
315-
let mut by_edition = targets
327+
let by_edition = targets
316328
.iter()
317329
.inspect(|t| {
318330
if verbosity == Verbosity::Verbose {
319331
println!("[{} ({})] {:?}", t.kind, t.edition, t.path)
320332
}
321333
})
322-
.map(|t| (&t.edition, &t.path))
323-
.fold(HashMap::new(), |mut h, t| {
324-
h.entry(t.0).or_insert_with(Vec::new).push(t.1);
334+
.fold(BTreeMap::new(), |mut h, t| {
335+
h.entry(&t.edition).or_insert_with(Vec::new).push(&t.path);
325336
h
326337
});
327-
if by_edition.is_empty() {
328-
by_edition.insert(&default_edition, Vec::new());
329-
}
330338

339+
let mut status = vec![];
331340
for (edition, files) in by_edition {
332341
let stdout = if verbosity == Verbosity::Quiet {
333342
std::process::Stdio::null()
@@ -357,13 +366,14 @@ fn run_rustfmt(
357366
_ => e,
358367
})?;
359368

360-
let status = command.wait()?;
361-
if !status.success() {
362-
return Ok(status.code().unwrap_or(FAILURE));
363-
}
369+
status.push(command.wait()?);
364370
}
365371

366-
Ok(SUCCESS)
372+
Ok(status
373+
.iter()
374+
.filter_map(|s| if s.success() { None } else { s.code() })
375+
.next()
376+
.unwrap_or(SUCCESS))
367377
}
368378

369379
fn get_cargo_metadata(manifest_path: Option<&Path>) -> Result<cargo_metadata::Metadata, io::Error> {

0 commit comments

Comments
 (0)