prefer default over new

This commit is contained in:
BenjaminBrienen 2025-04-04 15:49:50 +02:00
parent 6ca780700d
commit 2462624a7d
46 changed files with 100 additions and 127 deletions

View file

@ -7,14 +7,20 @@ pub(crate) struct TopologicSortIterBuilder<T> {
nodes: FxHashMap<T, Entry<T>>,
}
// this implementation has different bounds on T than would be implied by #[derive(Default)]
impl<T> Default for TopologicSortIterBuilder<T>
where
T: Copy + Eq + PartialEq + Hash,
{
fn default() -> Self {
Self { nodes: Default::default() }
}
}
impl<T> TopologicSortIterBuilder<T>
where
T: Copy + Eq + PartialEq + Hash,
{
fn new() -> Self {
Self { nodes: Default::default() }
}
fn get_or_create_entry(&mut self, item: T) -> &mut Entry<T> {
self.nodes.entry(item).or_default()
}
@ -54,7 +60,7 @@ where
T: Copy + Eq + PartialEq + Hash,
{
pub(crate) fn builder() -> TopologicSortIterBuilder<T> {
TopologicSortIterBuilder::new()
TopologicSortIterBuilder::default()
}
pub(crate) fn pending(&self) -> usize {

View file

@ -469,7 +469,7 @@ impl SourceChangeBuilder {
}
fn add_snippet_annotation(&mut self, kind: AnnotationSnippet) -> SyntaxAnnotation {
let annotation = SyntaxAnnotation::new();
let annotation = SyntaxAnnotation::default();
self.snippet_annotations.push((kind, annotation));
self.source_change.is_snippet = true;
annotation

View file

@ -96,21 +96,16 @@ pub struct NameGenerator {
}
impl NameGenerator {
/// Create a new empty generator
pub fn new() -> Self {
Self { pool: FxHashMap::default() }
}
/// Create a new generator with existing names. When suggesting a name, it will
/// avoid conflicts with existing names.
pub fn new_with_names<'a>(existing_names: impl Iterator<Item = &'a str>) -> Self {
let mut generator = Self::new();
let mut generator = Self::default();
existing_names.for_each(|name| generator.insert(name));
generator
}
pub fn new_from_scope_locals(scope: Option<SemanticsScope<'_>>) -> Self {
let mut generator = Self::new();
let mut generator = Self::default();
if let Some(scope) = scope {
scope.process_all_names(&mut |name, scope| {
if let hir::ScopeDef::Local(_) = scope {
@ -471,7 +466,7 @@ mod tests {
frange.range,
"selection is not an expression(yet contained in one)"
);
let name = NameGenerator::new().for_variable(&expr, &sema);
let name = NameGenerator::default().for_variable(&expr, &sema);
assert_eq!(&name, expected);
}
@ -1118,7 +1113,7 @@ fn main() {
#[test]
fn conflicts_with_existing_names() {
let mut generator = NameGenerator::new();
let mut generator = NameGenerator::default();
assert_eq!(generator.suggest_name("a"), "a");
assert_eq!(generator.suggest_name("a"), "a1");
assert_eq!(generator.suggest_name("a"), "a2");