Skip to content

Commit

Permalink
Auto merge of #16294 - matthiaskrgr:less_alloc, r=Veykril
Browse files Browse the repository at this point in the history
minor: some minor clippy::perf fixes

can be read commit by commit if you want 🤷
  • Loading branch information
bors committed Jan 7, 2024
2 parents f595e60 + 3fb2cd2 commit 1c5fa44
Show file tree
Hide file tree
Showing 34 changed files with 62 additions and 73 deletions.
2 changes: 1 addition & 1 deletion crates/hir-def/src/body/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1610,7 +1610,7 @@ impl ExprCollector<'_> {
|name| self.alloc_expr_desugared(Expr::Path(Path::from(name))),
|name, span| {
if let Some(span) = span {
mappings.push((span, name.clone()))
mappings.push((span, name))
}
},
),
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-def/src/nameres/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1397,7 +1397,7 @@ impl DefCollector<'_> {
always!(krate == loc.def.krate);
DefDiagnostic::unresolved_proc_macro(module_id, loc.kind.clone(), loc.def.krate)
}
_ => DefDiagnostic::macro_error(module_id, loc.kind.clone(), err.to_string()),
_ => DefDiagnostic::macro_error(module_id, loc.kind, err.to_string()),
};

self.def_map.diagnostics.push(diag);
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-ty/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ fn render_const_scalar(
}
hir_def::AdtId::EnumId(e) => {
let Some((var_id, var_layout)) =
detect_variant_from_bytes(&layout, f.db, trait_env.clone(), b, e)
detect_variant_from_bytes(&layout, f.db, trait_env, b, e)
else {
return f.write_str("<failed-to-detect-variant>");
};
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-ty/src/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ impl<'a> InferenceContext<'a> {
result.tuple_field_access_types = tuple_field_accesses_rev
.into_iter()
.enumerate()
.map(|(idx, subst)| (TupleId(idx as u32), table.resolve_completely(subst.clone())))
.map(|(idx, subst)| (TupleId(idx as u32), table.resolve_completely(subst)))
.collect();
result
}
Expand Down
6 changes: 3 additions & 3 deletions crates/hir-ty/src/infer/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ impl HirPlace {
ctx.owner.module(ctx.db.upcast()).krate(),
);
}
ty.clone()
ty
}

