aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Krüger <[email protected]>2021-03-15 09:15:08 +0000
committerMatthias Krüger <[email protected]>2021-03-15 09:19:59 +0000
commitcad617bba054334e2172b9ef54f2ed82c6067794 (patch)
tree3c124ef8606b985353de946245498babe5d7c6a0
parentde360275416ca095102f2b17d6ca1de3bd091fdb (diff)
some clippy::performance fixes
use vec![] instead of Vec::new() + push() avoid redundant clones use chars instead of &str for single char patterns in ends_with() and starts_with() allocate some Vecs with capacity to avoid unneccessary resizing
-rw-r--r--crates/hir/src/lib.rs3
-rw-r--r--crates/hir_expand/src/builtin_derive.rs2
-rw-r--r--crates/ide/src/join_lines.rs4
-rw-r--r--crates/ide/src/typing.rs3
-rw-r--r--crates/ide_completion/src/completions/postfix/format_like.rs2
-rw-r--r--crates/mbe/src/syntax_bridge.rs8
-rw-r--r--crates/proc_macro_api/src/version.rs4
-rw-r--r--crates/project_model/src/rustc_cfg.rs2
-rw-r--r--crates/rust-analyzer/src/lsp_utils.rs4
9 files changed, 13 insertions, 19 deletions
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs
index eb1cd66fb..caa760d21 100644
--- a/crates/hir/src/lib.rs
+++ b/crates/hir/src/lib.rs
@@ -266,8 +266,7 @@ impl ModuleDef {
266 } 266 }
267 267
268 pub fn canonical_path(&self, db: &dyn HirDatabase) -> Option<String> { 268 pub fn canonical_path(&self, db: &dyn HirDatabase) -> Option<String> {
269 let mut segments = Vec::new(); 269 let mut segments = vec![self.name(db)?.to_string()];
270 segments.push(self.name(db)?.to_string());
271 for m in self.module(db)?.path_to_root(db) { 270 for m in self.module(db)?.path_to_root(db) {
272 segments.extend(m.name(db).map(|it| it.to_string())) 271 segments.extend(m.name(db).map(|it| it.to_string()))
273 } 272 }
diff --git a/crates/hir_expand/src/builtin_derive.rs b/crates/hir_expand/src/builtin_derive.rs
index dfdb9cf59..5e908b223 100644
--- a/crates/hir_expand/src/builtin_derive.rs
+++ b/crates/hir_expand/src/builtin_derive.rs
@@ -108,7 +108,7 @@ fn parse_adt(tt: &tt::Subtree) -> Result<BasicAdtInfo, mbe::ExpandError> {
108} 108}
109 109
110fn make_type_args(n: usize, bound: Vec<tt::TokenTree>) -> Vec<tt::TokenTree> { 110fn make_type_args(n: usize, bound: Vec<tt::TokenTree>) -> Vec<tt::TokenTree> {
111 let mut result = Vec::<tt::TokenTree>::new(); 111 let mut result = Vec::<tt::TokenTree>::with_capacity(n * 2);
112 result.push( 112 result.push(
113 tt::Leaf::Punct(tt::Punct { 113 tt::Leaf::Punct(tt::Punct {
114 char: '<', 114 char: '<',
diff --git a/crates/ide/src/join_lines.rs b/crates/ide/src/join_lines.rs
index 20a920ddb..d571ed559 100644
--- a/crates/ide/src/join_lines.rs
+++ b/crates/ide/src/join_lines.rs
@@ -218,7 +218,7 @@ mod tests {
218 let result = join_lines(&file, range); 218 let result = join_lines(&file, range);
219 219
220 let actual = { 220 let actual = {
221 let mut actual = before.to_string(); 221 let mut actual = before;
222 result.apply(&mut actual); 222 result.apply(&mut actual);
223 actual 223 actual
224 }; 224 };
@@ -622,7 +622,7 @@ fn foo() {
622 let parse = SourceFile::parse(&before); 622 let parse = SourceFile::parse(&before);
623 let result = join_lines(&parse.tree(), sel); 623 let result = join_lines(&parse.tree(), sel);
624 let actual = { 624 let actual = {
625 let mut actual = before.to_string(); 625 let mut actual = before;
626 result.apply(&mut actual); 626 result.apply(&mut actual);
627 actual 627 actual
628 }; 628 };
diff --git a/crates/ide/src/typing.rs b/crates/ide/src/typing.rs
index e3c3aebac..a718faf63 100644
--- a/crates/ide/src/typing.rs
+++ b/crates/ide/src/typing.rs
@@ -145,9 +145,8 @@ mod tests {
145 use super::*; 145 use super::*;
146 146
147 fn do_type_char(char_typed: char, before: &str) -> Option<String> { 147 fn do_type_char(char_typed: char, before: &str) -> Option<String> {
148 let (offset, before) = extract_offset(before); 148 let (offset, mut before) = extract_offset(before);
149 let edit = TextEdit::insert(offset, char_typed.to_string()); 149 let edit = TextEdit::insert(offset, char_typed.to_string());
150 let mut before = before.to_string();
151 edit.apply(&mut before); 150 edit.apply(&mut before);
152 let parse = SourceFile::parse(&before); 151 let parse = SourceFile::parse(&before);
153 on_char_typed_inner(&parse.tree(), offset, char_typed).map(|it| { 152 on_char_typed_inner(&parse.tree(), offset, char_typed).map(|it| {
diff --git a/crates/ide_completion/src/completions/postfix/format_like.rs b/crates/ide_completion/src/completions/postfix/format_like.rs
index 3afc63021..cee4eec10 100644
--- a/crates/ide_completion/src/completions/postfix/format_like.rs
+++ b/crates/ide_completion/src/completions/postfix/format_like.rs
@@ -59,7 +59,7 @@ pub(crate) fn add_format_like_completions(
59/// Checks whether provided item is a string literal. 59/// Checks whether provided item is a string literal.
60fn string_literal_contents(item: &ast::String) -> Option<String> { 60fn string_literal_contents(item: &ast::String) -> Option<String> {
61 let item = item.text(); 61 let item = item.text();
62 if item.len() >= 2 && item.starts_with("\"") && item.ends_with("\"") { 62 if item.len() >= 2 && item.starts_with('\"') && item.ends_with('\"') {
63 return Some(item[1..item.len() - 1].to_owned()); 63 return Some(item[1..item.len() - 1].to_owned());
64 } 64 }
65 65
diff --git a/crates/mbe/src/syntax_bridge.rs b/crates/mbe/src/syntax_bridge.rs
index aacae1026..b715ebfc4 100644
--- a/crates/mbe/src/syntax_bridge.rs
+++ b/crates/mbe/src/syntax_bridge.rs
@@ -222,14 +222,10 @@ fn convert_doc_comment(token: &syntax::SyntaxToken) -> Option<Vec<tt::TokenTree>
222 let doc = comment.kind().doc?; 222 let doc = comment.kind().doc?;
223 223
224 // Make `doc="\" Comments\"" 224 // Make `doc="\" Comments\""
225 let mut meta_tkns = Vec::new(); 225 let meta_tkns = vec![mk_ident("doc"), mk_punct('='), mk_doc_literal(&comment)];
226 meta_tkns.push(mk_ident("doc"));
227 meta_tkns.push(mk_punct('='));
228 meta_tkns.push(mk_doc_literal(&comment));
229 226
230 // Make `#![]` 227 // Make `#![]`
231 let mut token_trees = Vec::new(); 228 let mut token_trees = vec![mk_punct('#')];
232 token_trees.push(mk_punct('#'));
233 if let ast::CommentPlacement::Inner = doc { 229 if let ast::CommentPlacement::Inner = doc {
234 token_trees.push(mk_punct('!')); 230 token_trees.push(mk_punct('!'));
235 } 231 }
diff --git a/crates/proc_macro_api/src/version.rs b/crates/proc_macro_api/src/version.rs
index 11a7fb59a..b903658fb 100644
--- a/crates/proc_macro_api/src/version.rs
+++ b/crates/proc_macro_api/src/version.rs
@@ -33,7 +33,7 @@ pub(crate) fn read_info(dylib_path: &Path) -> io::Result<RustCInfo> {
33 } 33 }
34 34
35 let version_part = items.next().ok_or(err!("no version string"))?; 35 let version_part = items.next().ok_or(err!("no version string"))?;
36 let mut version_parts = version_part.split("-"); 36 let mut version_parts = version_part.split('-');
37 let version = version_parts.next().ok_or(err!("no version"))?; 37 let version = version_parts.next().ok_or(err!("no version"))?;
38 let channel = version_parts.next().unwrap_or_default().to_string(); 38 let channel = version_parts.next().unwrap_or_default().to_string();
39 39
@@ -51,7 +51,7 @@ pub(crate) fn read_info(dylib_path: &Path) -> io::Result<RustCInfo> {
51 let date = date[0..date.len() - 2].to_string(); 51 let date = date[0..date.len() - 2].to_string();
52 52
53 let version_numbers = version 53 let version_numbers = version
54 .split(".") 54 .split('.')
55 .map(|it| it.parse::<usize>()) 55 .map(|it| it.parse::<usize>())
56 .collect::<Result<Vec<_>, _>>() 56 .collect::<Result<Vec<_>, _>>()
57 .map_err(|_| err!("version number error"))?; 57 .map_err(|_| err!("version number error"))?;
diff --git a/crates/project_model/src/rustc_cfg.rs b/crates/project_model/src/rustc_cfg.rs
index 4a7bd8ae3..312708575 100644
--- a/crates/project_model/src/rustc_cfg.rs
+++ b/crates/project_model/src/rustc_cfg.rs
@@ -6,7 +6,7 @@ use crate::{cfg_flag::CfgFlag, utf8_stdout};
6 6
7pub(crate) fn get(target: Option<&str>) -> Vec<CfgFlag> { 7pub(crate) fn get(target: Option<&str>) -> Vec<CfgFlag> {
8 let _p = profile::span("rustc_cfg::get"); 8 let _p = profile::span("rustc_cfg::get");
9 let mut res = Vec::new(); 9 let mut res = Vec::with_capacity(6 * 2 + 1);
10 10
11 // Some nightly-only cfgs, which are required for stdlib 11 // Some nightly-only cfgs, which are required for stdlib
12 res.push(CfgFlag::Atom("target_thread_local".into())); 12 res.push(CfgFlag::Atom("target_thread_local".into()));
diff --git a/crates/rust-analyzer/src/lsp_utils.rs b/crates/rust-analyzer/src/lsp_utils.rs
index 84f78b5b8..3ca7f8040 100644
--- a/crates/rust-analyzer/src/lsp_utils.rs
+++ b/crates/rust-analyzer/src/lsp_utils.rs
@@ -360,11 +360,11 @@ mod tests {
360 "Completion with disjoint edits is valid" 360 "Completion with disjoint edits is valid"
361 ); 361 );
362 assert!( 362 assert!(
363 !all_edits_are_disjoint(&completion_with_disjoint_edits, &[joint_edit.clone()]), 363 !all_edits_are_disjoint(&completion_with_disjoint_edits, &[joint_edit]),
364 "Completion with disjoint edits and joint extra edit is invalid" 364 "Completion with disjoint edits and joint extra edit is invalid"
365 ); 365 );
366 assert!( 366 assert!(
367 all_edits_are_disjoint(&completion_with_disjoint_edits, &[disjoint_edit_2.clone()]), 367 all_edits_are_disjoint(&completion_with_disjoint_edits, &[disjoint_edit_2]),
368 "Completion with disjoint edits and joint extra edit is valid" 368 "Completion with disjoint edits and joint extra edit is valid"
369 ); 369 );
370 } 370 }