From 44e794614e781d723001efdeba2c9851b5732d42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Kr=C3=BCger?= Date: Sat, 1 Aug 2020 13:07:35 +0200 Subject: [PATCH 1/2] use matches! macro in more places --- src/cargo/core/compiler/build_config.rs | 35 +++++++++---------------- src/cargo/core/compiler/compile_kind.rs | 5 +--- src/cargo/core/compiler/crate_type.rs | 15 +++++------ src/cargo/core/dependency.rs | 5 +--- src/cargo/core/manifest.rs | 23 +++++----------- src/cargo/core/profiles.rs | 5 +--- src/cargo/core/source/source_id.rs | 18 +++++-------- 7 files changed, 35 insertions(+), 71 deletions(-) diff --git a/src/cargo/core/compiler/build_config.rs b/src/cargo/core/compiler/build_config.rs index e34b58e6c22..d0f5431a47e 100644 --- a/src/cargo/core/compiler/build_config.rs +++ b/src/cargo/core/compiler/build_config.rs @@ -86,10 +86,7 @@ impl BuildConfig { /// Whether or not the *user* wants JSON output. Whether or not rustc /// actually uses JSON is decided in `add_error_format`. pub fn emit_json(&self) -> bool { - match self.message_format { - MessageFormat::Json { .. } => true, - _ => false, - } + matches!(self.message_format, MessageFormat::Json { .. }) } pub fn test(&self) -> bool { @@ -171,18 +168,12 @@ impl ser::Serialize for CompileMode { impl CompileMode { /// Returns `true` if the unit is being checked. pub fn is_check(self) -> bool { - match self { - CompileMode::Check { .. } => true, - _ => false, - } + matches!(self, CompileMode::Check { .. }) } /// Returns `true` if this is generating documentation. pub fn is_doc(self) -> bool { - match self { - CompileMode::Doc { .. } => true, - _ => false, - } + matches!(self, CompileMode::Doc { .. }) } /// Returns `true` if this a doc test. @@ -193,21 +184,21 @@ impl CompileMode { /// Returns `true` if this is any type of test (test, benchmark, doc test, or /// check test). pub fn is_any_test(self) -> bool { - match self { + matches!( + self, CompileMode::Test - | CompileMode::Bench - | CompileMode::Check { test: true } - | CompileMode::Doctest => true, - _ => false, - } + | CompileMode::Bench + | CompileMode::Check { test: true } + | CompileMode::Doctest + ) } /// Returns `true` if this is something that passes `--test` to rustc. pub fn is_rustc_test(self) -> bool { - match self { - CompileMode::Test | CompileMode::Bench | CompileMode::Check { test: true } => true, - _ => false, - } + matches!( + self, + CompileMode::Test | CompileMode::Bench | CompileMode::Check { test: true } + ) } /// Returns `true` if this is the *execution* of a `build.rs` script. diff --git a/src/cargo/core/compiler/compile_kind.rs b/src/cargo/core/compiler/compile_kind.rs index a621d8ce7f7..4c123601f8e 100644 --- a/src/cargo/core/compiler/compile_kind.rs +++ b/src/cargo/core/compiler/compile_kind.rs @@ -27,10 +27,7 @@ pub enum CompileKind { impl CompileKind { pub fn is_host(&self) -> bool { - match self { - CompileKind::Host => true, - _ => false, - } + matches!(self, CompileKind::Host) } pub fn for_target(self, target: &Target) -> CompileKind { diff --git a/src/cargo/core/compiler/crate_type.rs b/src/cargo/core/compiler/crate_type.rs index 28a2d9340a1..763f99a65f0 100644 --- a/src/cargo/core/compiler/crate_type.rs +++ b/src/cargo/core/compiler/crate_type.rs @@ -58,16 +58,13 @@ impl CrateType { } pub fn requires_upstream_objects(&self) -> bool { - match self { - // "lib" == "rlib" and is a compilation that doesn't actually - // require upstream object files to exist, only upstream metadata - // files. As a result, it doesn't require upstream artifacts - CrateType::Lib | CrateType::Rlib => false, + // "lib" == "rlib" and is a compilation that doesn't actually + // require upstream object files to exist, only upstream metadata + // files. As a result, it doesn't require upstream artifacts - // Everything else, however, is some form of "linkable output" or - // something that requires upstream object files. - _ => true, - } + !matches!(self, CrateType::Lib | CrateType::Rlib) + // Everything else, however, is some form of "linkable output" or + // something that requires upstream object files. } } diff --git a/src/cargo/core/dependency.rs b/src/cargo/core/dependency.rs index e4358a24178..61795936dc2 100644 --- a/src/cargo/core/dependency.rs +++ b/src/cargo/core/dependency.rs @@ -413,10 +413,7 @@ impl Dependency { } pub fn is_build(&self) -> bool { - match self.inner.kind { - DepKind::Build => true, - _ => false, - } + matches!(self.inner.kind, DepKind::Build) } pub fn is_optional(&self) -> bool { diff --git a/src/cargo/core/manifest.rs b/src/cargo/core/manifest.rs index d42f0fd179c..dc69fbc08e3 100644 --- a/src/cargo/core/manifest.rs +++ b/src/cargo/core/manifest.rs @@ -220,10 +220,7 @@ impl TargetSourcePath { } pub fn is_path(&self) -> bool { - match self { - TargetSourcePath::Path(_) => true, - _ => false, - } + matches!(self, TargetSourcePath::Path(_)) } } @@ -777,10 +774,7 @@ impl Target { } pub fn is_lib(&self) -> bool { - match self.kind() { - TargetKind::Lib(_) => true, - _ => false, - } + matches!(self.kind(), TargetKind::Lib(_)) } pub fn is_dylib(&self) -> bool { @@ -813,10 +807,10 @@ impl Target { } pub fn is_example(&self) -> bool { - match self.kind() { - TargetKind::ExampleBin | TargetKind::ExampleLib(..) => true, - _ => false, - } + matches!( + self.kind(), + TargetKind::ExampleBin | TargetKind::ExampleLib(..) + ) } /// Returns `true` if it is a binary or executable example. @@ -828,10 +822,7 @@ impl Target { /// Returns `true` if it is an executable example. pub fn is_exe_example(&self) -> bool { // Needed for --all-examples in contexts where only runnable examples make sense - match self.kind() { - TargetKind::ExampleBin => true, - _ => false, - } + matches!(self.kind(), TargetKind::ExampleBin) } pub fn is_test(&self) -> bool { diff --git a/src/cargo/core/profiles.rs b/src/cargo/core/profiles.rs index 558cca53074..6bae0337f7c 100644 --- a/src/cargo/core/profiles.rs +++ b/src/cargo/core/profiles.rs @@ -296,10 +296,7 @@ impl Profiles { // `--release` and `--debug` predicates, and convert back from // ProfileKind::Custom instantiation. - let release = match self.requested_profile.as_str() { - "release" | "bench" => true, - _ => false, - }; + let release = matches!(self.requested_profile.as_str(), "release" | "bench"); match mode { CompileMode::Test | CompileMode::Bench => { diff --git a/src/cargo/core/source/source_id.rs b/src/cargo/core/source/source_id.rs index c20a9064b3a..e896e7e5107 100644 --- a/src/cargo/core/source/source_id.rs +++ b/src/cargo/core/source/source_id.rs @@ -239,10 +239,10 @@ impl SourceId { /// Returns `true` if this source is from a registry (either local or not). pub fn is_registry(self) -> bool { - match self.inner.kind { - SourceKind::Registry | SourceKind::LocalRegistry => true, - _ => false, - } + matches!( + self.inner.kind, + SourceKind::Registry | SourceKind::LocalRegistry + ) } /// Returns `true` if this source is a "remote" registry. @@ -250,18 +250,12 @@ impl SourceId { /// "remote" may also mean a file URL to a git index, so it is not /// necessarily "remote". This just means it is not `local-registry`. pub fn is_remote_registry(self) -> bool { - match self.inner.kind { - SourceKind::Registry => true, - _ => false, - } + matches!(self.inner.kind, SourceKind::Registry) } /// Returns `true` if this source from a Git repository. pub fn is_git(self) -> bool { - match self.inner.kind { - SourceKind::Git(_) => true, - _ => false, - } + matches!(self.inner.kind, SourceKind::Git(_)) } /// Creates an implementation of `Source` corresponding to this ID. From f23b91190ccb103d8041a14e2a715095dcae0ce7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Kr=C3=BCger?= Date: Sat, 1 Aug 2020 13:15:35 +0200 Subject: [PATCH 2/2] use sort_by_key() instead of comparing by keys manually replace format!() macro by String::from() use eprintln() instead of manually writing to std::io::stderr --- crates/cargo-test-support/src/lib.rs | 2 +- src/cargo/core/resolver/errors.rs | 2 +- src/cargo/sources/git/utils.rs | 2 +- tests/testsuite/package.rs | 2 ++ 4 files changed, 5 insertions(+), 3 deletions(-) diff --git a/crates/cargo-test-support/src/lib.rs b/crates/cargo-test-support/src/lib.rs index 1a7a9ac7851..5c64307425b 100644 --- a/crates/cargo-test-support/src/lib.rs +++ b/crates/cargo-test-support/src/lib.rs @@ -1233,7 +1233,7 @@ impl Execs { None => failures.push(e_line), } } - if failures.len() > 0 { + if !failures.is_empty() { return Err(format!( "Did not find expected line(s):\n{}\n\ Remaining available output:\n{}\n", diff --git a/src/cargo/core/resolver/errors.rs b/src/cargo/core/resolver/errors.rs index aac83654f79..86ab1bfa6c9 100644 --- a/src/cargo/core/resolver/errors.rs +++ b/src/cargo/core/resolver/errors.rs @@ -254,7 +254,7 @@ pub(super) fn activation_error( if let Err(e) = registry.query(&new_dep, &mut |s| candidates.push(s), true) { return to_resolve_err(e); }; - candidates.sort_unstable_by(|a, b| a.name().cmp(&b.name())); + candidates.sort_unstable_by_key(|a| a.name()); candidates.dedup_by(|a, b| a.name() == b.name()); let mut candidates: Vec<_> = candidates .iter() diff --git a/src/cargo/sources/git/utils.rs b/src/cargo/sources/git/utils.rs index a7e90f10444..eb572ad50ac 100644 --- a/src/cargo/sources/git/utils.rs +++ b/src/cargo/sources/git/utils.rs @@ -901,7 +901,7 @@ pub fn fetch( GitReference::DefaultBranch => { // See the module docs for why we're fetching `master` here. - refspecs.push(format!("refs/heads/master:refs/remotes/origin/master")); + refspecs.push(String::from("refs/heads/master:refs/remotes/origin/master")); refspecs.push(String::from("HEAD:refs/remotes/origin/HEAD")); } diff --git a/tests/testsuite/package.rs b/tests/testsuite/package.rs index 25090acc7ac..a2224abd858 100644 --- a/tests/testsuite/package.rs +++ b/tests/testsuite/package.rs @@ -1870,6 +1870,8 @@ fn long_file_names() { test_path.mkdir_p(); let test_path = test_path.join(long_name); if let Err(e) = File::create(&test_path) { + // write to stderr directly to avoid output from being captured + // and always display text, even without --nocapture use std::io::Write; writeln!( std::io::stderr(),