fn capture_kind_of_truncated_place(
Expand Down Expand Up @@ -245,7 +245,7 @@ pub(crate) struct CapturedItemWithoutTy {

impl CapturedItemWithoutTy {
fn with_ty(self, ctx: &mut InferenceContext<'_>) -> CapturedItem {
let ty = self.place.ty(ctx).clone();
let ty = self.place.ty(ctx);
let ty = match &self.kind {
CaptureKind::ByValue => ty,
CaptureKind::ByRef(bk) => {
Expand Down Expand Up @@ -396,7 +396,7 @@ impl InferenceContext<'_> {

fn consume_place(&mut self, place: HirPlace, span: MirSpan) {
if self.is_upvar(&place) {
let ty = place.ty(self).clone();
let ty = place.ty(self);
let kind = if self.is_ty_copy(ty) {
CaptureKind::ByRef(BorrowKind::Shared)
} else {
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-ty/src/infer/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,7 @@ impl InferenceContext<'_> {
.push(callee_ty.clone())
.push(TyBuilder::tuple_with(params.iter().cloned()))
.build();
self.write_method_resolution(tgt_expr, func, subst.clone());
self.write_method_resolution(tgt_expr, func, subst);
}
}

Expand Down
3 changes: 1 addition & 2 deletions crates/hir-ty/src/infer/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,6 @@ impl InferenceContext<'_> {
};
let mut expectations_iter = expectations
.iter()
.cloned()
.map(|a| a.assert_ty_ref(Interner).clone())
.chain(repeat_with(|| self.table.new_type_var()));

Expand Down Expand Up @@ -336,7 +335,7 @@ impl InferenceContext<'_> {
&Pat::Lit(expr) => {
// Don't emit type mismatches again, the expression lowering already did that.
let ty = self.infer_lit_pat(expr, &expected);
self.write_pat_ty(pat, ty.clone());
self.write_pat_ty(pat, ty);
return self.pat_ty_after_adjustment(pat);
}
Pat::Box { inner } => match self.resolve_boxed_box() {
Expand Down
16 changes: 8 additions & 8 deletions crates/hir-ty/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ fn layout_of_simd_ty(
};

// Compute the ABI of the element type:
let e_ly = db.layout_of_ty(e_ty, env.clone())?;
let e_ly = db.layout_of_ty(e_ty, env)?;
let Abi::Scalar(e_abi) = e_ly.abi else {
return Err(LayoutError::Unknown);
};
Expand Down Expand Up @@ -204,17 +204,17 @@ pub fn layout_of_ty_query(
};
let cx = LayoutCx { target: &target };
let dl = &*cx.current_data_layout();
let ty = normalize(db, trait_env.clone(), ty.clone());
let ty = normalize(db, trait_env.clone(), ty);
let result = match ty.kind(Interner) {
TyKind::Adt(AdtId(def), subst) => {
if let hir_def::AdtId::StructId(s) = def {
let data = db.struct_data(*s);
let repr = data.repr.unwrap_or_default();
if repr.simd() {
return layout_of_simd_ty(db, *s, subst, trait_env.clone(), &target);
return layout_of_simd_ty(db, *s, subst, trait_env, &target);
}
};
return db.layout_of_adt(*def, subst.clone(), trait_env.clone());
return db.layout_of_adt(*def, subst.clone(), trait_env);
}
TyKind::Scalar(s) => match s {
chalk_ir::Scalar::Bool => Layout::scalar(
Expand Down Expand Up @@ -280,7 +280,7 @@ pub fn layout_of_ty_query(
}
TyKind::Array(element, count) => {
let count = try_const_usize(db, &count).ok_or(LayoutError::HasErrorConst)? as u64;
let element = db.layout_of_ty(element.clone(), trait_env.clone())?;
let element = db.layout_of_ty(element.clone(), trait_env)?;
let size = element.size.checked_mul(count, dl).ok_or(LayoutError::SizeOverflow)?;

let abi = if count != 0 && matches!(element.abi, Abi::Uninhabited) {
Expand All @@ -303,7 +303,7 @@ pub fn layout_of_ty_query(
}
}
TyKind::Slice(element) => {
let element = db.layout_of_ty(element.clone(), trait_env.clone())?;
let element = db.layout_of_ty(element.clone(), trait_env)?;
Layout {
variants: Variants::Single { index: struct_variant_idx() },
fields: FieldsShape::Array { stride: element.size, count: 0 },
Expand Down Expand Up @@ -345,7 +345,7 @@ pub fn layout_of_ty_query(
}))
.intern(Interner);
}
unsized_part = normalize(db, trait_env.clone(), unsized_part);
unsized_part = normalize(db, trait_env, unsized_part);
let metadata = match unsized_part.kind(Interner) {
TyKind::Slice(_) | TyKind::Str => {
scalar_unit(dl, Primitive::Int(dl.ptr_sized_integer(), false))
Expand Down Expand Up @@ -384,7 +384,7 @@ pub fn layout_of_ty_query(
match impl_trait_id {
crate::ImplTraitId::ReturnTypeImplTrait(func, idx) => {
let infer = db.infer(func.into());
return db.layout_of_ty(infer.type_of_rpit[idx].clone(), trait_env.clone());
return db.layout_of_ty(infer.type_of_rpit[idx].clone(), trait_env);
}
crate::ImplTraitId::AsyncBlockTypeImplTrait(_, _) => {
return Err(LayoutError::NotImplemented)
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-ty/src/method_resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1350,7 +1350,7 @@ pub(crate) fn resolve_indexing_op(
ty: Canonical<Ty>,
index_trait: TraitId,
) -> Option<ReceiverAdjustments> {
let mut table = InferenceTable::new(db, env.clone());
let mut table = InferenceTable::new(db, env);
let ty = table.instantiate_canonical(ty);
let deref_chain = autoderef_method_receiver(&mut table, ty);
for (ty, adj) in deref_chain {
Expand Down
4 changes: 2 additions & 2 deletions crates/hir-ty/src/mir/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ impl MirEvalError {
write!(
f,
"Layout for type `{}` is not available due {err:?}",
ty.display(db).with_closure_style(ClosureStyle::ClosureWithId).to_string()
ty.display(db).with_closure_style(ClosureStyle::ClosureWithId)
)?;
}
MirEvalError::MirLowerError(func, err) => {
Expand Down Expand Up @@ -1533,7 +1533,7 @@ impl Evaluator<'_> {
}
},
TyKind::Dyn(_) => {
let vtable = self.vtable_map.id(current_ty.clone());
let vtable = self.vtable_map.id(current_ty);
let mut r = Vec::with_capacity(16);
let addr = addr.get(self)?;
r.extend(addr.iter().copied());
Expand Down
6 changes: 3 additions & 3 deletions crates/hir-ty/src/mir/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ impl MirLowerError {
)?;
writeln!(f, "Provided args: [")?;
for g in subst.iter(Interner) {
write!(f, " {},", g.display(db).to_string())?;
write!(f, " {},", g.display(db))?;
}
writeln!(f, "]")?;
}
Expand Down Expand Up @@ -2070,8 +2070,8 @@ pub fn mir_body_for_closure_query(
prev_projs
.lookup(&store)
.iter()
.cloned()
.skip(it.0.place.projections.len()),
.skip(it.0.place.projections.len())
.cloned(),
);
p.projection = store.intern(next_projs.into());
}
Expand Down
7 changes: 3 additions & 4 deletions crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,7 @@ impl Field {

pub fn layout(&self, db: &dyn HirDatabase) -> Result<Layout, LayoutError> {
db.layout_of_ty(
self.ty(db).ty.clone(),
self.ty(db).ty,
db.trait_environment(match hir_def::VariantId::from(self.parent) {
hir_def::VariantId::EnumVariantId(id) => GenericDefId::EnumVariantId(id),
hir_def::VariantId::StructId(id) => GenericDefId::AdtId(id.into()),
Expand Down Expand Up @@ -1854,7 +1854,7 @@ impl DefWithBody {
let local = Local { parent: self.into(), binding_id };
match (need_mut, local.is_mut(db)) {
(mir::MutabilityReason::Unused, _) => {
let should_ignore = matches!(body[binding_id].name.as_str(), Some(it) if it.starts_with("_"));
let should_ignore = matches!(body[binding_id].name.as_str(), Some(it) if it.starts_with('_'));
if !should_ignore {
acc.push(UnusedVariable { local }.into())
}
Expand All @@ -1879,7 +1879,7 @@ impl DefWithBody {
}
(mir::MutabilityReason::Not, true) => {
if !infer.mutated_bindings_in_closure.contains(&binding_id) {
let should_ignore = matches!(body[binding_id].name.as_str(), Some(it) if it.starts_with("_"));
let should_ignore = matches!(body[binding_id].name.as_str(), Some(it) if it.starts_with('_'));
if !should_ignore {
acc.push(UnusedMut { local }.into())
}
Expand Down Expand Up @@ -3673,7 +3673,6 @@ impl Closure {
let (captures, _) = infer.closure_info(&self.id);
captures
.iter()
.cloned()
.map(|capture| Type {
env: db.trait_environment_for_body(owner),
ty: capture.ty(&self.subst),
Expand Down
2 changes: 1 addition & 1 deletion crates/hir/src/semantics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ impl<'db> SemanticsImpl<'db> {
if let Some(original_string) = ast::String::cast(original_token.clone()) {
if let Some(quote) = original_string.open_quote_text_range() {
return self
.descend_into_macros(DescendPreference::SameText, original_token.clone())
.descend_into_macros(DescendPreference::SameText, original_token)
.into_iter()
.find_map(|token| {
self.resolve_offset_in_format_args(
Expand Down
4 changes: 2 additions & 2 deletions crates/ide-assists/src/handlers/bool_to_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ fn replace_usages(

// add imports across modules where needed
if let Some((import_scope, path)) = import_data {
let scope = match import_scope.clone() {
let scope = match import_scope {
ImportScope::File(it) => ImportScope::File(edit.make_mut(it)),
ImportScope::Module(it) => ImportScope::Module(edit.make_mut(it)),
ImportScope::Block(it) => ImportScope::Block(edit.make_mut(it)),
Expand Down Expand Up @@ -329,7 +329,7 @@ fn augment_references_with_imports(
references
.into_iter()
.filter_map(|FileReference { range, name, .. }| {
let name = name.clone().into_name_like()?;
let name = name.into_name_like()?;
ctx.sema.scope(name.syntax()).map(|scope| (range, name, scope.module()))
})
.map(|(range, name, ref_module)| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ pub(crate) fn convert_nested_function_to_closure(
target,
|edit| {
let params = &param_list.syntax().text().to_string();
let params = params.strip_prefix("(").unwrap_or(params);
let params = params.strip_suffix(")").unwrap_or(params);
let params = params.strip_prefix('(').unwrap_or(params);
let params = params.strip_suffix(')').unwrap_or(params);

let mut body = body.to_string();
if !has_semicolon(&function) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ fn augment_references_with_imports(
ctx.sema.scope(name.syntax()).map(|scope| (name, scope.module()))
})
.map(|(name, ref_module)| {
let new_name = edit.make_mut(name.clone());
let new_name = edit.make_mut(name);

// if the referenced module is not the same as the target one and has not been seen before, add an import
let import_data = if ref_module.nearest_non_block_module(ctx.db()) != *target_module
Expand Down
2 changes: 1 addition & 1 deletion crates/ide-assists/src/handlers/extract_variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pub(crate) fn extract_variable(acc: &mut Assists, ctx: &AssistContext<'_>) -> Op
let insert_place = edit.make_syntax_mut(place);

// Adjust ws to insert depending on if this is all inline or on separate lines
let trailing_ws = if prev_ws.is_some_and(|it| it.text().starts_with("\n")) {
let trailing_ws = if prev_ws.is_some_and(|it| it.text().starts_with('\n')) {
format!("\n{indent_to}")
} else {
format!(" ")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext<'
None => {
let name = &strukt_name.to_string();
let params = strukt.generic_param_list();
let ty_params = params.clone();
let ty_params = params;
let where_clause = strukt.where_clause();

let impl_def = make::impl_(
Expand Down
16 changes: 5 additions & 11 deletions crates/ide-assists/src/handlers/generate_delegate_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,8 @@ fn generate_impl(
.clone_for_update();

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

let delegate_assoc_items = delegate.get_or_create_assoc_item_list();
match bound_def.assoc_item_list() {
Expand Down Expand Up @@ -373,11 +370,8 @@ fn generate_impl(
.clone_for_update();

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

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

fn qualified_path(qual_path_ty: ast::Path, path_expr_seg: ast::Path) -> ast::Path {
make::path_from_text(&format!("{}::{}", qual_path_ty.to_string(), path_expr_seg.to_string()))
make::path_from_text(&format!("{}::{}", qual_path_ty, path_expr_seg))
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion crates/ide-assists/src/handlers/generate_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ fn get_fn_target(
}
None => next_space_for_fn_after_call_site(ast::CallableExpr::Call(call))?,
};
Some((target.clone(), file))
Some((target, file))
}

fn get_method_target(
Expand Down
4 changes: 2 additions & 2 deletions crates/ide-assists/src/handlers/generate_mut_trait_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub(crate) fn generate_mut_trait_impl(acc: &mut Assists, ctx: &AssistContext<'_>
let impl_def = ctx.find_node_at_offset::<ast::Impl>()?.clone_for_update();

let trait_ = impl_def.trait_()?;
if let ast::Type::PathType(trait_path) = trait_.clone() {
if let ast::Type::PathType(trait_path) = trait_ {
let trait_type = ctx.sema.resolve_trait(&trait_path.path()?)?;
let scope = ctx.sema.scope(trait_path.syntax())?;
if trait_type != FamousDefs(&ctx.sema, scope.krate()).core_convert_Index()? {
Expand Down Expand Up @@ -105,7 +105,7 @@ pub(crate) fn generate_mut_trait_impl(acc: &mut Assists, ctx: &AssistContext<'_>
"Generate `IndexMut` impl from this `Index` trait",
target,
|edit| {
edit.insert(target.start(), format!("$0{}\n\n", impl_def.to_string()));
edit.insert(target.start(), format!("$0{}\n\n", impl_def));
},
)
}
Expand Down
10 changes: 3 additions & 7 deletions crates/ide-assists/src/handlers/generate_trait_from_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ pub(crate) fn generate_trait_from_impl(acc: &mut Assists, ctx: &AssistContext<'_
builder.replace_snippet(
snippet_cap,
impl_name.syntax().text_range(),
format!("${{0:TraitName}}{} for {}", arg_list, impl_name.to_string()),
format!("${{0:TraitName}}{} for {}", arg_list, impl_name),
);

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

// Insert trait before TraitImpl
builder.insert(
impl_ast.syntax().text_range().start(),
format!(
"{}\n\n{}",
trait_ast.to_string(),
IndentLevel::from_node(impl_ast.syntax())
),
format!("{}\n\n{}", trait_ast, IndentLevel::from_node(impl_ast.syntax())),
);
}

Expand Down
Loading

0 comments on commit 1c5fa44

Please sign in to comment.