about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-12-19 16:57:12 +0000
committerbors <bors@rust-lang.org>2022-12-19 16:57:12 +0000
commitffedfc63a1936c65eb8ada3cb8df8edbc1f5f3ca (patch)
treebbcfbf5114ddc1de1aa4efa3031ad2967f1cca36
parent1f74b1b04ecc78e89e5587068d4fc0d0dc118b31 (diff)
parent5706910add63a2a0800cc98c867a8e06f3422f48 (diff)
downloadrust-ffedfc63a1936c65eb8ada3cb8df8edbc1f5f3ca.tar.gz
rust-ffedfc63a1936c65eb8ada3cb8df8edbc1f5f3ca.zip
Auto merge of #13795 - jonas-schievink:fix-rustfmt-edition-in-path-deps, r=jonas-schievink
fix: Use the correct edition when formatting code in path dependencies

Fixes https://github.com/rust-lang/rust-analyzer/issues/13790

Don't go through the Cargo workspace info, since that doesn't contain path dependencies. Instead, query the crate graph via `Analysis::crate_edition`.
-rw-r--r--crates/rust-analyzer/src/handlers.rs11
1 files changed, 6 insertions, 5 deletions
diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs
index 1604a02fb13..2850bc2c3d9 100644
--- a/crates/rust-analyzer/src/handlers.rs
+++ b/crates/rust-analyzer/src/handlers.rs
@@ -1788,14 +1788,15 @@ fn run_rustfmt(
     let file_id = from_proto::file_id(snap, &text_document.uri)?;
     let file = snap.analysis.file_text(file_id)?;
 
-    // find the edition of the package the file belongs to
-    // (if it belongs to multiple we'll just pick the first one and pray)
-    let edition = snap
+    // Determine the edition of the crate the file belongs to (if there's multiple, we pick the
+    // highest edition).
+    let editions = snap
         .analysis
         .relevant_crates_for(file_id)?
         .into_iter()
-        .find_map(|crate_id| snap.cargo_target_for_crate_root(crate_id))
-        .map(|(ws, target)| ws[ws[target].package].edition);
+        .map(|crate_id| snap.analysis.crate_edition(crate_id))
+        .collect::<Result<Vec<_>, _>>()?;
+    let edition = editions.iter().copied().max();
 
     let line_index = snap.file_line_index(file_id)?;