Skip to content

Commit cbca655

Browse files
committed
reduce clones and restrict visibility
1 parent 36cdb33 commit cbca655

4 files changed

Lines changed: 11 additions & 17 deletions

File tree

crates/ty_python_semantic/src/diagnostic/levenshtein.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::collections::BTreeSet;
1212
///
1313
/// If the typo itself starts with an underscore, this policy is ignored.
1414
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
15-
pub(crate) enum HideUnderscoredSuggestions {
15+
pub(super) enum HideUnderscoredSuggestions {
1616
Yes,
1717
#[cfg_attr(not(test), expect(dead_code))]
1818
No,
@@ -24,7 +24,7 @@ impl HideUnderscoredSuggestions {
2424
}
2525
}
2626

27-
pub(crate) fn find_best_suggestion<'a, O, I>(
27+
pub(super) fn find_best_suggestion<'a, O, I>(
2828
options: O,
2929
typo: &str,
3030
hide_underscored_suggestions: HideUnderscoredSuggestions,

crates/ty_python_semantic/src/diagnostic/mod.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,12 @@ use std::fmt::Write;
1212
pub(crate) mod levenshtein;
1313

1414
/// Suggest a name from `existing_names` that is similar to `wrong_name`.
15-
pub(crate) fn did_you_mean<S: AsRef<str>, T: AsRef<str>>(
16-
existing_names: impl Iterator<Item = S>,
17-
wrong_name: T,
18-
) -> Option<String> {
19-
let names: Vec<String> = existing_names.map(|n| n.as_ref().to_string()).collect();
20-
let name_refs: Vec<&str> = names.iter().map(String::as_str).collect();
21-
find_best_suggestion(
22-
name_refs,
23-
wrong_name.as_ref(),
24-
HideUnderscoredSuggestions::Yes,
25-
)
26-
.map(str::to_string)
15+
pub(crate) fn did_you_mean<'a, O, I>(options: O, typo: &str) -> Option<&'a str>
16+
where
17+
O: IntoIterator<IntoIter = I>,
18+
I: ExactSizeIterator<Item = &'a str>,
19+
{
20+
find_best_suggestion(options, typo, HideUnderscoredSuggestions::Yes)
2721
}
2822

2923
/// Add a subdiagnostic to `diagnostic` that explains why a certain Python version was inferred.

crates/ty_python_semantic/src/lint.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,11 +389,11 @@ impl LintRegistry {
389389
}
390390
}
391391

392-
let suggestion = did_you_mean(self.by_name.keys(), code);
392+
let suggestion = did_you_mean(self.by_name.keys().copied(), code);
393393

394394
Err(GetLintError::Unknown {
395395
code: code.to_string(),
396-
suggestion,
396+
suggestion: suggestion.map(str::to_string),
397397
})
398398
}
399399
}

crates/ty_python_semantic/src/types/diagnostic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4521,7 +4521,7 @@ pub(crate) fn report_invalid_key_on_typed_dict<'db>(
45214521
.message(format_args!("TypedDict `{typed_dict_name}`"))
45224522
});
45234523

4524-
let existing_keys = items.keys();
4524+
let existing_keys = items.keys().map(Name::as_str);
45254525
if let Some(suggestion) = did_you_mean(existing_keys, key) {
45264526
if let AnyNodeRef::ExprStringLiteral(literal) = key_node {
45274527
let quoted_suggestion = format!(

0 commit comments

Comments
 (0)