Skip to content

Commit

Permalink
Remove unnecessary usages of the vec! macro
Browse files Browse the repository at this point in the history
For cases where a known-at-compile-time length array is being
passed to a function that accepts an `Into<Iterator>`, there is
no need to wrap the array in a `vec![]` invocation.

I spotted a couple of these in one of the newly added libcnb-test
tests, and a search and replace later it turns out there were quite
a few instances of this!
  • Loading branch information
edmorley committed Sep 21, 2023
1 parent 89bbc63 commit 4a8bc04
Show file tree
Hide file tree
Showing 24 changed files with 80 additions and 76 deletions.
4 changes: 2 additions & 2 deletions examples/ruby-sample/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ impl Buildpack for RubyBuildpack {
LaunchBuilder::new()
.process(
ProcessBuilder::new(process_type!("web"), ["bundle"])
.args(vec!["exec", "ruby", "app.rb"])
.args(["exec", "ruby", "app.rb"])
.default(true)
.build(),
)
.process(
ProcessBuilder::new(process_type!("worker"), ["bundle"])
.args(vec!["exec", "ruby", "worker.rb"])
.args(["exec", "ruby", "worker.rb"])
.build(),
)
.build(),
Expand Down
2 changes: 1 addition & 1 deletion examples/ruby-sample/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ where

stream.write_all(format!("{}\n", payload.as_ref()).as_bytes())?;

let mut buffer = vec![];
let mut buffer = Vec::new();
stream.read_to_end(&mut buffer)?;

Ok(String::from_utf8_lossy(&buffer).to_string())
Expand Down
4 changes: 2 additions & 2 deletions libcnb-cargo/src/package/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub(crate) fn execute(args: &PackageArgs) -> Result<(), Error> {

eprintln!("🖥️ Gathering Cargo configuration (for {})", args.target);
let cargo_build_env = if args.no_cross_compile_assistance {
vec![]
Vec::new()
} else {
match cross_compile_assistance(&args.target) {
CrossCompileAssistance::Configuration { cargo_env } => cargo_env,
Expand All @@ -51,7 +51,7 @@ pub(crate) fn execute(args: &PackageArgs) -> Result<(), Error> {
);
eprintln!("This is not an error, but without proper cross-compile settings in your Cargo manifest and locally installed toolchains, compilation might fail.");
eprintln!("To disable this warning, pass --no-cross-compile-assistance.");
vec![]
Vec::new()
}
CrossCompileAssistance::HelpText(help_text) => {
eprintln!("{help_text}");
Expand Down
4 changes: 2 additions & 2 deletions libcnb-data/src/build_plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ impl BuildPlanBuilder {
pub fn or(mut self) -> Self {
self.acc
.push_back((self.current_provides, self.current_requires));
self.current_provides = vec![];
self.current_requires = vec![];
self.current_provides = Vec::new();
self.current_requires = Vec::new();

self
}
Expand Down
18 changes: 9 additions & 9 deletions libcnb-data/src/buildpack/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl<BM> BuildpackDescriptor<BM> {
/// let buildpack_descriptor =
/// toml::from_str::<SingleBuildpackDescriptor>(toml_str).unwrap();
/// assert_eq!(buildpack_descriptor.buildpack.id, buildpack_id!("foo/bar"));
/// assert_eq!(buildpack_descriptor.stacks, vec![Stack::Any]);
/// assert_eq!(buildpack_descriptor.stacks, [Stack::Any]);
/// ```
#[derive(Deserialize, Debug)]
#[serde(deny_unknown_fields)]
Expand Down Expand Up @@ -290,11 +290,11 @@ checksum = "abc123"
);
assert_eq!(
buildpack_descriptor.buildpack.keywords,
vec![String::from("foo"), String::from("bar")]
[String::from("foo"), String::from("bar")]
);
assert_eq!(
buildpack_descriptor.buildpack.licenses,
vec![
[
License {
r#type: Some(String::from("BSD-3-Clause")),
uri: None
Expand All @@ -319,7 +319,7 @@ checksum = "abc123"
);
assert_eq!(
buildpack_descriptor.stacks,
vec![
[
Stack::Specific {
// Cannot use the `stack_id!` macro due to: https://github.com/heroku/libcnb.rs/issues/179
id: "heroku-20".parse().unwrap(),
Expand Down Expand Up @@ -410,11 +410,11 @@ checksum = "abc123"
);
assert_eq!(
buildpack_descriptor.buildpack.keywords,
vec![String::from("foo"), String::from("bar")]
[String::from("foo"), String::from("bar")]
);
assert_eq!(
buildpack_descriptor.buildpack.licenses,
vec![
[
License {
r#type: Some(String::from("BSD-3-Clause")),
uri: None
Expand All @@ -431,7 +431,7 @@ checksum = "abc123"
);
assert_eq!(
buildpack_descriptor.order,
vec![Order {
[Order {
group: vec![
Group {
id: "foo/bar".parse().unwrap(),
Expand Down Expand Up @@ -489,7 +489,7 @@ id = "*"
);
assert_eq!(buildpack_descriptor.buildpack.licenses, Vec::new());
assert_eq!(buildpack_descriptor.buildpack.sbom_formats, HashSet::new());
assert_eq!(buildpack_descriptor.stacks, vec![Stack::Any]);
assert_eq!(buildpack_descriptor.stacks, [Stack::Any]);
assert_eq!(buildpack_descriptor.metadata, None);
}

Expand Down Expand Up @@ -534,7 +534,7 @@ version = "0.0.1"
assert_eq!(buildpack_descriptor.buildpack.licenses, Vec::new());
assert_eq!(
buildpack_descriptor.order,
vec![Order {
[Order {
group: vec![Group {
id: "foo/bar".parse().unwrap(),
version: BuildpackVersion::new(0, 0, 1),
Expand Down
16 changes: 8 additions & 8 deletions libcnb-data/src/launch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct Launch {
/// let launch_toml = LaunchBuilder::new()
/// .process(
/// ProcessBuilder::new(process_type!("web"), ["bundle"])
/// .args(vec!["exec", "ruby", "app.rb"])
/// .args(["exec", "ruby", "app.rb"])
/// .build(),
/// )
/// .build();
Expand Down Expand Up @@ -318,15 +318,15 @@ mod tests {
fn launch_builder_add_processes() {
let launch = LaunchBuilder::new()
.process(ProcessBuilder::new(process_type!("web"), ["web_command"]).build())
.processes(vec![
.processes([
ProcessBuilder::new(process_type!("another"), ["another_command"]).build(),
ProcessBuilder::new(process_type!("worker"), ["worker_command"]).build(),
])
.build();

assert_eq!(
launch.processes,
vec![
[
ProcessBuilder::new(process_type!("web"), ["web_command"]).build(),
ProcessBuilder::new(process_type!("another"), ["another_command"]).build(),
ProcessBuilder::new(process_type!("worker"), ["worker_command"]).build(),
Expand Down Expand Up @@ -372,7 +372,7 @@ command = ["foo"]
Ok(Process {
r#type: process_type!("web"),
command: vec![String::from("foo")],
args: vec![],
args: Vec::new(),
default: false,
working_directory: WorkingDirectory::App
})
Expand Down Expand Up @@ -419,7 +419,7 @@ working-directory = "dist"
Process {
r#type: process_type!("web"),
command: vec![String::from("java")],
args: vec![],
args: Vec::new(),
default: false,
working_directory: WorkingDirectory::App
}
Expand All @@ -432,7 +432,7 @@ working-directory = "dist"
Process {
r#type: process_type!("web"),
command: vec![String::from("java")],
args: vec![],
args: Vec::new(),
default: true,
working_directory: WorkingDirectory::App
}
Expand All @@ -445,7 +445,7 @@ working-directory = "dist"
Process {
r#type: process_type!("web"),
command: vec![String::from("java")],
args: vec![],
args: Vec::new(),
default: true,
working_directory: WorkingDirectory::Directory(PathBuf::from("dist"))
}
Expand All @@ -457,7 +457,7 @@ working-directory = "dist"
assert_eq!(
ProcessBuilder::new(process_type!("web"), ["java"])
.arg("foo")
.args(vec!["baz", "eggs"])
.args(["baz", "eggs"])
.arg("bar")
.build(),
Process {
Expand Down
6 changes: 3 additions & 3 deletions libcnb-data/src/newtypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,21 +242,21 @@ mod tests {

#[test]
fn join() {
let names = vec![capitalized_name!("A"), capitalized_name!("B")];
let names = [capitalized_name!("A"), capitalized_name!("B")];
assert_eq!("A, B", names.join(", "));
}

#[test]
fn ord() {
let mut names = vec![
let mut names = [
capitalized_name!("A"),
capitalized_name!("C"),
capitalized_name!("B"),
];
names.sort();

assert_eq!(
vec![
[
capitalized_name!("A"),
capitalized_name!("B"),
capitalized_name!("C")
Expand Down
4 changes: 2 additions & 2 deletions libcnb-data/src/package_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl Default for PackageDescriptor {
PackageDescriptor {
buildpack: PackageDescriptorBuildpackReference::try_from(".")
.expect("a package.toml with buildpack.uri=\".\" should be valid"),
dependencies: vec![],
dependencies: Vec::new(),
platform: Platform::default(),
}
}
Expand Down Expand Up @@ -218,7 +218,7 @@ os = "windows"
assert_eq!(package_descriptor.platform.os, Windows);
assert_eq!(
package_descriptor.dependencies,
vec![
[
PackageDescriptorDependency::try_from("libcnb:buildpack-id").unwrap(),
PackageDescriptorDependency::try_from("../relative/path").unwrap(),
PackageDescriptorDependency::try_from("/absolute/path").unwrap(),
Expand Down
2 changes: 1 addition & 1 deletion libcnb-package/src/buildpack_dependency_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ fn build_libcnb_buildpack_dependency_graph_node(
)
})
})
.unwrap_or(Ok(vec![]))
.unwrap_or(Ok(Vec::new()))
}?;

Ok(BuildpackDependencyGraphNode {
Expand Down
4 changes: 3 additions & 1 deletion libcnb-package/src/cross_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ https://github.com/messense/homebrew-macos-cross-toolchains"#,
})
} else if target_triple.as_ref() == X86_64_UNKNOWN_LINUX_MUSL && cfg!(target_os = "linux") {
match which("musl-gcc") {
Ok(_) => CrossCompileAssistance::Configuration { cargo_env: vec![] },
Ok(_) => CrossCompileAssistance::Configuration {
cargo_env: Vec::new(),
},
Err(_) => CrossCompileAssistance::HelpText(String::from(
r#"For cross-compilation from Linux to x86_64-unknown-linux-musl, a C compiler and
linker for the target platform must be installed on your computer.
Expand Down
14 changes: 7 additions & 7 deletions libcnb-package/src/dependency_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ where
T: DependencyNode<I, E>,
I: PartialEq,
{
let mut order: Vec<&T> = vec![];
let mut order: Vec<&T> = Vec::new();
let mut dfs = DfsPostOrder::empty(&graph);
for root_node in root_nodes {
let idx = graph
Expand Down Expand Up @@ -128,8 +128,8 @@ mod tests {

#[test]
fn test_get_dependencies_one_level_deep() {
let a = ("a", vec![]);
let b = ("b", vec![]);
let a = ("a", Vec::new());
let b = ("b", Vec::new());
let c = ("c", vec!["a", "b"]);

let graph = create_dependency_graph(vec![a.clone(), b.clone(), c.clone()]).unwrap();
Expand All @@ -148,7 +148,7 @@ mod tests {

#[test]
fn test_get_dependencies_two_levels_deep() {
let a = ("a", vec![]);
let a = ("a", Vec::new());
let b = ("b", vec!["a"]);
let c = ("c", vec!["b"]);

Expand All @@ -169,9 +169,9 @@ mod tests {
#[test]
#[allow(clippy::many_single_char_names)]
fn test_get_dependencies_with_overlap() {
let a = ("a", vec![]);
let b = ("b", vec![]);
let c = ("c", vec![]);
let a = ("a", Vec::new());
let b = ("b", Vec::new());
let c = ("c", Vec::new());
let d = ("d", vec!["a", "b"]);
let e = ("e", vec!["b", "c"]);

Expand Down
2 changes: 1 addition & 1 deletion libcnb-test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ use libcnb_test::{BuildConfig, BuildpackReference, TestRunner};
// #[test]
fn additional_buildpacks() {
TestRunner::default().build(
BuildConfig::new("heroku/builder:22", "test-fixtures/app").buildpacks(vec![
BuildConfig::new("heroku/builder:22", "test-fixtures/app").buildpacks([
BuildpackReference::CurrentCrate,
BuildpackReference::WorkspaceBuildpack(buildpack_id!("my-project/buildpack")),
BuildpackReference::Other(String::from("heroku/another-buildpack")),
Expand Down
4 changes: 2 additions & 2 deletions libcnb-test/src/build_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl BuildConfig {
/// use libcnb_test::{BuildConfig, BuildpackReference, TestRunner};
///
/// TestRunner::default().build(
/// BuildConfig::new("heroku/builder:22", "test-fixtures/app").buildpacks(vec![
/// BuildConfig::new("heroku/builder:22", "test-fixtures/app").buildpacks([
/// BuildpackReference::CurrentCrate,
/// BuildpackReference::WorkspaceBuildpack(buildpack_id!("my-project/buildpack")),
/// BuildpackReference::Other(String::from("heroku/another-buildpack")),
Expand Down Expand Up @@ -149,7 +149,7 @@ impl BuildConfig {
/// use libcnb_test::{BuildConfig, TestRunner};
///
/// TestRunner::default().build(
/// BuildConfig::new("heroku/builder:22", "test-fixtures/app").envs(vec![
/// BuildConfig::new("heroku/builder:22", "test-fixtures/app").envs([
/// ("ENV_VAR_ONE", "VALUE ONE"),
/// ("ENV_VAR_TWO", "SOME OTHER VALUE"),
/// ]),
Expand Down
2 changes: 1 addition & 1 deletion libcnb-test/src/container_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl ContainerConfig {
/// |context| {
/// // ...
/// context.start_container(
/// ContainerConfig::new().envs(vec![("PORT", "5678"), ("DEBUG", "true")]),
/// ContainerConfig::new().envs([("PORT", "5678"), ("DEBUG", "true")]),
/// |container| {
/// // ...
/// },
Expand Down
Loading

0 comments on commit 4a8bc04

Please sign in to comment.