Skip to content

Use let chains to simplify nested conditional patterns #11467

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/controllers/krate/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,12 @@ pub async fn publish(app: AppState, req: Parts, body: Body) -> AppResult<Json<Go
return Err(bad_request(&message));
}

if let Some(description) = &description {
if description.len() > MAX_DESCRIPTION_LENGTH {
return Err(bad_request(format!(
"The `description` is too long. A maximum of {MAX_DESCRIPTION_LENGTH} characters are currently allowed."
)));
}
if let Some(description) = &description
&& description.len() > MAX_DESCRIPTION_LENGTH
{
return Err(bad_request(format!(
"The `description` is too long. A maximum of {MAX_DESCRIPTION_LENGTH} characters are currently allowed."
)));
}

if let Some(ref license) = license {
Expand Down
95 changes: 47 additions & 48 deletions src/controllers/krate/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,43 +118,42 @@ pub async fn list_crates(
.left_join(versions::table.on(default_versions::version_id.eq(versions::id)))
.select(selection);

if let Some(q_string) = &filter_params.q_string {
if !q_string.is_empty() {
let q_string = q_string.as_str();

let sort = sort.unwrap_or("relevance");

query = query.order(Crate::with_name(q_string).desc());

if sort == "relevance" {
let q =
plainto_tsquery_with_search_config(TsConfigurationByName("english"), q_string);
let rank = ts_rank_cd(crates::textsearchable_index_col, q);
query = query.select((
ALL_COLUMNS,
Crate::with_name(q_string),
crate_downloads::downloads,
recent_crate_downloads::downloads.nullable(),
rank,
versions::num.nullable(),
versions::yanked.nullable(),
default_versions::num_versions.nullable(),
));
seek = Some(Seek::Relevance);
query = query.then_order_by(rank.desc())
} else {
query = query.select((
ALL_COLUMNS,
Crate::with_name(q_string),
crate_downloads::downloads,
recent_crate_downloads::downloads.nullable(),
0_f32.into_sql::<Float>(),
versions::num.nullable(),
versions::yanked.nullable(),
default_versions::num_versions.nullable(),
));
seek = Some(Seek::Query);
}
if let Some(q_string) = &filter_params.q_string
&& !q_string.is_empty()
{
let q_string = q_string.as_str();

let sort = sort.unwrap_or("relevance");

query = query.order(Crate::with_name(q_string).desc());

if sort == "relevance" {
let q = plainto_tsquery_with_search_config(TsConfigurationByName("english"), q_string);
let rank = ts_rank_cd(crates::textsearchable_index_col, q);
query = query.select((
ALL_COLUMNS,
Crate::with_name(q_string),
crate_downloads::downloads,
recent_crate_downloads::downloads.nullable(),
rank,
versions::num.nullable(),
versions::yanked.nullable(),
default_versions::num_versions.nullable(),
));
seek = Some(Seek::Relevance);
query = query.then_order_by(rank.desc())
} else {
query = query.select((
ALL_COLUMNS,
Crate::with_name(q_string),
crate_downloads::downloads,
recent_crate_downloads::downloads.nullable(),
0_f32.into_sql::<Float>(),
versions::num.nullable(),
versions::yanked.nullable(),
default_versions::num_versions.nullable(),
));
seek = Some(Seek::Query);
}
}

Expand Down Expand Up @@ -384,17 +383,17 @@ impl FilterParams {
fn make_query(&self) -> crates::BoxedQuery<'_, diesel::pg::Pg> {
let mut query = crates::table.into_boxed();

if let Some(q_string) = &self.q_string {
if !q_string.is_empty() {
let q = plainto_tsquery_with_search_config(
TsConfigurationByName("english"),
q_string.as_str(),
);
query = query.filter(
q.matches(crates::textsearchable_index_col)
.or(Crate::loosly_matches_name(q_string.as_str())),
);
}
if let Some(q_string) = &self.q_string
&& !q_string.is_empty()
{
let q = plainto_tsquery_with_search_config(
TsConfigurationByName("english"),
q_string.as_str(),
);
query = query.filter(
q.matches(crates::textsearchable_index_col)
.or(Crate::loosly_matches_name(q_string.as_str())),
);
}

if let Some(cat) = &self.category {
Expand Down
28 changes: 14 additions & 14 deletions src/typosquat/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ impl Check for Affixes {
// If the package being examined starts with this prefix and separator combo, then
// we should see if it exists without that prefix in the popular crate corpus.
let combo = format!("{affix}{separator}");
if let Some(stem) = name.strip_prefix(&combo) {
if corpus.possible_squat(stem, name, package)? {
squats.push(Squat::Custom {
message: format!("adds the {combo} prefix"),
package: stem.to_string(),
})
}
if let Some(stem) = name.strip_prefix(&combo)
&& corpus.possible_squat(stem, name, package)?
{
squats.push(Squat::Custom {
message: format!("adds the {combo} prefix"),
package: stem.to_string(),
})
}

// Alternatively, let's see if adding the prefix and separator combo to the package
Expand All @@ -59,13 +59,13 @@ impl Check for Affixes {
// If the package being examined ends in this separator and suffix combo, then we
// should see if it exists without that suffix in the popular crate corpus.
let combo = format!("{separator}{affix}");
if let Some(stem) = name.strip_suffix(&combo) {
if corpus.possible_squat(stem, name, package)? {
squats.push(Squat::Custom {
message: format!("adds the {combo} suffix"),
package: stem.to_string(),
})
}
if let Some(stem) = name.strip_suffix(&combo)
&& corpus.possible_squat(stem, name, package)?
{
squats.push(Squat::Custom {
message: format!("adds the {combo} suffix"),
package: stem.to_string(),
})
}

// Alternatively, let's see if adding the separator and suffix combo to the package
Expand Down