Skip to content

Commit 1c5fa44

Browse files
committed
Auto merge of rust-lang#16294 - matthiaskrgr:less_alloc, r=Veykril
minor: some minor clippy::perf fixes can be read commit by commit if you want 🤷
2 parents f595e60 + 3fb2cd2 commit 1c5fa44

34 files changed

+62
-73
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1610,7 +1610,7 @@ impl ExprCollector<'_> {
16101610
|name| self.alloc_expr_desugared(Expr::Path(Path::from(name))),
16111611
|name, span| {
16121612
if let Some(span) = span {
1613-
mappings.push((span, name.clone()))
1613+
mappings.push((span, name))
16141614
}
16151615
},
16161616
),

crates/hir-def/src/nameres/collector.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1397,7 +1397,7 @@ impl DefCollector<'_> {
13971397
always!(krate == loc.def.krate);
13981398
DefDiagnostic::unresolved_proc_macro(module_id, loc.kind.clone(), loc.def.krate)
13991399
}
1400-
_ => DefDiagnostic::macro_error(module_id, loc.kind.clone(), err.to_string()),
1400+
_ => DefDiagnostic::macro_error(module_id, loc.kind, err.to_string()),
14011401
};
14021402

14031403
self.def_map.diagnostics.push(diag);

crates/hir-ty/src/display.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ fn render_const_scalar(
609609
}
610610
hir_def::AdtId::EnumId(e) => {
611611
let Some((var_id, var_layout)) =
612-
detect_variant_from_bytes(&layout, f.db, trait_env.clone(), b, e)
612+
detect_variant_from_bytes(&layout, f.db, trait_env, b, e)
613613
else {
614614
return f.write_str("<failed-to-detect-variant>");
615615
};

crates/hir-ty/src/infer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ impl<'a> InferenceContext<'a> {
738738
result.tuple_field_access_types = tuple_field_accesses_rev
739739
.into_iter()
740740
.enumerate()
741-
.map(|(idx, subst)| (TupleId(idx as u32), table.resolve_completely(subst.clone())))
741+
.map(|(idx, subst)| (TupleId(idx as u32), table.resolve_completely(subst)))
742742
.collect();
743743
result
744744
}

crates/hir-ty/src/infer/closure.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ impl HirPlace {
130130
ctx.owner.module(ctx.db.upcast()).krate(),
131131
);
132132
}
133-
ty.clone()
133+
ty
134134
}
135135

