From 7e0fa715329571da8d5d551b1815303c39cf51c1 Mon Sep 17 00:00:00 2001 From: Shotaro Yamada Date: Wed, 25 Sep 2019 10:30:33 +0900 Subject: Avoid intermediate allocation --- crates/ra_hir/src/ty.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index e6ecbe1ea..fae9c1e22 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -223,7 +223,7 @@ impl Substs { } pub fn prefix(&self, n: usize) -> Substs { - Substs(self.0.iter().cloned().take(n).collect::>().into()) + Substs(self.0[..std::cmp::min(self.0.len(), n)].into()) } pub fn walk(&self, f: &mut impl FnMut(&Ty)) { -- cgit v1.2.3 From 9c45a9e58632966984a79e18aa5221efb65e0ead Mon Sep 17 00:00:00 2001 From: Shotaro Yamada Date: Wed, 25 Sep 2019 10:32:01 +0900 Subject: Remove redundant clone() --- crates/ra_assists/src/merge_match_arms.rs | 2 +- crates/ra_cli/src/main.rs | 2 +- crates/ra_hir/src/adt.rs | 11 +++-------- crates/ra_hir/src/code_model.rs | 2 +- crates/ra_hir/src/from_source.rs | 8 ++++---- crates/ra_hir/src/nameres/collector.rs | 2 +- crates/ra_hir/src/nameres/raw.rs | 5 +---- crates/ra_hir/src/source_binder.rs | 2 +- crates/ra_hir/src/ty/infer.rs | 4 ++-- crates/ra_hir/src/ty/method_resolution.rs | 2 +- crates/ra_ide_api/src/completion/completion_context.rs | 2 +- crates/ra_ide_api/src/completion/presentation.rs | 2 +- crates/ra_ide_api/src/diagnostics.rs | 2 +- crates/ra_ide_api/src/display/navigation_target.rs | 4 ++-- 14 files changed, 21 insertions(+), 29 deletions(-) diff --git a/crates/ra_assists/src/merge_match_arms.rs b/crates/ra_assists/src/merge_match_arms.rs index 225a48d3a..3b6a99895 100644 --- a/crates/ra_assists/src/merge_match_arms.rs +++ b/crates/ra_assists/src/merge_match_arms.rs @@ -8,7 +8,7 @@ pub(crate) fn merge_match_arms(mut ctx: AssistCtx) -> Option Result<()> { (true, true) => Err("Invalid flags: -q conflicts with -v")?, }; let memory_usage = matches.contains("--memory-usage"); - let only = matches.value_from_str(["-o", "--only"])?.map(|v: String| v.to_owned()); + let only: Option = matches.value_from_str(["-o", "--only"])?; let path = { let mut trailing = matches.free()?; if trailing.len() != 1 { diff --git a/crates/ra_hir/src/adt.rs b/crates/ra_hir/src/adt.rs index 56f2b7aa3..fbb4ff4d8 100644 --- a/crates/ra_hir/src/adt.rs +++ b/crates/ra_hir/src/adt.rs @@ -56,8 +56,7 @@ impl EnumVariant { .zip(db.enum_data(self.parent).variants.iter()) .find(|(_syntax, (id, _))| *id == self.id) .unwrap() - .0 - .to_owned(); + .0; Source { file_id: src.file_id, ast } } pub(crate) fn variant_data(self, db: &impl DefDatabase) -> Arc { @@ -203,12 +202,8 @@ impl StructField { }; let field_sources = match struct_kind { - ast::StructKind::Tuple(fl) => { - fl.fields().map(|it| FieldSource::Pos(it.to_owned())).collect() - } - ast::StructKind::Named(fl) => { - fl.fields().map(|it| FieldSource::Named(it.to_owned())).collect() - } + ast::StructKind::Tuple(fl) => fl.fields().map(|it| FieldSource::Pos(it)).collect(), + ast::StructKind::Named(fl) => fl.fields().map(|it| FieldSource::Named(it)).collect(), ast::StructKind::Unit => Vec::new(), }; let ast = field_sources diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 99c247a0b..9fecba63d 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -161,7 +161,7 @@ impl ModuleSource { ) -> ModuleSource { match (file_id, decl_id) { (Some(file_id), _) => { - let source_file = db.parse(file_id).tree().to_owned(); + let source_file = db.parse(file_id).tree(); ModuleSource::SourceFile(source_file) } (None, Some(item_id)) => { diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs index f3194595f..7b6d9b240 100644 --- a/crates/ra_hir/src/from_source.rs +++ b/crates/ra_hir/src/from_source.rs @@ -137,7 +137,7 @@ impl ModuleSource { match &find_node_at_offset::(parse.tree().syntax(), position.offset) { Some(m) if !m.has_semi() => ModuleSource::Module(m.clone()), _ => { - let source_file = parse.tree().to_owned(); + let source_file = parse.tree(); ModuleSource::SourceFile(source_file) } } @@ -149,15 +149,15 @@ impl ModuleSource { child: &SyntaxNode, ) -> ModuleSource { if let Some(m) = child.ancestors().filter_map(ast::Module::cast).find(|it| !it.has_semi()) { - ModuleSource::Module(m.clone()) + ModuleSource::Module(m) } else { - let source_file = db.parse(file_id).tree().to_owned(); + let source_file = db.parse(file_id).tree(); ModuleSource::SourceFile(source_file) } } pub fn from_file_id(db: &(impl DefDatabase + AstDatabase), file_id: FileId) -> ModuleSource { - let source_file = db.parse(file_id).tree().to_owned(); + let source_file = db.parse(file_id).tree(); ModuleSource::SourceFile(source_file) } } diff --git a/crates/ra_hir/src/nameres/collector.rs b/crates/ra_hir/src/nameres/collector.rs index 6b253ac40..ef7dc6ebe 100644 --- a/crates/ra_hir/src/nameres/collector.rs +++ b/crates/ra_hir/src/nameres/collector.rs @@ -166,7 +166,7 @@ where // In Rust, `#[macro_export]` macros are unconditionally visible at the // crate root, even if the parent modules is **not** visible. if export { - self.update(self.def_map.root, None, &[(name.clone(), Resolution::from_macro(macro_))]); + self.update(self.def_map.root, None, &[(name, Resolution::from_macro(macro_))]); } } diff --git a/crates/ra_hir/src/nameres/raw.rs b/crates/ra_hir/src/nameres/raw.rs index 8bf883ac2..29aaddbf1 100644 --- a/crates/ra_hir/src/nameres/raw.rs +++ b/crates/ra_hir/src/nameres/raw.rs @@ -36,10 +36,7 @@ type ImportSource = Either; impl ImportSourcePtr { fn to_node(self, file: &SourceFile) -> ImportSource { - self.map( - |ptr| ptr.to_node(file.syntax()).to_owned(), - |ptr| ptr.to_node(file.syntax()).to_owned(), - ) + self.map(|ptr| ptr.to_node(file.syntax()), |ptr| ptr.to_node(file.syntax())) } } diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 296acc364..bd4be8430 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -73,7 +73,7 @@ fn def_with_body_from_child_node( if let Some(def) = ast::ConstDef::cast(node.clone()) { return Some(Const { id: ctx.to_def(&def) }.into()); } - if let Some(def) = ast::StaticDef::cast(node.clone()) { + if let Some(def) = ast::StaticDef::cast(node) { return Some(Static { id: ctx.to_def(&def) }.into()); } None diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index 378d2f829..76b4b6faa 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs @@ -436,7 +436,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { fn normalize_projection_ty(&mut self, proj_ty: ProjectionTy) -> Ty { let var = self.new_type_var(); - let predicate = ProjectionPredicate { projection_ty: proj_ty.clone(), ty: var.clone() }; + let predicate = ProjectionPredicate { projection_ty: proj_ty, ty: var.clone() }; let obligation = Obligation::Projection(predicate); self.obligations.push(obligation); var @@ -953,7 +953,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { arm_tys.push(self.infer_expr_inner(arm.expr, &expected)); } - let lub_ty = calculate_least_upper_bound(expected.ty.clone(), &arm_tys); + let lub_ty = calculate_least_upper_bound(expected.ty, &arm_tys); for arm_ty in &arm_tys { self.coerce(arm_ty, &lub_ty); diff --git a/crates/ra_hir/src/ty/method_resolution.rs b/crates/ra_hir/src/ty/method_resolution.rs index 8b46b11a9..a967d8a7f 100644 --- a/crates/ra_hir/src/ty/method_resolution.rs +++ b/crates/ra_hir/src/ty/method_resolution.rs @@ -290,7 +290,7 @@ pub(crate) fn implements_trait( return true; } let env = lower::trait_env(db, resolver); - let goal = generic_implements_goal(db, env.clone(), trait_, ty.clone()); + let goal = generic_implements_goal(db, env, trait_, ty.clone()); let solution = db.trait_solve(krate, goal); solution.is_some() diff --git a/crates/ra_ide_api/src/completion/completion_context.rs b/crates/ra_ide_api/src/completion/completion_context.rs index 59bd3689b..57542152f 100644 --- a/crates/ra_ide_api/src/completion/completion_context.rs +++ b/crates/ra_ide_api/src/completion/completion_context.rs @@ -94,7 +94,7 @@ impl<'a> CompletionContext<'a> { // actual completion. let file = { let edit = AtomTextEdit::insert(offset, "intellijRulezz".to_string()); - original_parse.reparse(&edit).tree().to_owned() + original_parse.reparse(&edit).tree() }; // First, let's try to complete a reference to some declaration. diff --git a/crates/ra_ide_api/src/completion/presentation.rs b/crates/ra_ide_api/src/completion/presentation.rs index ad414412b..b8aa433c1 100644 --- a/crates/ra_ide_api/src/completion/presentation.rs +++ b/crates/ra_ide_api/src/completion/presentation.rs @@ -76,7 +76,7 @@ impl Completions { None, ), ScopeDef::MacroDef(mac) => { - self.add_macro(ctx, Some(local_name.clone()), *mac); + self.add_macro(ctx, Some(local_name), *mac); return; } ScopeDef::Unknown => { diff --git a/crates/ra_ide_api/src/diagnostics.rs b/crates/ra_ide_api/src/diagnostics.rs index 1ae152e5b..93e1e7c2d 100644 --- a/crates/ra_ide_api/src/diagnostics.rs +++ b/crates/ra_ide_api/src/diagnostics.rs @@ -86,7 +86,7 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec fix: Some(fix), }) }); - let source_file = db.parse(file_id).tree().to_owned(); + let source_file = db.parse(file_id).tree(); let src = hir::Source { file_id: file_id.into(), ast: hir::ModuleSource::SourceFile(source_file) }; if let Some(m) = hir::Module::from_definition(db, src) { diff --git a/crates/ra_ide_api/src/display/navigation_target.rs b/crates/ra_ide_api/src/display/navigation_target.rs index 11f73ccfd..d3e774bd0 100644 --- a/crates/ra_ide_api/src/display/navigation_target.rs +++ b/crates/ra_ide_api/src/display/navigation_target.rs @@ -304,7 +304,7 @@ impl NavigationTarget { pub(crate) fn docs_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option { let parse = db.parse(symbol.file_id); - let node = symbol.ptr.to_node(parse.tree().syntax()).to_owned(); + let node = symbol.ptr.to_node(parse.tree().syntax()); visitor() .visit(|it: ast::FnDef| it.doc_comment_text()) @@ -326,7 +326,7 @@ pub(crate) fn docs_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option /// e.g. `struct Name`, `enum Name`, `fn Name` pub(crate) fn description_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option { let parse = db.parse(symbol.file_id); - let node = symbol.ptr.to_node(parse.tree().syntax()).to_owned(); + let node = symbol.ptr.to_node(parse.tree().syntax()); visitor() .visit(|node: ast::FnDef| node.short_label()) -- cgit v1.2.3