From e3ce88f6f25901b1d33fbc5f931a9d8f90b9e2b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauren=C8=9Biu=20Nicola?= Date: Fri, 18 Jun 2021 14:40:51 +0300 Subject: [PATCH 1/2] Minor clippy perf fixes --- crates/hir_def/src/resolver.rs | 3 +-- crates/hir_expand/src/builtin_macro.rs | 4 ++-- crates/hir_ty/src/diagnostics/expr.rs | 2 +- crates/hir_ty/src/infer/expr.rs | 10 ++++++---- crates/hir_ty/src/infer/pat.rs | 2 +- crates/hir_ty/src/lower.rs | 2 +- crates/ide/src/goto_type_definition.rs | 2 +- crates/ide_assists/src/handlers/remove_dbg.rs | 4 ++-- crates/proc_macro_api/src/version.rs | 10 +++++----- crates/project_model/src/build_data.rs | 2 +- crates/test_utils/src/fixture.rs | 2 +- 11 files changed, 22 insertions(+), 21 deletions(-) diff --git a/crates/hir_def/src/resolver.rs b/crates/hir_def/src/resolver.rs index 47e56259f4f..49c5730877b 100644 --- a/crates/hir_def/src/resolver.rs +++ b/crates/hir_def/src/resolver.rs @@ -640,8 +640,7 @@ pub trait HasResolver: Copy { impl HasResolver for ModuleId { fn resolver(self, db: &dyn DefDatabase) -> Resolver { let mut def_map = self.def_map(db); - let mut modules = Vec::new(); - modules.push((def_map.clone(), self.local_id)); + let mut modules = vec![(def_map.clone(), self.local_id)]; while let Some(parent) = def_map.parent() { def_map = parent.def_map(db); modules.push((def_map.clone(), parent.local_id)); diff --git a/crates/hir_expand/src/builtin_macro.rs b/crates/hir_expand/src/builtin_macro.rs index f24d1d919cf..4c83a2efe37 100644 --- a/crates/hir_expand/src/builtin_macro.rs +++ b/crates/hir_expand/src/builtin_macro.rs @@ -202,7 +202,7 @@ fn assert_expand( let arg_tts = args.into_iter().flat_map(|arg| { quote! { &(#arg), } - }.token_trees).collect::>(); + }.token_trees); let expanded = quote! { { { (##arg_tts); } } @@ -254,7 +254,7 @@ fn format_args_expand( let _format_string = args.remove(0); let arg_tts = args.into_iter().flat_map(|arg| { quote! { std::fmt::ArgumentV1::new(&(#arg), std::fmt::Display::fmt), } - }.token_trees).collect::>(); + }.token_trees); let expanded = quote! { std::fmt::Arguments::new_v1(&[], &[##arg_tts]) }; diff --git a/crates/hir_ty/src/diagnostics/expr.rs b/crates/hir_ty/src/diagnostics/expr.rs index b809b96a0f6..dc8f20138fa 100644 --- a/crates/hir_ty/src/diagnostics/expr.rs +++ b/crates/hir_ty/src/diagnostics/expr.rs @@ -56,7 +56,7 @@ impl BodyValidationDiagnostic { pub fn collect(db: &dyn HirDatabase, owner: DefWithBodyId) -> Vec { let _p = profile::span("BodyValidationDiagnostic::collect"); let infer = db.infer(owner); - let mut validator = ExprValidator::new(owner, infer.clone()); + let mut validator = ExprValidator::new(owner, infer); validator.validate_body(db); validator.diagnostics } diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs index 4e4f6e5a46a..c3a5b979f71 100644 --- a/crates/hir_ty/src/infer/expr.rs +++ b/crates/hir_ty/src/infer/expr.rs @@ -367,7 +367,7 @@ fn infer_expr_inner(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty { Expr::Path(p) => { // FIXME this could be more efficient... let resolver = resolver_for_expr(self.db.upcast(), self.owner, tgt_expr); - self.infer_path(&resolver, p, tgt_expr.into()).unwrap_or(self.err_ty()) + self.infer_path(&resolver, p, tgt_expr.into()).unwrap_or_else(|| self.err_ty()) } Expr::Continue { .. } => TyKind::Never.intern(&Interner), Expr::Break { expr, label } => { @@ -511,7 +511,7 @@ fn infer_expr_inner(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty { _ => None, } }) - .unwrap_or(self.err_ty()); + .unwrap_or_else(|| self.err_ty()); let ty = self.insert_type_vars(ty); self.normalize_associated_types_in(ty) } @@ -818,8 +818,10 @@ fn infer_block( for stmt in statements { match stmt { Statement::Let { pat, type_ref, initializer } => { - let decl_ty = - type_ref.as_ref().map(|tr| self.make_ty(tr)).unwrap_or(self.err_ty()); + let decl_ty = type_ref + .as_ref() + .map(|tr| self.make_ty(tr)) + .unwrap_or_else(|| self.err_ty()); // Always use the declared type when specified let mut ty = decl_ty.clone(); diff --git a/crates/hir_ty/src/infer/pat.rs b/crates/hir_ty/src/infer/pat.rs index 58cb23e9dca..c79ed91eacf 100644 --- a/crates/hir_ty/src/infer/pat.rs +++ b/crates/hir_ty/src/infer/pat.rs @@ -192,7 +192,7 @@ pub(super) fn infer_pat( Pat::Path(path) => { // FIXME use correct resolver for the surrounding expression let resolver = self.resolver.clone(); - self.infer_path(&resolver, path, pat.into()).unwrap_or(self.err_ty()) + self.infer_path(&resolver, path, pat.into()).unwrap_or_else(|| self.err_ty()) } Pat::Bind { mode, name: _, subpat } => { let mode = if mode == &BindingAnnotation::Unannotated { diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs index 817a65c2001..ea03b6a6c5e 100644 --- a/crates/hir_ty/src/lower.rs +++ b/crates/hir_ty/src/lower.rs @@ -562,7 +562,7 @@ fn select_associated_type(&self, res: Option, segment: PathSegment<'_>) }, ); - ty.unwrap_or(TyKind::Error.intern(&Interner)) + ty.unwrap_or_else(|| TyKind::Error.intern(&Interner)) } else { TyKind::Error.intern(&Interner) } diff --git a/crates/ide/src/goto_type_definition.rs b/crates/ide/src/goto_type_definition.rs index ca3c02bf65b..43cffefe506 100644 --- a/crates/ide/src/goto_type_definition.rs +++ b/crates/ide/src/goto_type_definition.rs @@ -25,7 +25,7 @@ pub(crate) fn goto_type_definition( let token: SyntaxToken = pick_best(file.syntax().token_at_offset(position.offset))?; let token: SyntaxToken = sema.descend_into_macros(token); - let (ty, node) = sema.token_ancestors_with_macros(token.clone()).find_map(|node| { + let (ty, node) = sema.token_ancestors_with_macros(token).find_map(|node| { let ty = match_ast! { match node { ast::Expr(it) => sema.type_of_expr(&it)?, diff --git a/crates/ide_assists/src/handlers/remove_dbg.rs b/crates/ide_assists/src/handlers/remove_dbg.rs index b20fe992d89..5866d8974ef 100644 --- a/crates/ide_assists/src/handlers/remove_dbg.rs +++ b/crates/ide_assists/src/handlers/remove_dbg.rs @@ -35,14 +35,14 @@ pub(crate) fn remove_dbg(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { .prev_sibling_or_token() .and_then(whitespace_start) .map(|start| TextRange::new(start, macro_call.syntax().text_range().end())) - .unwrap_or(macro_call.syntax().text_range()) + .unwrap_or_else(|| macro_call.syntax().text_range()) }, ast::ExprStmt(it) => { let start = it .syntax() .prev_sibling_or_token() .and_then(whitespace_start) - .unwrap_or(it.syntax().text_range().start()); + .unwrap_or_else(|| it.syntax().text_range().start()); let end = it.syntax().text_range().end(); TextRange::new(start, end) diff --git a/crates/proc_macro_api/src/version.rs b/crates/proc_macro_api/src/version.rs index 28a4ac0862e..434decc7e51 100644 --- a/crates/proc_macro_api/src/version.rs +++ b/crates/proc_macro_api/src/version.rs @@ -28,23 +28,23 @@ macro_rules! err { let ver_str = read_version(dylib_path)?; let mut items = ver_str.split_whitespace(); - let tag = items.next().ok_or(err!("version format error"))?; + let tag = items.next().ok_or_else(|| err!("version format error"))?; if tag != "rustc" { return Err(err!("version format error (No rustc tag)")); } - let version_part = items.next().ok_or(err!("no version string"))?; + let version_part = items.next().ok_or_else(|| err!("no version string"))?; let mut version_parts = version_part.split('-'); - let version = version_parts.next().ok_or(err!("no version"))?; + let version = version_parts.next().ok_or_else(|| err!("no version"))?; let channel = version_parts.next().unwrap_or_default().to_string(); - let commit = items.next().ok_or(err!("no commit info"))?; + let commit = items.next().ok_or_else(|| err!("no commit info"))?; // remove ( if commit.len() == 0 { return Err(err!("commit format error")); } let commit = commit[1..].to_string(); - let date = items.next().ok_or(err!("no date info"))?; + let date = items.next().ok_or_else(|| err!("no date info"))?; // remove ) if date.len() == 0 { return Err(err!("date format error")); diff --git a/crates/project_model/src/build_data.rs b/crates/project_model/src/build_data.rs index 53cb4bae738..a76f25a8cdc 100644 --- a/crates/project_model/src/build_data.rs +++ b/crates/project_model/src/build_data.rs @@ -187,7 +187,7 @@ fn collect( let mut deserializer = serde_json::Deserializer::from_str(line); deserializer.disable_recursion_limit(); let message = Message::deserialize(&mut deserializer) - .unwrap_or(Message::TextLine(line.to_string())); + .unwrap_or_else(|_| Message::TextLine(line.to_string())); match message { Message::BuildScriptExecuted(BuildScript { diff --git a/crates/test_utils/src/fixture.rs b/crates/test_utils/src/fixture.rs index 8d8f3b560c4..44656267fed 100644 --- a/crates/test_utils/src/fixture.rs +++ b/crates/test_utils/src/fixture.rs @@ -131,7 +131,7 @@ pub fn parse(ra_fixture: &str) -> (Option, Vec) { res.push(meta) } else { if line.starts_with("// ") - && line.contains(":") + && line.contains(':') && !line.contains("::") && line.chars().all(|it| !it.is_uppercase()) { From 6b1f0057f2dbe48f1c59c8938957daf158e30823 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauren=C8=9Biu=20Nicola?= Date: Fri, 18 Jun 2021 14:44:36 +0300 Subject: [PATCH 2/2] Avoid string allocation --- crates/project_model/src/build_data.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/project_model/src/build_data.rs b/crates/project_model/src/build_data.rs index a76f25a8cdc..45bbb08dc65 100644 --- a/crates/project_model/src/build_data.rs +++ b/crates/project_model/src/build_data.rs @@ -229,7 +229,7 @@ fn collect( Message::CompilerArtifact(message) => { progress(format!("metadata {}", message.target.name)); - if message.target.kind.contains(&"proc-macro".to_string()) { + if message.target.kind.iter().any(|k| k == "proc-macro") { let package_id = message.package_id; // Skip rmeta file if let Some(filename) =