Skip to content

Commit afcb45e

Browse files
authored
Merge pull request #19520 from BenjaminBrienen/defaultnew
Align usage of `default` and `new` with style guide
2 parents 6ca7807 + 2462624 commit afcb45e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+98
-125
lines changed

crates/base-db/src/change.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ impl fmt::Debug for FileChange {
3434
}
3535

3636
impl FileChange {
37-
pub fn new() -> Self {
38-
FileChange::default()
39-
}
40-
4137
pub fn set_roots(&mut self, roots: Vec<SourceRoot>) {
4238
self.roots = Some(roots);
4339
}

crates/hir-def/src/dyn_map.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ pub struct Key<K, V, P = (K, V)> {
112112
}
113113

114114
impl<K, V, P> Key<K, V, P> {
115+
#[allow(
116+
clippy::new_without_default,
117+
reason = "this a const fn, so it can't be default yet. See <https://github.com/rust-lang/rust/issues/63065>"
118+
)]
115119
pub(crate) const fn new() -> Key<K, V, P> {
116120
Key { _phantom: PhantomData }
117121
}
@@ -148,16 +152,11 @@ impl<K: Hash + Eq + 'static, V: 'static> Policy for (K, V) {
148152
}
149153
}
150154

155+
#[derive(Default)]
151156
pub struct DynMap {
152157
pub(crate) map: Map,
153158
}
154159