136136
fn capture_kind_of_truncated_place(
@@ -245,7 +245,7 @@ pub(crate) struct CapturedItemWithoutTy {
245245

246246
impl CapturedItemWithoutTy {
247247
fn with_ty(self, ctx: &mut InferenceContext<'_>) -> CapturedItem {
248-
let ty = self.place.ty(ctx).clone();
248+
let ty = self.place.ty(ctx);
249249
let ty = match &self.kind {
250250
CaptureKind::ByValue => ty,
251251
CaptureKind::ByRef(bk) => {
@@ -396,7 +396,7 @@ impl InferenceContext<'_> {
396396

397397
fn consume_place(&mut self, place: HirPlace, span: MirSpan) {
398398
if self.is_upvar(&place) {
399-
let ty = place.ty(self).clone();
399+
let ty = place.ty(self);
400400
let kind = if self.is_ty_copy(ty) {
401401
CaptureKind::ByRef(BorrowKind::Shared)
402402
} else {

crates/hir-ty/src/infer/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -978,7 +978,7 @@ impl InferenceContext<'_> {
978978
.push(callee_ty.clone())
979979
.push(TyBuilder::tuple_with(params.iter().cloned()))
980980
.build();
981-
self.write_method_resolution(tgt_expr, func, subst.clone());
981+
self.write_method_resolution(tgt_expr, func, subst);
982982
}
983983
}
984984

crates/hir-ty/src/infer/pat.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,6 @@ impl InferenceContext<'_> {
233233
};
234234
let mut expectations_iter = expectations
235235
.iter()
236-
.cloned()
237236
.map(|a| a.assert_ty_ref(Interner).clone())
238237
.chain(repeat_with(|| self.table.new_type_var()));
239238

@@ -336,7 +335,7 @@ impl InferenceContext<'_> {
336335
&Pat::Lit(expr) => {
337336
// Don't emit type mismatches again, the expression lowering already did that.
338337
let ty = self.infer_lit_pat(expr, &expected);
339-
self.write_pat_ty(pat, ty.clone());
338+
self.write_pat_ty(pat, ty);
340339
return self.pat_ty_after_adjustment(pat);
341340
}
342341
Pat::Box { inner } => match self.resolve_boxed_box() {

crates/hir-ty/src/layout.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ fn layout_of_simd_ty(
164164
};
165165

166166
// Compute the ABI of the element type:
167-
let e_ly = db.layout_of_ty(e_ty, env.clone())?;
167+
let e_ly = db.layout_of_ty(e_ty, env)?;
168168
let Abi::Scalar(e_abi) = e_ly.abi else {
169169
return Err(LayoutError::Unknown);
170170
};
@@ -204,17 +204,17 @@ pub fn layout_of_ty_query(
204204
};
205205
let cx = LayoutCx { target: &target };
206206
let dl = &*cx.current_data_layout();
207-
let ty = normalize(db, trait_env.clone(), ty.clone());
207+
let ty = normalize(db, trait_env.clone(), ty);
208208
let result = match ty.kind(Interner) {
209209
TyKind::Adt(AdtId(def), subst) => {
210210
if let hir_def::AdtId::StructId(s) = def {
211211
let data = db.struct_data(*s);
212212
let repr = data.repr.unwrap_or_default();
213213
if repr.simd() {
214-
return layout_of_simd_ty(db, *s, subst, trait_env.clone(), &target);
214+
return layout_of_simd_ty(db, *s, subst, trait_env, &target);
215215
}
216216
};
217-
return db.layout_of_adt(*def, subst.clone(), trait_env.clone());
217+
return db.layout_of_adt(*def, subst.clone(), trait_env);
218218
}
219219
TyKind::Scalar(s) => match s {
220220
chalk_ir::Scalar::Bool => Layout::scalar(
@@ -280,7 +280,7 @@ pub fn layout_of_ty_query(
280280
}
281281
TyKind::Array(element, count) => {
282282
let count = try_const_usize(db, &count).ok_or(LayoutError::HasErrorConst)? as u64;
283-
let element = db.layout_of_ty(element.clone(), trait_env.clone())?;
283+
let element = db.layout_of_ty(element.clone(), trait_env)?;
284284
let size = element.size.checked_mul(count, dl).ok_or(LayoutError::SizeOverflow)?;
285285

286286
let abi = if count != 0 && matches!(element.abi, Abi::Uninhabited) {
@@ -303,7 +303,7 @@ pub fn layout_of_ty_query(
303303
}
304304
}
305305
TyKind::Slice(element) => {
306-
let element = db.layout_of_ty(element.clone(), trait_env.clone())?;
306+
let element = db.layout_of_ty(element.clone(), trait_env)?;
307307
Layout {
308308
variants: Variants::Single { index: struct_variant_idx() },
309309
fields: FieldsShape::Array { stride: element.size, count: 0 },
@@ -345,7 +345,7 @@ pub fn layout_of_ty_query(
345345
}))
346346
.intern(Interner);
347347
}
348-
unsized_part = normalize(db, trait_env.clone(), unsized_part);
348+
unsized_part = normalize(db, trait_env, unsized_part);
349349
let metadata = match unsized_part.kind(Interner) {
350350
TyKind::Slice(_) | TyKind::Str => {
351351
scalar_unit(dl, Primitive::Int(dl.ptr_sized_integer(), false))
@@ -384,7 +384,7 @@ pub fn layout_of_ty_query(
384384
match impl_trait_id {
385385
crate::ImplTraitId::ReturnTypeImplTrait(func, idx) => {
386386
let infer = db.infer(func.into());
387-
return db.layout_of_ty(infer.type_of_rpit[idx].clone(), trait_env.clone());
387+
return db.layout_of_ty(infer.type_of_rpit[idx].clone(), trait_env);
388388
}
389389
crate::ImplTraitId::AsyncBlockTypeImplTrait(_, _) => {
390390
return Err(LayoutError::NotImplemented)

crates/hir-ty/src/method_resolution.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1350,7 +1350,7 @@ pub(crate) fn resolve_indexing_op(
13501350
ty: Canonical<Ty>,
13511351
index_trait: TraitId,
13521352
) -> Option<ReceiverAdjustments> {
1353-
let mut table = InferenceTable::new(db, env.clone());
1353+
let mut table = InferenceTable::new(db, env);
13541354
let ty = table.instantiate_canonical(ty);
13551355
let deref_chain = autoderef_method_receiver(&mut table, ty);
13561356
for (ty, adj) in deref_chain {

crates/hir-ty/src/mir/eval.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ impl MirEvalError {
386386
write!(
387387
f,
388388
"Layout for type `{}` is not available due {err:?}",
389-
ty.display(db).with_closure_style(ClosureStyle::ClosureWithId).to_string()
389+
ty.display(db).with_closure_style(ClosureStyle::ClosureWithId)
390390
)?;
391391
}
392392
MirEvalError::MirLowerError(func, err) => {
@@ -1533,7 +1533,7 @@ impl Evaluator<'_> {
15331533
}
15341534
},
15351535
TyKind::Dyn(_) => {
1536-
let vtable = self.vtable_map.id(current_ty.clone());
1536+
let vtable = self.vtable_map.id(current_ty);
15371537
let mut r = Vec::with_capacity(16);
15381538
let addr = addr.get(self)?;
15391539
r.extend(addr.iter().copied());

crates/hir-ty/src/mir/lower.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ impl MirLowerError {
177177
)?;
178178
writeln!(f, "Provided args: [")?;
179179
for g in subst.iter(Interner) {
180-
write!(f, " {},", g.display(db).to_string())?;
180+
write!(f, " {},", g.display(db))?;
181181
}
182182
writeln!(f, "]")?;
183183
}
@@ -2070,8 +2070,8 @@ pub fn mir_body_for_closure_query(
20702070
prev_projs
20712071
.lookup(&store)
20722072
.iter()
2073-
.cloned()
2074-
.skip(it.0.place.projections.len()),
2073+
.skip(it.0.place.projections.len())
2074+
.cloned(),
20752075
);
20762076
p.projection = store.intern(next_projs.into());
20772077
}

crates/hir/src/lib.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1093,7 +1093,7 @@ impl Field {
10931093

10941094
pub fn layout(&self, db: &dyn HirDatabase) -> Result<Layout, LayoutError> {
10951095
db.layout_of_ty(
1096-
self.ty(db).ty.clone(),
1096+
self.ty(db).ty,
10971097
db.trait_environment(match hir_def::VariantId::from(self.parent) {
10981098
hir_def::VariantId::EnumVariantId(id) => GenericDefId::EnumVariantId(id),
10991099
hir_def::VariantId::StructId(id) => GenericDefId::AdtId(id.into()),
@@ -1854,7 +1854,7 @@ impl DefWithBody {
18541854
let local = Local { parent: self.into(), binding_id };
18551855
match (need_mut, local.is_mut(db)) {
18561856
(mir::MutabilityReason::Unused, _) => {
1857-
let should_ignore = matches!(body[binding_id].name.as_str(), Some(it) if it.starts_with("_"));
1857+
let should_ignore = matches!(body[binding_id].name.as_str(), Some(it) if it.starts_with('_'));
18581858
if !should_ignore {
18591859
acc.push(UnusedVariable { local }.into())
18601860
}
@@ -1879,7 +1879,7 @@ impl DefWithBody {
18791879
}
18801880
(mir::MutabilityReason::Not, true) => {
18811881
if !infer.mutated_bindings_in_closure.contains(&binding_id) {
1882-
let should_ignore = matches!(body[binding_id].name.as_str(), Some(it) if it.starts_with("_"));
1882+
let should_ignore = matches!(body[binding_id].name.as_str(), Some(it) if it.starts_with('_'));
18831883
if !should_ignore {
18841884
acc.push(UnusedMut { local }.into())
18851885
}
@@ -3673,7 +3673,6 @@ impl Closure {
36733673
let (captures, _) = infer.closure_info(&self.id);
36743674
captures
36753675
.iter()
3676-
.cloned()
36773676
.map(|capture| Type {
36783677
env: db.trait_environment_for_body(owner),
36793678
ty: capture.ty(&self.subst),

crates/hir/src/semantics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ impl<'db> SemanticsImpl<'db> {
428428
if let Some(original_string) = ast::String::cast(original_token.clone()) {
429429
if let Some(quote) = original_string.open_quote_text_range() {
430430
return self
431-
.descend_into_macros(DescendPreference::SameText, original_token.clone())
431+
.descend_into_macros(DescendPreference::SameText, original_token)
432432
.into_iter()
433433
.find_map(|token| {
434434
self.resolve_offset_in_format_args(

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ fn replace_usages(
301301

302302
// add imports across modules where needed
303303
if let Some((import_scope, path)) = import_data {
304-
let scope = match import_scope.clone() {
304+
let scope = match import_scope {
305305
ImportScope::File(it) => ImportScope::File(edit.make_mut(it)),
306306
ImportScope::Module(it) => ImportScope::Module(edit.make_mut(it)),
307307
ImportScope::Block(it) => ImportScope::Block(edit.make_mut(it)),
@@ -329,7 +329,7 @@ fn augment_references_with_imports(
329329
references
330330
.into_iter()
331331
.filter_map(|FileReference { range, name, .. }| {
332-
let name = name.clone().into_name_like()?;
332+
let name = name.into_name_like()?;
333333
ctx.sema.scope(name.syntax()).map(|scope| (range, name, scope.module()))
334334
})
335335
.map(|(range, name, ref_module)| {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ pub(crate) fn convert_nested_function_to_closure(
4949
target,
5050
|edit| {
5151
let params = &param_list.syntax().text().to_string();
52-
let params = params.strip_prefix("(").unwrap_or(params);
53-
let params = params.strip_suffix(")").unwrap_or(params);
52+
let params = params.strip_prefix('(').unwrap_or(params);
53+
let params = params.strip_suffix(')').unwrap_or(params);
5454

5555
let mut body = body.to_string();
5656
if !has_semicolon(&function) {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ fn augment_references_with_imports(
190190
ctx.sema.scope(name.syntax()).map(|scope| (name, scope.module()))
191191
})
192192
.map(|(name, ref_module)| {
193-
let new_name = edit.make_mut(name.clone());
193+
let new_name = edit.make_mut(name);
194194

195195
// if the referenced module is not the same as the target one and has not been seen before, add an import
196196
let import_data = if ref_module.nearest_non_block_module(ctx.db()) != *target_module

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ pub(crate) fn extract_variable(acc: &mut Assists, ctx: &AssistContext<'_>) -> Op
112112
let insert_place = edit.make_syntax_mut(place);
113113

114114
// Adjust ws to insert depending on if this is all inline or on separate lines
115-
let trailing_ws = if prev_ws.is_some_and(|it| it.text().starts_with("\n")) {
115+
let trailing_ws = if prev_ws.is_some_and(|it| it.text().starts_with('\n')) {
116116
format!("\n{indent_to}")
117117
} else {
118118
format!(" ")

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext<'
147147
None => {
148148
let name = &strukt_name.to_string();
149149
let params = strukt.generic_param_list();
150-
let ty_params = params.clone();
150+
let ty_params = params;
151151
let where_clause = strukt.where_clause();
152152

153153
let impl_def = make::impl_(

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

+5-11
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,8 @@ fn generate_impl(
266266
.clone_for_update();
267267

268268
// Goto link : https://doc.rust-lang.org/reference/paths.html#qualified-paths
269-
let qualified_path_type = make::path_from_text(&format!(
270-
"<{} as {}>",
271-
field_ty.to_string(),
272-
delegate.trait_()?.to_string()
273-
));
269+
let qualified_path_type =
270+
make::path_from_text(&format!("<{} as {}>", field_ty, delegate.trait_()?));
274271

275272
let delegate_assoc_items = delegate.get_or_create_assoc_item_list();
276273
match bound_def.assoc_item_list() {
@@ -373,11 +370,8 @@ fn generate_impl(
373370
.clone_for_update();
374371

375372
// Goto link : https://doc.rust-lang.org/reference/paths.html#qualified-paths
376-
let qualified_path_type = make::path_from_text(&format!(
377-
"<{} as {}>",
378-
field_ty.to_string(),
379-
delegate.trait_()?.to_string()
380-
));
373+
let qualified_path_type =
374+
make::path_from_text(&format!("<{} as {}>", field_ty, delegate.trait_()?));
381375

382376
// 4) Transform associated items in delegte trait impl
383377
let delegate_assoc_items = delegate.get_or_create_assoc_item_list();
@@ -759,7 +753,7 @@ fn ty_assoc_item(item: syntax::ast::TypeAlias, qual_path_ty: Path) -> Option<Ass
759753
}
760754

761755
fn qualified_path(qual_path_ty: ast::Path, path_expr_seg: ast::Path) -> ast::Path {
762-
make::path_from_text(&format!("{}::{}", qual_path_ty.to_string(), path_expr_seg.to_string()))
756+
make::path_from_text(&format!("{}::{}", qual_path_ty, path_expr_seg))
763757
}
764758

765759
#[cfg(test)]

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ fn get_fn_target(
432432
}
433433
None => next_space_for_fn_after_call_site(ast::CallableExpr::Call(call))?,
434434
};
435-
Some((target.clone(), file))
435+
Some((target, file))
436436
}
437437

438438
fn get_method_target(

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub(crate) fn generate_mut_trait_impl(acc: &mut Assists, ctx: &AssistContext<'_>
4747
let impl_def = ctx.find_node_at_offset::<ast::Impl>()?.clone_for_update();
4848

4949
let trait_ = impl_def.trait_()?;
50-
if let ast::Type::PathType(trait_path) = trait_.clone() {
50+
if let ast::Type::PathType(trait_path) = trait_ {
5151
let trait_type = ctx.sema.resolve_trait(&trait_path.path()?)?;
5252
let scope = ctx.sema.scope(trait_path.syntax())?;
5353
if trait_type != FamousDefs(&ctx.sema, scope.krate()).core_convert_Index()? {
@@ -105,7 +105,7 @@ pub(crate) fn generate_mut_trait_impl(acc: &mut Assists, ctx: &AssistContext<'_>
105105
"Generate `IndexMut` impl from this `Index` trait",
106106
target,
107107
|edit| {
108-
edit.insert(target.start(), format!("$0{}\n\n", impl_def.to_string()));
108+
edit.insert(target.start(), format!("$0{}\n\n", impl_def));
109109
},
110110
)
111111
}

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

+3-7
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ pub(crate) fn generate_trait_from_impl(acc: &mut Assists, ctx: &AssistContext<'_
128128
builder.replace_snippet(
129129
snippet_cap,
130130
impl_name.syntax().text_range(),
131-
format!("${{0:TraitName}}{} for {}", arg_list, impl_name.to_string()),
131+
format!("${{0:TraitName}}{} for {}", arg_list, impl_name),
132132
);
133133

134134
// Insert trait before TraitImpl
@@ -144,17 +144,13 @@ pub(crate) fn generate_trait_from_impl(acc: &mut Assists, ctx: &AssistContext<'_
144144
} else {
145145
builder.replace(
146146
impl_name.syntax().text_range(),
147-
format!("NewTrait{} for {}", arg_list, impl_name.to_string()),
147+
format!("NewTrait{} for {}", arg_list, impl_name),
148148
);
149149

150150
// Insert trait before TraitImpl
151151
builder.insert(
152152
impl_ast.syntax().text_range().start(),
153-
format!(
154-
"{}\n\n{}",
155-
trait_ast.to_string(),
156-
IndentLevel::from_node(impl_ast.syntax())
157-
),
153+
format!("{}\n\n{}", trait_ast, IndentLevel::from_node(impl_ast.syntax())),
158154
);
159155
}
160156

0 commit comments

Comments
 (0)