Skip to content

Commit a7be79b

Browse files
authored
refactor: clean up clippy::perf lint warnings (#15631)
### What does this PR try to resolve? The `clippy::perf` lint group is fairly useful for catching bad practices that might hurt performance marginally. This PR fixes most of them except `clippy::large_enum_variant`, which doesn't feel right at this moment and need more researches. And that is why I didn't enable the lint group. ### How to test and review this PR?
2 parents a32f073 + c3409d5 commit a7be79b

File tree

28 files changed

+73
-96
lines changed

28 files changed

+73
-96
lines changed

crates/cargo-test-support/src/paths.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub fn global_root() -> PathBuf {
6666
// [*] It does set the thread name, but only when running concurrently. If not
6767
// running concurrently, all tests are run on the main thread.
6868
thread_local! {
69-
static TEST_ID: RefCell<Option<usize>> = RefCell::new(None);
69+
static TEST_ID: RefCell<Option<usize>> = const { RefCell::new(None) };
7070
}
7171

7272
/// See [`init_root`]

crates/cargo-test-support/src/publish.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ fn split_index_features(mut features: FeatureMap) -> (FeatureMap, Option<Feature
322322
.iter()
323323
.any(|value| value.starts_with("dep:") || value.contains("?/"))
324324
{
325-
let new_values = values.drain(..).collect();
325+
let new_values = std::mem::take(values);
326326
features2.insert(feat.clone(), new_values);
327327
}
328328
}

crates/cargo-test-support/src/registry.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -646,15 +646,15 @@ pub struct HttpServerHandle {
646646

647647
impl HttpServerHandle {
648648
pub fn index_url(&self) -> Url {
649-
Url::parse(&format!("sparse+http://{}/index/", self.addr.to_string())).unwrap()
649+
Url::parse(&format!("sparse+http://{}/index/", self.addr)).unwrap()
650650
}
651651

652652
pub fn api_url(&self) -> Url {
653-
Url::parse(&format!("http://{}/", self.addr.to_string())).unwrap()
653+
Url::parse(&format!("http://{}/", self.addr)).unwrap()
654654
}
655655

656656
pub fn dl_url(&self) -> Url {
657-
Url::parse(&format!("http://{}/dl", self.addr.to_string())).unwrap()
657+
Url::parse(&format!("http://{}/dl", self.addr)).unwrap()
658658
}
659659

660660
fn stop(&self) {
@@ -892,7 +892,7 @@ impl HttpServer {
892892

893893
// - The URL matches the registry base URL
894894
if footer.url != "https://github.com/rust-lang/crates.io-index"
895-
&& footer.url != &format!("sparse+http://{}/index/", self.addr.to_string())
895+
&& footer.url != &format!("sparse+http://{}/index/", self.addr)
896896
{
897897
return false;
898898
}

crates/mdman/src/format/man.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ impl<'e> ManRenderer<'e> {
427427
}
428428
}
429429

430+
#[allow(clippy::collapsible_str_replace)]
430431
fn escape(s: &str) -> Result<String, Error> {
431432
// Note: Possible source on output escape sequences: https://man7.org/linux/man-pages/man7/groff_char.7.html.
432433
// Otherwise, use generic escaping in the form `\[u1EE7]` or `\[u1F994]`.

crates/resolver-tests/tests/proptests.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -205,19 +205,15 @@ proptest! {
205205
// If resolution was successful, then unpublishing a version of a crate
206206
// that was not selected should not change that.
207207
let not_selected: Vec<_> = input
208-
.iter()
209-
.cloned()
210-
.filter(|x| !r.contains(&x.package_id()))
208+
.iter().filter(|&x| !r.contains(&x.package_id())).cloned()
211209
.collect();
212210

213211
if !not_selected.is_empty() {
214212
let indexes_to_unpublish: Vec<_> = indexes_to_unpublish.iter().map(|x| x.get(&not_selected)).collect();
215213

216214
let new_reg = registry(
217215
input
218-
.iter()
219-
.cloned()
220-
.filter(|x| !indexes_to_unpublish.contains(&x))
216+
.iter().filter(|&x| !indexes_to_unpublish.contains(&x)).cloned()
221217
.collect(),
222218
);
223219

@@ -248,9 +244,7 @@ proptest! {
248244

249245
let new_reg = registry(
250246
input
251-
.iter()
252-
.cloned()
253-
.filter(|x| !indexes_to_unpublish.contains(&x))
247+
.iter().filter(|&x| !indexes_to_unpublish.contains(&x)).cloned()
254248
.collect(),
255249
);
256250

crates/resolver-tests/tests/resolve.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -741,8 +741,8 @@ fn incomplete_information_skipping() {
741741
let new_reg = registry(
742742
input
743743
.iter()
744+
.filter(|&x| package_to_yank != x.package_id())
744745
.cloned()
745-
.filter(|x| package_to_yank != x.package_id())
746746
.collect(),
747747
);
748748
assert_eq!(input.len(), new_reg.len() + 1);
@@ -810,8 +810,8 @@ fn incomplete_information_skipping_2() {
810810
let new_reg = registry(
811811
input
812812
.iter()
813+
.filter(|&x| package_to_yank != x.package_id())
813814
.cloned()
814-
.filter(|x| package_to_yank != x.package_id())
815815
.collect(),
816816
);
817817
assert_eq!(input.len(), new_reg.len() + 1);
@@ -860,8 +860,8 @@ fn incomplete_information_skipping_3() {
860860
let new_reg = registry(
861861
input
862862
.iter()
863+
.filter(|&x| package_to_yank != x.package_id())
863864
.cloned()
864-
.filter(|x| package_to_yank != x.package_id())
865865
.collect(),
866866
);
867867
assert_eq!(input.len(), new_reg.len() + 1);

src/cargo/core/features.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,7 @@ impl GitFeatures {
975975
}
976976

977977
fn expecting() -> String {
978-
let fields = vec!["`shallow-index`", "`shallow-deps`"];
978+
let fields = ["`shallow-index`", "`shallow-deps`"];
979979
format!(
980980
"unstable 'git' only takes {} as valid inputs",
981981
fields.join(" and ")
@@ -1087,7 +1087,7 @@ impl GitoxideFeatures {
10871087
}
10881088

10891089
fn expecting() -> String {
1090-
let fields = vec!["`fetch`", "`checkout`", "`internal-use-git2`"];
1090+
let fields = ["`fetch`", "`checkout`", "`internal-use-git2`"];
10911091
format!(
10921092
"unstable 'gitoxide' only takes {} as valid inputs, for shallow fetches see `-Zgit=shallow-index,shallow-deps`",
10931093
fields.join(" and ")

src/cargo/core/manifest.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ impl ManifestMetadata {
151151
/// Whether the given env var should be tracked by Cargo's dep-info.
152152
pub fn should_track(env_key: &str) -> bool {
153153
let keys = MetadataEnvs::keys();
154-
keys.iter().any(|k| *k == env_key)
154+
keys.contains(&env_key)
155155
}
156156

157157
pub fn env_var<'a>(&'a self, env_key: &str) -> Option<Cow<'a, str>> {
@@ -1016,21 +1016,21 @@ impl Target {
10161016

10171017
pub fn is_dylib(&self) -> bool {
10181018
match self.kind() {
1019-
TargetKind::Lib(libs) => libs.iter().any(|l| *l == CrateType::Dylib),
1019+
TargetKind::Lib(libs) => libs.contains(&CrateType::Dylib),
10201020
_ => false,
10211021
}
10221022
}
10231023

10241024
pub fn is_cdylib(&self) -> bool {
10251025
match self.kind() {
1026-
TargetKind::Lib(libs) => libs.iter().any(|l| *l == CrateType::Cdylib),
1026+
TargetKind::Lib(libs) => libs.contains(&CrateType::Cdylib),
10271027
_ => false,
10281028
}
10291029
}
10301030

10311031
pub fn is_staticlib(&self) -> bool {
10321032
match self.kind() {
1033-
TargetKind::Lib(libs) => libs.iter().any(|l| *l == CrateType::Staticlib),
1033+
TargetKind::Lib(libs) => libs.contains(&CrateType::Staticlib),
10341034
_ => false,
10351035
}
10361036
}

src/cargo/core/package.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1182,7 +1182,7 @@ mod tls {
11821182

11831183
use super::Downloads;
11841184

1185-
thread_local!(static PTR: Cell<usize> = Cell::new(0));
1185+
thread_local!(static PTR: Cell<usize> = const { Cell::new(0) });
11861186

11871187
pub(crate) fn with<R>(f: impl FnOnce(Option<&Downloads<'_, '_>>) -> R) -> R {
11881188
let ptr = PTR.with(|p| p.get());

src/cargo/core/resolver/encode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ impl ser::Serialize for Resolve {
698698
None => "<none>",
699699
};
700700
let id = encodable_package_id(id, &state, self.version());
701-
metadata.insert(format!("checksum {}", id.to_string()), checksum.to_string());
701+
metadata.insert(format!("checksum {}", id), checksum.to_string());
702702
}
703703
}
704704

0 commit comments

Comments
 (0)