155-
impl Default for DynMap {
156-
fn default() -> Self {
157-
DynMap { map: Map::new() }
158-
}
159-
}
160-
161160
#[repr(transparent)]
162161
pub struct KeyMap<KEY> {
163162
map: DynMap,

crates/hir-def/src/expr_store/lower.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2053,7 +2053,7 @@ impl ExprCollector<'_> {
20532053
f: ast::FormatArgsExpr,
20542054
syntax_ptr: AstPtr<ast::Expr>,
20552055
) -> ExprId {
2056-
let mut args = FormatArgumentsCollector::new();
2056+
let mut args = FormatArgumentsCollector::default();
20572057
f.args().for_each(|arg| {
20582058
args.add(FormatArgument {
20592059
kind: match arg.name() {

crates/hir-def/src/hir/format_args.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -460,10 +460,6 @@ impl FormatArgumentsCollector {
460460
}
461461
}
462462

463-
pub fn new() -> Self {
464-
Default::default()
465-
}
466-
467463
pub fn add(&mut self, arg: FormatArgument) -> usize {
468464
let index = self.arguments.len();
469465
if let Some(name) = arg.kind.ident() {

crates/hir-expand/src/change.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ pub struct ChangeWithProcMacros {
1414
}
1515

1616
impl ChangeWithProcMacros {
17-
pub fn new() -> Self {
18-
Self::default()
19-
}
20-
2117
pub fn apply(self, db: &mut impl ExpandDatabase) {
2218
let crates_id_map = self.source_change.apply(db);
2319
if let Some(proc_macros) = self.proc_macros {

crates/ide-assists/src/handlers/add_braces.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub(crate) fn add_braces(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<(
3939
},
4040
expr.syntax().text_range(),
4141
|builder| {
42-
let make = SyntaxFactory::new();
42+
let make = SyntaxFactory::with_mappings();
4343
let mut editor = builder.make_editor(expr.syntax());
4444

4545
let block_expr = make.block_expr(None, Some(expr.clone()));

crates/ide-assists/src/handlers/add_missing_match_arms.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext<'_>)
7777

7878
let cfg = ctx.config.import_path_config();
7979

80-
let make = SyntaxFactory::new();
80+
let make = SyntaxFactory::with_mappings();
8181

8282
let module = ctx.sema.scope(expr.syntax())?.module();
8383
let (mut missing_pats, is_non_exhaustive, has_hidden_variants): (
@@ -467,7 +467,7 @@ fn build_pat(
467467
let fields = var.fields(db);
468468
let pat: ast::Pat = match var.kind(db) {
469469
hir::StructKind::Tuple => {
470-
let mut name_generator = suggest_name::NameGenerator::new();
470+
let mut name_generator = suggest_name::NameGenerator::default();
471471
let pats = fields.into_iter().map(|f| {
472472
let name = name_generator.for_type(&f.ty(db), db, edition);
473473
match name {

crates/ide-assists/src/handlers/add_turbo_fish.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ pub(crate) fn add_turbo_fish(acc: &mut Assists, ctx: &AssistContext<'_>) -> Opti
141141
|builder| {
142142
builder.trigger_parameter_hints();
143143

144-
let make = SyntaxFactory::new();
144+
let make = SyntaxFactory::with_mappings();
145145
let mut editor = match &turbofish_target {
146146
Either::Left(it) => builder.make_editor(it.syntax()),
147147
Either::Right(it) => builder.make_editor(it.syntax()),

crates/ide-assists/src/handlers/apply_demorgan.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub(crate) fn apply_demorgan(acc: &mut Assists, ctx: &AssistContext<'_>) -> Opti
6464
_ => return None,
6565
};
6666

67-
let make = SyntaxFactory::new();
67+
let make = SyntaxFactory::with_mappings();
6868

6969
let demorganed = bin_expr.clone_subtree();
7070
let mut editor = SyntaxEditor::new(demorganed.syntax().clone());
@@ -111,7 +111,7 @@ pub(crate) fn apply_demorgan(acc: &mut Assists, ctx: &AssistContext<'_>) -> Opti
111111
"Apply De Morgan's law",
112112
op_range,
113113
|builder| {
114-
let make = SyntaxFactory::new();
114+
let make = SyntaxFactory::with_mappings();
115115
let paren_expr = bin_expr.syntax().parent().and_then(ast::ParenExpr::cast);
116116
let neg_expr = paren_expr
117117
.clone()
@@ -194,7 +194,7 @@ pub(crate) fn apply_demorgan_iterator(acc: &mut Assists, ctx: &AssistContext<'_>
194194
label,
195195
op_range,
196196
|builder| {
197-
let make = SyntaxFactory::new();
197+
let make = SyntaxFactory::with_mappings();
198198
let mut editor = builder.make_editor(method_call.syntax());
199199
// replace the method name
200200
let new_name = match name.text().as_str() {

crates/ide-assists/src/handlers/convert_bool_then.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ pub(crate) fn convert_if_to_bool_then(acc: &mut Assists, ctx: &AssistContext<'_>
9898
let closure_body = ast::Expr::cast(edit.new_root().clone()).unwrap();
9999

100100
let mut editor = builder.make_editor(expr.syntax());
101-
let make = SyntaxFactory::new();
101+
let make = SyntaxFactory::with_mappings();
102102
let closure_body = match closure_body {
103103
ast::Expr::BlockExpr(block) => unwrap_trivial_block(block),
104104
e => e,
@@ -216,7 +216,7 @@ pub(crate) fn convert_bool_then_to_if(acc: &mut Assists, ctx: &AssistContext<'_>
216216
let closure_body = ast::BlockExpr::cast(edit.new_root().clone()).unwrap();
217217

218218
let mut editor = builder.make_editor(mcall.syntax());
219-
let make = SyntaxFactory::new();
219+
let make = SyntaxFactory::with_mappings();
220220

221221
let cond = match &receiver {
222222
ast::Expr::ParenExpr(expr) => expr.expr().unwrap_or(receiver),

crates/ide-assists/src/handlers/convert_for_to_while_let.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub(crate) fn convert_for_loop_to_while_let(
5151
"Replace this for loop with `while let`",
5252
for_loop.syntax().text_range(),
5353
|builder| {
54-
let make = SyntaxFactory::new();
54+
let make = SyntaxFactory::with_mappings();
5555
let mut editor = builder.make_editor(for_loop.syntax());
5656

5757
let (iterable, method) = if impls_core_iter(&ctx.sema, &iterable) {

crates/ide-assists/src/handlers/extract_variable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ pub(crate) fn extract_variable(acc: &mut Assists, ctx: &AssistContext<'_>) -> Op
170170
|edit| {
171171
let (var_name, expr_replace) = kind.get_name_and_expr(ctx, &to_extract);
172172

173-
let make = SyntaxFactory::new();
173+
let make = SyntaxFactory::with_mappings();
174174
let mut editor = edit.make_editor(&expr_replace);
175175

176176
let pat_name = make.name(&var_name);

crates/ide-assists/src/handlers/flip_binexpr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub(crate) fn flip_binexpr(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option
4848
op_token.text_range(),
4949
|builder| {
5050
let mut editor = builder.make_editor(&expr.syntax().parent().unwrap());
51-
let make = SyntaxFactory::new();
51+
let make = SyntaxFactory::with_mappings();
5252
if let FlipAction::FlipAndReplaceOp(binary_op) = action {
5353
editor.replace(op_token, make.token(binary_op))
5454
};

crates/ide-assists/src/handlers/flip_comma.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ fn flip_tree(tree: ast::TokenTree, comma: SyntaxToken) -> (ast::TokenTree, Synta
101101
]
102102
.concat();
103103

104-
let make = SyntaxFactory::new();
104+
let make = SyntaxFactory::with_mappings();
105105
let new_token_tree = make.token_tree(tree.left_delimiter_token().unwrap().kind(), result);
106106
(new_token_tree, make.finish_with_mappings())
107107
}

crates/ide-assists/src/handlers/generate_enum_variant.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub(crate) fn generate_enum_variant(acc: &mut Assists, ctx: &AssistContext<'_>)
6060

6161
acc.add(AssistId::generate("generate_enum_variant"), "Generate variant", target, |builder| {
6262
let mut editor = builder.make_editor(enum_node.syntax());
63-
let make = SyntaxFactory::new();
63+
let make = SyntaxFactory::with_mappings();
6464
let field_list = parent.make_field_list(ctx, &make);
6565
let variant = make.variant(None, make.name(&name_ref.text()), field_list, None);
6666
if let Some(it) = enum_node.variant_list() {

crates/ide-assists/src/handlers/inline_local_variable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ pub(crate) fn inline_local_variable(acc: &mut Assists, ctx: &AssistContext<'_>)
9191
}
9292
}
9393

94-
let make = SyntaxFactory::new();
94+
let make = SyntaxFactory::with_mappings();
9595

9696
for (name, should_wrap) in wrap_in_parens {
9797
let replacement = if should_wrap {

crates/ide-assists/src/handlers/introduce_named_type_parameter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub(crate) fn introduce_named_type_parameter(
2424
let fn_ = param.syntax().ancestors().nth(2).and_then(ast::Fn::cast)?;
2525
let type_bound_list = impl_trait_type.type_bound_list()?;
2626

27-
let make = SyntaxFactory::new();
27+
let make = SyntaxFactory::with_mappings();
2828
let target = fn_.syntax().text_range();
2929
acc.add(
3030
AssistId::refactor_rewrite("introduce_named_type_parameter"),

crates/ide-assists/src/handlers/remove_parentheses.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub(crate) fn remove_parentheses(acc: &mut Assists, ctx: &AssistContext<'_>) ->
5454
None => false,
5555
};
5656
if need_to_add_ws {
57-
let make = SyntaxFactory::new();
57+
let make = SyntaxFactory::with_mappings();
5858
editor.insert(Position::before(parens.syntax()), make.whitespace(" "));
5959
editor.add_mappings(make.finish_with_mappings());
6060
}

crates/ide-assists/src/handlers/replace_if_let_with_match.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ pub(crate) fn replace_if_let_with_match(acc: &mut Assists, ctx: &AssistContext<'
105105
format!("Replace if{let_} with match"),
106106
available_range,
107107
move |builder| {
108-
let make = SyntaxFactory::new();
108+
let make = SyntaxFactory::with_mappings();
109109
let match_expr = {
110110
let else_arm = make_else_arm(ctx, &make, else_block, &cond_bodies);
111111
let make_match_arm = |(pat, body): (_, ast::BlockExpr)| {
@@ -253,7 +253,7 @@ pub(crate) fn replace_match_with_if_let(acc: &mut Assists, ctx: &AssistContext<'
253253
format!("Replace match with if{let_}"),
254254
match_expr.syntax().text_range(),
255255
move |builder| {
256-
let make = SyntaxFactory::new();
256+
let make = SyntaxFactory::with_mappings();
257257
let make_block_expr = |expr: ast::Expr| {
258258
// Blocks with modifiers (unsafe, async, etc.) are parsed as BlockExpr, but are
259259
// formatted without enclosing braces. If we encounter such block exprs,

crates/ide-assists/src/handlers/replace_let_with_if_let.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub(crate) fn replace_let_with_if_let(acc: &mut Assists, ctx: &AssistContext<'_>
4343
target,
4444
|builder| {
4545
let mut editor = builder.make_editor(let_stmt.syntax());
46-
let make = SyntaxFactory::new();
46+
let make = SyntaxFactory::with_mappings();
4747
let ty = ctx.sema.type_of_expr(&init);
4848
let pat = if let_stmt.let_else().is_some() {
4949
// Do not add the wrapper type that implements `Try`,

crates/ide-assists/src/handlers/unwrap_return_type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ pub(crate) fn unwrap_return_type(acc: &mut Assists, ctx: &AssistContext<'_>) ->
6767

6868
acc.add(kind.assist_id(), kind.label(), type_ref.syntax().text_range(), |builder| {
6969
let mut editor = builder.make_editor(&parent);
70-
let make = SyntaxFactory::new();
70+
let make = SyntaxFactory::with_mappings();
7171

7272
let mut exprs_to_unwrap = Vec::new();
7373
let tail_cb = &mut |e: &_| tail_cb_impl(&mut exprs_to_unwrap, e);

crates/ide-assists/src/handlers/wrap_return_type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ pub(crate) fn wrap_return_type(acc: &mut Assists, ctx: &AssistContext<'_>) -> Op
7777
type_ref.syntax().text_range(),
7878
|builder| {
7979
let mut editor = builder.make_editor(&parent);
80-
let make = SyntaxFactory::new();
80+
let make = SyntaxFactory::with_mappings();
8181
let alias = wrapper_alias(ctx, &make, &core_wrapper, type_ref, kind.symbol());
8282
let new_return_ty = alias.unwrap_or_else(|| match kind {
8383
WrapperKind::Option => make.ty_option(type_ref.clone()),

crates/ide-completion/src/completions/pattern.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub(crate) fn complete_pattern(
4848

4949
// Suggest name only in let-stmt and fn param
5050
if pattern_ctx.should_suggest_name {
51-
let mut name_generator = suggest_name::NameGenerator::new();
51+
let mut name_generator = suggest_name::NameGenerator::default();
5252
if let Some(suggested) = ctx
5353
.expected_type
5454
.as_ref()

crates/ide-db/src/prime_caches/topologic_sort.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,20 @@ pub(crate) struct TopologicSortIterBuilder<T> {
77
nodes: FxHashMap<T, Entry<T>>,
88
}
99

10-
impl<T> TopologicSortIterBuilder<T>
10+
// this implementation has different bounds on T than would be implied by #[derive(Default)]
11+
impl<T> Default for TopologicSortIterBuilder<T>
1112
where
1213
T: Copy + Eq + PartialEq + Hash,
1314
{
14-
fn new() -> Self {
15+
fn default() -> Self {
1516
Self { nodes: Default::default() }
1617
}
18+
}
1719

20+
impl<T> TopologicSortIterBuilder<T>
21+
where
22+
T: Copy + Eq + PartialEq + Hash,
23+
{
1824
fn get_or_create_entry(&mut self, item: T) -> &mut Entry<T> {
1925
self.nodes.entry(item).or_default()
2026
}
@@ -54,7 +60,7 @@ where
5460
T: Copy + Eq + PartialEq + Hash,
5561
{
5662
pub(crate) fn builder() -> TopologicSortIterBuilder<T> {
57-
TopologicSortIterBuilder::new()
63+
TopologicSortIterBuilder::default()
5864
}
5965

6066
pub(crate) fn pending(&self) -> usize {

crates/ide-db/src/source_change.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ impl SourceChangeBuilder {
469469
}
470470

471471
fn add_snippet_annotation(&mut self, kind: AnnotationSnippet) -> SyntaxAnnotation {
472-
let annotation = SyntaxAnnotation::new();
472+
let annotation = SyntaxAnnotation::default();
473473
self.snippet_annotations.push((kind, annotation));
474474
self.source_change.is_snippet = true;
475475
annotation

crates/ide-db/src/syntax_helpers/suggest_name.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,21 +96,16 @@ pub struct NameGenerator {
9696
}
9797

9898
impl NameGenerator {
99-
/// Create a new empty generator
100-
pub fn new() -> Self {
101-
Self { pool: FxHashMap::default() }
102-
}
103-
10499
/// Create a new generator with existing names. When suggesting a name, it will
105100
/// avoid conflicts with existing names.
106101
pub fn new_with_names<'a>(existing_names: impl Iterator<Item = &'a str>) -> Self {
107-
let mut generator = Self::new();
102+
let mut generator = Self::default();
108103
existing_names.for_each(|name| generator.insert(name));
109104
generator
110105
}
111106

112107
pub fn new_from_scope_locals(scope: Option<SemanticsScope<'_>>) -> Self {
113-
let mut generator = Self::new();
108+
let mut generator = Self::default();
114109
if let Some(scope) = scope {
115110
scope.process_all_names(&mut |name, scope| {
116111
if let hir::ScopeDef::Local(_) = scope {
@@ -471,7 +466,7 @@ mod tests {
471466
frange.range,
472467
"selection is not an expression(yet contained in one)"
473468
);
474-
let name = NameGenerator::new().for_variable(&expr, &sema);
469+
let name = NameGenerator::default().for_variable(&expr, &sema);
475470
assert_eq!(&name, expected);
476471
}
477472

@@ -1118,7 +1113,7 @@ fn main() {
11181113

11191114
#[test]
11201115
fn conflicts_with_existing_names() {
1121-
let mut generator = NameGenerator::new();
1116+
let mut generator = NameGenerator::default();
11221117
assert_eq!(generator.suggest_name("a"), "a");
11231118
assert_eq!(generator.suggest_name("a"), "a1");
11241119
assert_eq!(generator.suggest_name("a"), "a2");

crates/ide-diagnostics/src/handlers/type_mismatch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ fn remove_unnecessary_wrapper(
231231
.and_then(Either::<ast::ReturnExpr, ast::StmtList>::cast)?;
232232

233233
editor = builder.make_editor(parent.syntax());
234-
let make = SyntaxFactory::new();
234+
let make = SyntaxFactory::with_mappings();
235235

236236
match parent {
237237
Either::Left(ret_expr) => {

crates/ide/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ impl Analysis {
238238
file_set.insert(file_id, VfsPath::new_virtual_path("/main.rs".to_owned()));
239239
let source_root = SourceRoot::new_local(file_set);
240240

241-
let mut change = ChangeWithProcMacros::new();
241+
let mut change = ChangeWithProcMacros::default();
242242
change.set_roots(vec![source_root]);
243243
let mut crate_graph = CrateGraphBuilder::default();
244244
// FIXME: cfg options

crates/intern/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,10 @@ pub struct InternStorage<T: ?Sized> {
177177
map: OnceLock<InternMap<T>>,
178178
}
179179

180-
#[allow(clippy::new_without_default)] // this a const fn, so it can't be default
180+
#[allow(
181+
clippy::new_without_default,
182+
reason = "this a const fn, so it can't be default yet. See <https://github.com/rust-lang/rust/issues/63065>"
183+
)]
181184
impl<T: ?Sized> InternStorage<T> {
182185
pub const fn new() -> Self {
183186
Self { map: OnceLock::new() }

crates/load-cargo/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ fn load_crate_graph(
426426
) -> RootDatabase {
427427
let lru_cap = std::env::var("RA_LRU_CAP").ok().and_then(|it| it.parse::<u16>().ok());
428428
let mut db = RootDatabase::new(lru_cap);
429-
let mut analysis_change = ChangeWithProcMacros::new();
429+
let mut analysis_change = ChangeWithProcMacros::default();
430430

431431
db.enable_proc_attr_macros();
432432

0 commit comments

Comments
 (0)