chore(clippy): enable and fix many stricter lints (#3783)

* chore(clippy): enable and fix many stricter lints

These ensure that the codebase uses more idiomatic, performant, and
concise language constructions.

* chore: make non-Clippy compiler warnings also deny by default
This commit is contained in:
Alejandro González
2025-06-14 02:10:12 +02:00
committed by GitHub
parent 301967d204
commit f84f8c1c2b
106 changed files with 542 additions and 760 deletions

View File

@@ -105,7 +105,7 @@ impl DBFriend {
created: row.created,
accepted: row.accepted,
})
.filter(|x| accepted.map(|y| y == x.accepted).unwrap_or(true))
.filter(|x| accepted.is_none_or(|y| y == x.accepted))
.collect::<Vec<_>>();
Ok(friends)

View File

@@ -675,7 +675,7 @@ impl LoaderFieldEnumValue {
.into_iter()
.filter(|x| {
let mut bool = true;
for (key, value) in filter.iter() {
for (key, value) in &filter {
if let Some(metadata_value) = x.metadata.get(key) {
bool &= metadata_value == value;
} else {
@@ -713,7 +713,7 @@ impl VersionField {
query_version_fields.push(base.clone().with_string_value(s))
}
VersionFieldValue::Boolean(b) => query_version_fields
.push(base.clone().with_int_value(if b { 1 } else { 0 })),
.push(base.clone().with_int_value(b as i32)),
VersionFieldValue::ArrayInteger(v) => {
for i in v {
query_version_fields
@@ -728,9 +728,8 @@ impl VersionField {
}
VersionFieldValue::ArrayBoolean(v) => {
for b in v {
query_version_fields.push(
base.clone().with_int_value(if b { 1 } else { 0 }),
);
query_version_fields
.push(base.clone().with_int_value(b as i32));
}
}
VersionFieldValue::Enum(_, v) => query_version_fields
@@ -757,7 +756,7 @@ impl VersionField {
l.field_id.0,
l.version_id.0,
l.int_value,
l.enum_value.as_ref().map(|e| e.0).unwrap_or(-1),
l.enum_value.as_ref().map_or(-1, |e| e.0),
l.string_value.clone(),
)
})
@@ -849,12 +848,11 @@ impl VersionField {
query_loader_fields
.iter()
.flat_map(|q| {
let loader_field_type = match LoaderFieldType::build(
let Some(loader_field_type) = LoaderFieldType::build(
&q.field_type,
q.enum_type.map(|l| l.0),
) {
Some(lft) => lft,
None => return vec![],
) else {
return vec![];
};
let loader_field = LoaderField {
id: q.id,
@@ -1085,23 +1083,17 @@ impl VersionFieldValue {
};
// Check errors- version_id must all be the same
// If the field type is a non-array, then the reason for multiple version ids is that there are multiple versions being aggregated, and those version ids are contained within.
// If the field type is an array, then the reason for multiple version ids is that there are multiple values for a single version
// (or a greater aggregation between multiple arrays, in which case the per-field version is lost, so we just take the first one and use it for that)
let version_id = qvfs
.iter()
.map(|qvf| qvf.version_id)
.unique()
.collect::<Vec<_>>();
// If the field type is a non-array, then the reason for multiple version ids is that there are multiple versions being aggregated, and those version ids are contained within.
// If the field type is an array, then the reason for multiple version ids is that there are multiple values for a single version
// (or a greater aggregation between multiple arrays, in which case the per-field version is lost, so we just take the first one and use it for that)
let version_id =
version_id.into_iter().next().unwrap_or(DBVersionId(0));
.next()
.unwrap_or(DBVersionId(0));
let field_id = qvfs
.iter()
.map(|qvf| qvf.field_id)
.unique()
.collect::<Vec<_>>();
if field_id.len() > 1 {
if qvfs.iter().map(|qvf| qvf.field_id).unique().count() > 1 {
return Err(DatabaseError::SchemaError(format!(
"Multiple field ids for field {field_name}"
)));
@@ -1274,7 +1266,7 @@ impl VersionFieldValue {
};
// Sort arrayenums by ordering, then by created
for (_, v) in value.iter_mut() {
for (_, v) in &mut value {
if let VersionFieldValue::ArrayEnum(_, v) = v {
v.sort_by(|a, b| {
a.ordering.cmp(&b.ordering).then(a.created.cmp(&b.created))
@@ -1317,8 +1309,8 @@ impl VersionFieldValue {
}
}
// For conversion to an interanl string(s), such as for search facets, filtering, or direct hardcoding
// No matter the type, it will be converted to a Vec<String>, whre the non-array types will have a single element
// For conversion to an internal string(s), such as for search facets, filtering, or direct hardcoding
// No matter the type, it will be converted to a Vec<String>, where the non-array types will have a single element
pub fn as_strings(&self) -> Vec<String> {
match self {
VersionFieldValue::Integer(i) => vec![i.to_string()],
@@ -1343,22 +1335,19 @@ impl VersionFieldValue {
VersionFieldValue::Integer(i) => value.as_i64() == Some(*i as i64),
VersionFieldValue::Text(s) => value.as_str() == Some(s),
VersionFieldValue::Boolean(b) => value.as_bool() == Some(*b),
VersionFieldValue::ArrayInteger(v) => value
.as_i64()
.map(|i| v.contains(&(i as i32)))
.unwrap_or(false),
VersionFieldValue::ArrayText(v) => value
.as_str()
.map(|s| v.contains(&s.to_string()))
.unwrap_or(false),
VersionFieldValue::ArrayInteger(v) => {
value.as_i64().is_some_and(|i| v.contains(&(i as i32)))
}
VersionFieldValue::ArrayText(v) => {
value.as_str().is_some_and(|s| v.contains(&s.to_string()))
}
VersionFieldValue::ArrayBoolean(v) => {
value.as_bool().map(|b| v.contains(&b)).unwrap_or(false)
value.as_bool().is_some_and(|b| v.contains(&b))
}
VersionFieldValue::Enum(_, v) => value.as_str() == Some(&v.value),
VersionFieldValue::ArrayEnum(_, v) => value
.as_str()
.map(|s| v.iter().any(|v| v.value == s))
.unwrap_or(false),
.is_some_and(|s| v.iter().any(|v| v.value == s)),
}
}
}

View File

@@ -122,7 +122,7 @@ impl DBOrganization {
|ids| async move {
let org_ids: Vec<i64> = ids
.iter()
.flat_map(|x| parse_base62(&x.to_string()).ok())
.filter_map(|x| parse_base62(&x.to_string()).ok())
.map(|x| x as i64)
.collect();
let slugs = ids

View File

@@ -108,7 +108,7 @@ impl DBPersonalAccessToken {
|ids| async move {
let pat_ids: Vec<i64> = ids
.iter()
.flat_map(|x| parse_base62(&x.to_string()).ok())
.filter_map(|x| parse_base62(&x.to_string()).ok())
.map(|x| x as i64)
.collect();
let slugs = ids.into_iter().map(|x| x.to_string()).collect::<Vec<_>>();

View File

@@ -545,7 +545,7 @@ impl DBProject {
let mut exec = exec.acquire().await?;
let project_ids_parsed: Vec<i64> = ids
.iter()
.flat_map(|x| parse_base62(&x.to_string()).ok())
.filter_map(|x| parse_base62(&x.to_string()).ok())
.map(|x| x as i64)
.collect();
let slugs = ids
@@ -723,7 +723,7 @@ impl DBProject {
// Add loader fields to the set we need to fetch
let loader_loader_field_ids = m.loader_fields.unwrap_or_default().into_iter().map(LoaderFieldId).collect::<Vec<_>>();
for loader_field_id in loader_loader_field_ids.iter() {
for loader_field_id in &loader_loader_field_ids {
loader_field_ids.insert(*loader_field_id);
}

View File

@@ -153,7 +153,7 @@ impl DBSession {
|ids| async move {
let session_ids: Vec<i64> = ids
.iter()
.flat_map(|x| parse_base62(&x.to_string()).ok())
.filter_map(|x| parse_base62(&x.to_string()).ok())
.map(|x| x as i64)
.collect();
let slugs = ids

View File

@@ -45,7 +45,7 @@ impl TeamBuilder {
.await?;
let mut team_member_ids = Vec::new();
for _ in self.members.iter() {
for _ in &self.members {
team_member_ids.push(generate_team_member_id(transaction).await?.0);
}
let TeamBuilder { members } = self;

View File

@@ -163,7 +163,7 @@ impl DBUser {
|ids| async move {
let user_ids: Vec<i64> = ids
.iter()
.flat_map(|x| parse_base62(&x.to_string()).ok())
.filter_map(|x| parse_base62(&x.to_string()).ok())
.map(|x| x as i64)
.collect();
let slugs = ids

View File

@@ -52,7 +52,7 @@ impl DependencyBuilder {
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
) -> Result<(), DatabaseError> {
let mut project_ids = Vec::new();
for dependency in builders.iter() {
for dependency in &builders {
project_ids.push(
dependency
.try_get_project_id(transaction)
@@ -333,9 +333,7 @@ impl DBVersion {
) -> Result<Option<()>, DatabaseError> {
let result = Self::get(id, &mut **transaction, redis).await?;
let result = if let Some(result) = result {
result
} else {
let Some(result) = result else {
return Ok(None);
};
@@ -550,7 +548,7 @@ impl DBVersion {
// Add loader fields to the set we need to fetch
let loader_loader_field_ids = m.loader_fields.unwrap_or_default().into_iter().map(LoaderFieldId).collect::<Vec<_>>();
for loader_field_id in loader_loader_field_ids.iter() {
for loader_field_id in &loader_loader_field_ids {
loader_field_ids.insert(*loader_field_id);
}
@@ -756,7 +754,7 @@ impl DBVersion {
let mut files = files.into_iter().map(|x| {
let mut file_hashes = HashMap::new();
for hash in hashes.iter() {
for hash in &hashes {
if hash.file_id == x.id {
file_hashes.insert(
hash.algorithm.clone(),
@@ -852,7 +850,7 @@ impl DBVersion {
ORDER BY v.date_published
",
algorithm,
&file_ids.into_iter().flat_map(|x| x.split('_').last().map(|x| x.as_bytes().to_vec())).collect::<Vec<_>>(),
&file_ids.into_iter().filter_map(|x| x.split('_').last().map(|x| x.as_bytes().to_vec())).collect::<Vec<_>>(),
)
.fetch(executor)
.try_fold(DashMap::new(), |acc, f| {
@@ -1042,14 +1040,14 @@ mod tests {
date_published,
project_id: DBProjectId(0),
author_id: DBUserId(0),
name: Default::default(),
version_number: Default::default(),
changelog: Default::default(),
downloads: Default::default(),
version_type: Default::default(),
featured: Default::default(),
name: String::new(),
version_number: String::new(),
changelog: String::new(),
downloads: 0,
version_type: String::new(),
featured: false,
status: VersionStatus::Listed,
requested_status: Default::default(),
requested_status: None,
}
}
}