mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-14 14:13:58 +00:00
Auto merge of #16722 - mo8it:allocations, r=Veykril
Avoid some allocations I went on a small `.clone()` hunting tour :D
This commit is contained in:
commit
2074cc28de
6 changed files with 30 additions and 27 deletions
|
@ -371,7 +371,7 @@ impl flags::AnalysisStats {
|
||||||
|
|
||||||
let parse = sema.parse(file_id);
|
let parse = sema.parse(file_id);
|
||||||
let file_txt = db.file_text(file_id);
|
let file_txt = db.file_text(file_id);
|
||||||
let path = vfs.file_path(file_id).as_path().unwrap().to_owned();
|
let path = vfs.file_path(file_id).as_path().unwrap();
|
||||||
|
|
||||||
for node in parse.syntax().descendants() {
|
for node in parse.syntax().descendants() {
|
||||||
let expr = match syntax::ast::Expr::cast(node.clone()) {
|
let expr = match syntax::ast::Expr::cast(node.clone()) {
|
||||||
|
@ -446,7 +446,7 @@ impl flags::AnalysisStats {
|
||||||
edit.apply(&mut txt);
|
edit.apply(&mut txt);
|
||||||
|
|
||||||
if self.validate_term_search {
|
if self.validate_term_search {
|
||||||
std::fs::write(&path, txt).unwrap();
|
std::fs::write(path, txt).unwrap();
|
||||||
|
|
||||||
let res = ws.run_build_scripts(&cargo_config, &|_| ()).unwrap();
|
let res = ws.run_build_scripts(&cargo_config, &|_| ()).unwrap();
|
||||||
if let Some(err) = res.error() {
|
if let Some(err) = res.error() {
|
||||||
|
@ -495,7 +495,7 @@ impl flags::AnalysisStats {
|
||||||
}
|
}
|
||||||
// Revert file back to original state
|
// Revert file back to original state
|
||||||
if self.validate_term_search {
|
if self.validate_term_search {
|
||||||
std::fs::write(&path, file_txt.to_string()).unwrap();
|
std::fs::write(path, file_txt.to_string()).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
bar.inc(1);
|
bar.inc(1);
|
||||||
|
@ -1077,12 +1077,12 @@ fn location_csv_pat(db: &RootDatabase, vfs: &Vfs, sm: &BodySourceMap, pat_id: Pa
|
||||||
format!("{path},{}:{},{}:{}", start.line + 1, start.col, end.line + 1, end.col)
|
format!("{path},{}:{},{}:{}", start.line + 1, start.col, end.line + 1, end.col)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn expr_syntax_range(
|
fn expr_syntax_range<'a>(
|
||||||
db: &RootDatabase,
|
db: &RootDatabase,
|
||||||
vfs: &Vfs,
|
vfs: &'a Vfs,
|
||||||
sm: &BodySourceMap,
|
sm: &BodySourceMap,
|
||||||
expr_id: ExprId,
|
expr_id: ExprId,
|
||||||
) -> Option<(VfsPath, LineCol, LineCol)> {
|
) -> Option<(&'a VfsPath, LineCol, LineCol)> {
|
||||||
let src = sm.expr_syntax(expr_id);
|
let src = sm.expr_syntax(expr_id);
|
||||||
if let Ok(src) = src {
|
if let Ok(src) = src {
|
||||||
let root = db.parse_or_expand(src.file_id);
|
let root = db.parse_or_expand(src.file_id);
|
||||||
|
@ -1098,12 +1098,12 @@ fn expr_syntax_range(
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn pat_syntax_range(
|
fn pat_syntax_range<'a>(
|
||||||
db: &RootDatabase,
|
db: &RootDatabase,
|
||||||
vfs: &Vfs,
|
vfs: &'a Vfs,
|
||||||
sm: &BodySourceMap,
|
sm: &BodySourceMap,
|
||||||
pat_id: PatId,
|
pat_id: PatId,
|
||||||
) -> Option<(VfsPath, LineCol, LineCol)> {
|
) -> Option<(&'a VfsPath, LineCol, LineCol)> {
|
||||||
let src = sm.pat_syntax(pat_id);
|
let src = sm.pat_syntax(pat_id);
|
||||||
if let Ok(src) = src {
|
if let Ok(src) = src {
|
||||||
let root = db.parse_or_expand(src.file_id);
|
let root = db.parse_or_expand(src.file_id);
|
||||||
|
|
|
@ -297,7 +297,7 @@ impl GlobalState {
|
||||||
let mut bytes = vec![];
|
let mut bytes = vec![];
|
||||||
let mut modified_rust_files = vec![];
|
let mut modified_rust_files = vec![];
|
||||||
for file in changed_files {
|
for file in changed_files {
|
||||||
let vfs_path = &vfs.file_path(file.file_id);
|
let vfs_path = vfs.file_path(file.file_id);
|
||||||
if let Some(path) = vfs_path.as_path() {
|
if let Some(path) = vfs_path.as_path() {
|
||||||
let path = path.to_path_buf();
|
let path = path.to_path_buf();
|
||||||
if reload::should_refresh_for_change(&path, file.kind()) {
|
if reload::should_refresh_for_change(&path, file.kind()) {
|
||||||
|
@ -481,7 +481,7 @@ impl GlobalStateSnapshot {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn anchored_path(&self, path: &AnchoredPathBuf) -> Url {
|
pub(crate) fn anchored_path(&self, path: &AnchoredPathBuf) -> Url {
|
||||||
let mut base = self.vfs_read().file_path(path.anchor);
|
let mut base = self.vfs_read().file_path(path.anchor).clone();
|
||||||
base.pop();
|
base.pop();
|
||||||
let path = base.join(&path.path).unwrap();
|
let path = base.join(&path.path).unwrap();
|
||||||
let path = path.as_path().unwrap();
|
let path = path.as_path().unwrap();
|
||||||
|
@ -489,7 +489,7 @@ impl GlobalStateSnapshot {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn file_id_to_file_path(&self, file_id: FileId) -> vfs::VfsPath {
|
pub(crate) fn file_id_to_file_path(&self, file_id: FileId) -> vfs::VfsPath {
|
||||||
self.vfs_read().file_path(file_id)
|
self.vfs_read().file_path(file_id).clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn cargo_target_for_crate_root(
|
pub(crate) fn cargo_target_for_crate_root(
|
||||||
|
@ -497,7 +497,7 @@ impl GlobalStateSnapshot {
|
||||||
crate_id: CrateId,
|
crate_id: CrateId,
|
||||||
) -> Option<(&CargoWorkspace, Target)> {
|
) -> Option<(&CargoWorkspace, Target)> {
|
||||||
let file_id = self.analysis.crate_root(crate_id).ok()?;
|
let file_id = self.analysis.crate_root(crate_id).ok()?;
|
||||||
let path = self.vfs_read().file_path(file_id);
|
let path = self.vfs_read().file_path(file_id).clone();
|
||||||
let path = path.as_path()?;
|
let path = path.as_path()?;
|
||||||
self.workspaces.iter().find_map(|ws| match ws {
|
self.workspaces.iter().find_map(|ws| match ws {
|
||||||
ProjectWorkspace::Cargo { cargo, .. } => {
|
ProjectWorkspace::Cargo { cargo, .. } => {
|
||||||
|
|
|
@ -2097,7 +2097,7 @@ pub(crate) fn fetch_dependency_list(
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter_map(|it| {
|
.filter_map(|it| {
|
||||||
let root_file_path = state.file_id_to_file_path(it.root_file_id);
|
let root_file_path = state.file_id_to_file_path(it.root_file_id);
|
||||||
crate_path(root_file_path).and_then(to_url).map(|path| CrateInfoResult {
|
crate_path(&root_file_path).and_then(to_url).map(|path| CrateInfoResult {
|
||||||
name: it.name,
|
name: it.name,
|
||||||
version: it.version,
|
version: it.version,
|
||||||
path,
|
path,
|
||||||
|
@ -2118,7 +2118,7 @@ pub(crate) fn fetch_dependency_list(
|
||||||
/// An `Option` value representing the path to the directory of the crate with the given
|
/// An `Option` value representing the path to the directory of the crate with the given
|
||||||
/// name, if such a crate is found. If no crate with the given name is found, this function
|
/// name, if such a crate is found. If no crate with the given name is found, this function
|
||||||
/// returns `None`.
|
/// returns `None`.
|
||||||
fn crate_path(root_file_path: VfsPath) -> Option<VfsPath> {
|
fn crate_path(root_file_path: &VfsPath) -> Option<VfsPath> {
|
||||||
let mut current_dir = root_file_path.parent();
|
let mut current_dir = root_file_path.parent();
|
||||||
while let Some(path) = current_dir {
|
while let Some(path) = current_dir {
|
||||||
let cargo_toml_path = path.join("../Cargo.toml")?;
|
let cargo_toml_path = path.join("../Cargo.toml")?;
|
||||||
|
|
|
@ -149,15 +149,15 @@ impl ChangeFixture {
|
||||||
for entry in fixture {
|
for entry in fixture {
|
||||||
let text = if entry.text.contains(CURSOR_MARKER) {
|
let text = if entry.text.contains(CURSOR_MARKER) {
|
||||||
if entry.text.contains(ESCAPED_CURSOR_MARKER) {
|
if entry.text.contains(ESCAPED_CURSOR_MARKER) {
|
||||||
entry.text.replace(ESCAPED_CURSOR_MARKER, CURSOR_MARKER)
|
entry.text.replace(ESCAPED_CURSOR_MARKER, CURSOR_MARKER).into()
|
||||||
} else {
|
} else {
|
||||||
let (range_or_offset, text) = extract_range_or_offset(&entry.text);
|
let (range_or_offset, text) = extract_range_or_offset(&entry.text);
|
||||||
assert!(file_position.is_none());
|
assert!(file_position.is_none());
|
||||||
file_position = Some((file_id, range_or_offset));
|
file_position = Some((file_id, range_or_offset));
|
||||||
text
|
text.into()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
entry.text.clone()
|
entry.text.as_str().into()
|
||||||
};
|
};
|
||||||
|
|
||||||
let meta = FileMeta::from_fixture(entry, current_source_root_kind);
|
let meta = FileMeta::from_fixture(entry, current_source_root_kind);
|
||||||
|
@ -195,7 +195,10 @@ impl ChangeFixture {
|
||||||
let prev = crates.insert(crate_name.clone(), crate_id);
|
let prev = crates.insert(crate_name.clone(), crate_id);
|
||||||
assert!(prev.is_none(), "multiple crates with same name: {}", crate_name);
|
assert!(prev.is_none(), "multiple crates with same name: {}", crate_name);
|
||||||
for dep in meta.deps {
|
for dep in meta.deps {
|
||||||
let prelude = meta.extern_prelude.contains(&dep);
|
let prelude = match &meta.extern_prelude {
|
||||||
|
Some(v) => v.contains(&dep),
|
||||||
|
None => true,
|
||||||
|
};
|
||||||
let dep = CrateName::normalize_dashes(&dep);
|
let dep = CrateName::normalize_dashes(&dep);
|
||||||
crate_deps.push((crate_name.clone(), dep, prelude))
|
crate_deps.push((crate_name.clone(), dep, prelude))
|
||||||
}
|
}
|
||||||
|
@ -206,7 +209,7 @@ impl ChangeFixture {
|
||||||
default_env.extend(meta.env.iter().map(|(x, y)| (x.to_owned(), y.to_owned())));
|
default_env.extend(meta.env.iter().map(|(x, y)| (x.to_owned(), y.to_owned())));
|
||||||
}
|
}
|
||||||
|
|
||||||
source_change.change_file(file_id, Some(text.into()));
|
source_change.change_file(file_id, Some(text));
|
||||||
let path = VfsPath::new_virtual_path(meta.path);
|
let path = VfsPath::new_virtual_path(meta.path);
|
||||||
file_set.insert(file_id, path);
|
file_set.insert(file_id, path);
|
||||||
files.push(file_id);
|
files.push(file_id);
|
||||||
|
@ -443,7 +446,7 @@ struct FileMeta {
|
||||||
path: String,
|
path: String,
|
||||||
krate: Option<(String, CrateOrigin, Option<String>)>,
|
krate: Option<(String, CrateOrigin, Option<String>)>,
|
||||||
deps: Vec<String>,
|
deps: Vec<String>,
|
||||||
extern_prelude: Vec<String>,
|
extern_prelude: Option<Vec<String>>,
|
||||||
cfg: CfgOptions,
|
cfg: CfgOptions,
|
||||||
edition: Edition,
|
edition: Edition,
|
||||||
env: Env,
|
env: Env,
|
||||||
|
@ -473,7 +476,7 @@ impl FileMeta {
|
||||||
Self {
|
Self {
|
||||||
path: f.path,
|
path: f.path,
|
||||||
krate: f.krate.map(|it| parse_crate(it, current_source_root_kind, f.library)),
|
krate: f.krate.map(|it| parse_crate(it, current_source_root_kind, f.library)),
|
||||||
extern_prelude: f.extern_prelude.unwrap_or_else(|| deps.clone()),
|
extern_prelude: f.extern_prelude,
|
||||||
deps,
|
deps,
|
||||||
cfg,
|
cfg,
|
||||||
edition: f.edition.map_or(Edition::CURRENT, |v| Edition::from_str(&v).unwrap()),
|
edition: f.edition.map_or(Edition::CURRENT, |v| Edition::from_str(&v).unwrap()),
|
||||||
|
|
|
@ -163,8 +163,8 @@ impl Vfs {
|
||||||
/// # Panics
|
/// # Panics
|
||||||
///
|
///
|
||||||
/// Panics if the id is not present in the `Vfs`.
|
/// Panics if the id is not present in the `Vfs`.
|
||||||
pub fn file_path(&self, file_id: FileId) -> VfsPath {
|
pub fn file_path(&self, file_id: FileId) -> &VfsPath {
|
||||||
self.interner.lookup(file_id).clone()
|
self.interner.lookup(file_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns an iterator over the stored ids and their corresponding paths.
|
/// Returns an iterator over the stored ids and their corresponding paths.
|
||||||
|
|
|
@ -37,7 +37,7 @@ impl<I> Incoming<I> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn cancel(&mut self, id: RequestId) -> Option<Response> {
|
pub fn cancel(&mut self, id: RequestId) -> Option<Response> {
|
||||||
let _data = self.complete(id.clone())?;
|
let _data = self.complete(&id)?;
|
||||||
let error = ResponseError {
|
let error = ResponseError {
|
||||||
code: ErrorCode::RequestCanceled as i32,
|
code: ErrorCode::RequestCanceled as i32,
|
||||||
message: "canceled by client".to_owned(),
|
message: "canceled by client".to_owned(),
|
||||||
|
@ -46,8 +46,8 @@ impl<I> Incoming<I> {
|
||||||
Some(Response { id, result: None, error: Some(error) })
|
Some(Response { id, result: None, error: Some(error) })
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn complete(&mut self, id: RequestId) -> Option<I> {
|
pub fn complete(&mut self, id: &RequestId) -> Option<I> {
|
||||||
self.pending.remove(&id)
|
self.pending.remove(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_completed(&self, id: &RequestId) -> bool {
|
pub fn is_completed(&self, id: &RequestId) -> bool {
|
||||||
|
|
Loading…
Reference in a new issue