From 501519c9393db4b1f9390fb5dcc9e430596f2c5d Mon Sep 17 00:00:00 2001 From: Samuel Tardieu Date: Wed, 2 Oct 2024 22:00:32 +0200 Subject: [PATCH] style: avoid using `.to_owned()`/`.to_vec()` on owned objects `.clone()` is more explicit when we already have an object of the right type. --- Cargo.toml | 1 + cli/src/commands/fix.rs | 2 +- cli/src/config.rs | 12 ++++++------ cli/tests/test_config_command.rs | 6 +++--- cli/tests/test_operations.rs | 2 +- lib/src/git_backend.rs | 2 +- lib/src/local_backend.rs | 2 +- lib/src/repo.rs | 2 +- lib/src/settings.rs | 2 +- lib/src/workspace.rs | 4 ++-- lib/tests/test_local_working_copy.rs | 2 +- 11 files changed, 19 insertions(+), 18 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3814c74bec..50062118d3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -134,6 +134,7 @@ testutils = { path = "lib/testutils" } [workspace.lints.clippy] explicit_iter_loop = "deny" +implicit_clone = "deny" semicolon_if_nothing_returned = "deny" uninlined_format_args = "deny" diff --git a/cli/src/commands/fix.rs b/cli/src/commands/fix.rs index f4c751faec..f720326a1e 100644 --- a/cli/src/commands/fix.rs +++ b/cli/src/commands/fix.rs @@ -173,7 +173,7 @@ pub(crate) fn cmd_fix( // reliably produce well formatted code anyway. Deduplicating inputs helps // to prevent quadratic growth in the number of tool executions required for // doing this in long chains of commits with disjoint sets of modified files. - let commits: Vec<_> = RevsetExpression::commits(root_commits.to_vec()) + let commits: Vec<_> = RevsetExpression::commits(root_commits.clone()) .descendants() .evaluate_programmatic(tx.base_repo().as_ref())? .iter() diff --git a/cli/src/config.rs b/cli/src/config.rs index c8fc30f8b2..7ee504b6c3 100644 --- a/cli/src/config.rs +++ b/cli/src/config.rs @@ -323,7 +323,7 @@ impl LayeredConfigs { config_vals.push(AnnotatedValue { path, value: value.to_owned(), - source: source.to_owned(), + source: source.clone(), // Note: Value updated below. is_overridden: false, }); @@ -888,8 +888,8 @@ mod tests { fn test_layered_configs_resolved_config_values_empty() { let empty_config = config::Config::default(); let layered_configs = LayeredConfigs { - default: empty_config.to_owned(), - env_base: empty_config.to_owned(), + default: empty_config.clone(), + env_base: empty_config.clone(), user: None, repo: None, env_overrides: empty_config, @@ -919,7 +919,7 @@ mod tests { .build() .unwrap(); let layered_configs = LayeredConfigs { - default: empty_config.to_owned(), + default: empty_config.clone(), env_base: env_base_config, user: None, repo: Some(repo_config), @@ -1042,8 +1042,8 @@ mod tests { .build() .unwrap(); let layered_configs = LayeredConfigs { - default: empty_config.to_owned(), - env_base: empty_config.to_owned(), + default: empty_config.clone(), + env_base: empty_config.clone(), user: Some(user_config), repo: Some(repo_config), env_overrides: empty_config, diff --git a/cli/tests/test_config_command.rs b/cli/tests/test_config_command.rs index 24c7705919..4740baa5aa 100644 --- a/cli/tests/test_config_command.rs +++ b/cli/tests/test_config_command.rs @@ -183,7 +183,7 @@ fn test_config_list_layer() { let mut test_env = TestEnvironment::default(); test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "repo"]); let user_config_path = test_env.config_path().join("config.toml"); - test_env.set_config_path(user_config_path.to_owned()); + test_env.set_config_path(user_config_path.clone()); let repo_path = test_env.env_root().join("repo"); // User @@ -456,7 +456,7 @@ fn test_config_set_for_user() { test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "repo"]); // Point to a config file since `config set` can't handle directories. let user_config_path = test_env.config_path().join("config.toml"); - test_env.set_config_path(user_config_path.to_owned()); + test_env.set_config_path(user_config_path.clone()); let repo_path = test_env.env_root().join("repo"); test_env.jj_cmd_ok( @@ -840,7 +840,7 @@ fn test_config_show_paths() { let mut test_env = TestEnvironment::default(); test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "repo"]); let user_config_path = test_env.config_path().join("config.toml"); - test_env.set_config_path(user_config_path.to_owned()); + test_env.set_config_path(user_config_path.clone()); let repo_path = test_env.env_root().join("repo"); test_env.jj_cmd_ok( diff --git a/cli/tests/test_operations.rs b/cli/tests/test_operations.rs index 27649eab3b..565ac35ff0 100644 --- a/cli/tests/test_operations.rs +++ b/cli/tests/test_operations.rs @@ -738,7 +738,7 @@ fn test_op_recover_from_bad_gc() { let repo_path = test_env.env_root().join("repo"); let git_object_path = |hex: &str| { let (shard, file_name) = hex.split_at(2); - let mut file_path = repo_path.to_owned(); + let mut file_path = repo_path.clone(); file_path.extend([".git", "objects", shard, file_name]); file_path }; diff --git a/lib/src/git_backend.rs b/lib/src/git_backend.rs index 7d2b267e99..47c67af7c8 100644 --- a/lib/src/git_backend.rs +++ b/lib/src/git_backend.rs @@ -957,7 +957,7 @@ impl Backend for GitBackend { .map_err(|err| to_read_object_err(err, id))?; let target = String::from_utf8(blob.take_data()) .map_err(|err| to_invalid_utf8_err(err.utf8_error(), id))? - .to_owned(); + .clone(); Ok(target) } diff --git a/lib/src/local_backend.rs b/lib/src/local_backend.rs index 36020f00cc..c0255bb2de 100644 --- a/lib/src/local_backend.rs +++ b/lib/src/local_backend.rs @@ -388,7 +388,7 @@ fn commit_from_proto(mut proto: crate::protos::local_store::Commit) -> Commit { MergedTreeId::Merge(merge_builder.build()) } else { assert_eq!(proto.root_tree.len(), 1); - MergedTreeId::Legacy(TreeId::new(proto.root_tree[0].to_vec())) + MergedTreeId::Legacy(TreeId::new(proto.root_tree[0].clone())) }; let change_id = ChangeId::new(proto.change_id); Commit { diff --git a/lib/src/repo.rs b/lib/src/repo.rs index 1d89b600ff..1a2ef180e5 100644 --- a/lib/src/repo.rs +++ b/lib/src/repo.rs @@ -1131,7 +1131,7 @@ impl MutableRepo { let commit = self .new_commit( settings, - new_commit_ids.to_vec(), + new_commit_ids.clone(), merged_parents_tree.id().clone(), ) .write()?; diff --git a/lib/src/settings.rs b/lib/src/settings.rs index e787f0b825..3b7551de96 100644 --- a/lib/src/settings.rs +++ b/lib/src/settings.rs @@ -171,7 +171,7 @@ impl UserSettings { pub const USER_EMAIL_PLACEHOLDER: &'static str = "(no email configured)"; pub fn commit_timestamp(&self) -> Option { - self.timestamp.to_owned() + self.timestamp } pub fn operation_timestamp(&self) -> Option { diff --git a/lib/src/workspace.rs b/lib/src/workspace.rs index 7d03ca610f..f8f6a49602 100644 --- a/lib/src/workspace.rs +++ b/lib/src/workspace.rs @@ -609,8 +609,8 @@ impl WorkspaceLoader for DefaultWorkspaceLoader { ) -> Result, WorkspaceLoadError> { Ok(working_copy_factory.load_working_copy( store.clone(), - self.workspace_root.to_owned(), - self.working_copy_state_path.to_owned(), + self.workspace_root.clone(), + self.working_copy_state_path.clone(), )?) } } diff --git a/lib/tests/test_local_working_copy.rs b/lib/tests/test_local_working_copy.rs index 0bf7d8cda6..d6fae2b4ca 100644 --- a/lib/tests/test_local_working_copy.rs +++ b/lib/tests/test_local_working_copy.rs @@ -234,7 +234,7 @@ fn test_checkout_file_transitions(backend: TestRepoBackend) { let path = RepoPathBuf::from_internal_string(format!("{left_kind:?}_{right_kind:?}")); write_path(&settings, repo, &mut left_tree_builder, *left_kind, &path); write_path(&settings, repo, &mut right_tree_builder, *right_kind, &path); - files.push((*left_kind, *right_kind, path.to_owned())); + files.push((*left_kind, *right_kind, path.clone())); } } let left_tree_id = left_tree_builder.write_tree(&store).unwrap();