Auto merge of #16451 - Urhengulas:satisfy-clippy, r=Veykril

internal: Work through temporarily allowed clippy lints, part 2

Another follow-up to https://github.com/rust-lang/rust-analyzer/pull/16401.
This commit is contained in:
bors 2024-02-01 14:23:18 +00:00
commit 850ba2fb63
39 changed files with 136 additions and 149 deletions

View File

@ -167,29 +167,14 @@ new_ret_no_self = "allow"
## Following lints should be tackled at some point
borrowed_box = "allow"
borrow_deref_ref = "allow"
derivable_impls = "allow"
derived_hash_with_manual_eq = "allow"
field_reassign_with_default = "allow"
forget_non_drop = "allow"
format_collect = "allow"
large_enum_variant = "allow"
needless_doctest_main = "allow"
new_without_default = "allow"
non_canonical_clone_impl = "allow"
non_canonical_partial_ord_impl = "allow"
self_named_constructors = "allow"
skip_while_next = "allow"
too_many_arguments = "allow"
toplevel_ref_arg = "allow"
type_complexity = "allow"
unnecessary_cast = "allow"
unnecessary_filter_map = "allow"
unnecessary_lazy_evaluations = "allow"
unnecessary_mut_passed = "allow"
useless_conversion = "allow"
useless_format = "allow"
wildcard_in_or_patterns = "allow"
wrong_self_convention = "allow"
## warn at following lints

View File

@ -493,7 +493,9 @@ impl CargoActor {
// Skip certain kinds of messages to only spend time on what's useful
JsonMessage::Cargo(message) => match message {
cargo_metadata::Message::CompilerArtifact(artifact) if !artifact.fresh => {
self.sender.send(CargoMessage::CompilerArtifact(artifact)).unwrap();
self.sender
.send(CargoMessage::CompilerArtifact(Box::new(artifact)))
.unwrap();
}
cargo_metadata::Message::CompilerMessage(msg) => {
self.sender.send(CargoMessage::Diagnostic(msg.message)).unwrap();
@ -538,7 +540,7 @@ impl CargoActor {
}
enum CargoMessage {
CompilerArtifact(cargo_metadata::Artifact),
CompilerArtifact(Box<cargo_metadata::Artifact>),
Diagnostic(Diagnostic),
}

View File

@ -33,7 +33,7 @@ pub(super) fn print_body_hir(db: &dyn DefDatabase, body: &Body, owner: DefWithBo
}
)
}),
DefWithBodyId::InTypeConstId(_) => format!("In type const = "),
DefWithBodyId::InTypeConstId(_) => "In type const = ".to_string(),
DefWithBodyId::VariantId(it) => {
let loc = it.lookup(db);
let enum_loc = loc.parent.lookup(db);

View File

@ -256,7 +256,7 @@ impl SsrError {
"##,
);
assert_eq!(db.body_with_source_map(def.into()).1.diagnostics(), &[]);
assert_eq!(db.body_with_source_map(def).1.diagnostics(), &[]);
expect![[r#"
fn main() {
_ = $crate::error::SsrError::new(
@ -309,7 +309,7 @@ fn f() {
"#,
);
let (_, source_map) = db.body_with_source_map(def.into());
let (_, source_map) = db.body_with_source_map(def);
assert_eq!(source_map.diagnostics(), &[]);
for (_, def_map) in body.blocks(&db) {

View File

@ -782,7 +782,7 @@ impl<'a> AssocItemCollector<'a> {
self.diagnostics.push(DefDiagnostic::macro_expansion_parse_error(
self.module_id.local_id,
error_call_kind(),
errors.into(),
errors,
));
}

View File

@ -166,6 +166,7 @@ enum PositionUsedAs {
}
use PositionUsedAs::*;
#[allow(clippy::unnecessary_lazy_evaluations)]
pub(crate) fn parse(
s: &ast::String,
fmt_snippet: Option<String>,
@ -177,9 +178,9 @@ pub(crate) fn parse(
let text = s.text_without_quotes();
let str_style = match s.quote_offsets() {
Some(offsets) => {
let raw = u32::from(offsets.quotes.0.len()) - 1;
let raw = usize::from(offsets.quotes.0.len()) - 1;
// subtract 1 for the `r` prefix
(raw != 0).then(|| raw as usize - 1)
(raw != 0).then(|| raw - 1)
}
None => None,
};
@ -432,7 +433,7 @@ pub(crate) fn parse(
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct FormatArgumentsCollector {
arguments: Vec<FormatArgument>,
num_unnamed_args: usize,
@ -451,7 +452,7 @@ impl FormatArgumentsCollector {
}
pub fn new() -> Self {
Self { arguments: vec![], names: vec![], num_unnamed_args: 0, num_explicit_args: 0 }
Default::default()
}
pub fn add(&mut self, arg: FormatArgument) -> usize {

View File

@ -297,7 +297,7 @@ impl SearchMode {
SearchMode::Exact => candidate.eq_ignore_ascii_case(query),
SearchMode::Prefix => {
query.len() <= candidate.len() && {
let prefix = &candidate[..query.len() as usize];
let prefix = &candidate[..query.len()];
if case_sensitive {
prefix == query
} else {
@ -396,7 +396,7 @@ impl Query {
pub fn search_dependencies(
db: &dyn DefDatabase,
krate: CrateId,
ref query: Query,
query: &Query,
) -> FxHashSet<ItemInNs> {
let _p = tracing::span!(tracing::Level::INFO, "search_dependencies", ?query).entered();
@ -446,7 +446,7 @@ fn search_maps(
let end = (value & 0xFFFF_FFFF) as usize;
let start = (value >> 32) as usize;
let ImportMap { item_to_info_map, importables, .. } = &*import_maps[import_map_idx];
let importables = &importables[start as usize..end];
let importables = &importables[start..end];
let iter = importables
.iter()
@ -516,7 +516,7 @@ mod tests {
})
.expect("could not find crate");
let actual = search_dependencies(db.upcast(), krate, query)
let actual = search_dependencies(db.upcast(), krate, &query)
.into_iter()
.filter_map(|dependency| {
let dependency_krate = dependency.krate(db.upcast())?;

View File

@ -25,7 +25,7 @@ use hir_expand::{
InFile, MacroFileId, MacroFileIdExt,
};
use span::Span;
use stdx::format_to;
use stdx::{format_to, format_to_acc};
use syntax::{
ast::{self, edit::IndentLevel},
AstNode,
@ -149,8 +149,7 @@ pub fn identity_when_valid(_attr: TokenStream, item: TokenStream) -> TokenStream
if tree {
let tree = format!("{:#?}", parse.syntax_node())
.split_inclusive('\n')
.map(|line| format!("// {line}"))
.collect::<String>();
.fold(String::new(), |mut acc, line| format_to_acc!(acc, "// {line}"));
format_to!(expn_text, "\n{}", tree)
}
let range = call.syntax().text_range();

View File

@ -1924,7 +1924,7 @@ impl ModCollector<'_, '_> {
item_tree: self.item_tree,
mod_dir,
}
.collect_in_top_module(&*items);
.collect_in_top_module(items);
if is_macro_use {
self.import_all_legacy_macros(module_id);
}

View File

@ -475,7 +475,7 @@ impl DefMap {
let macro_use_prelude = || {
self.macro_use_prelude.get(name).map_or(PerNs::none(), |&(it, _extern_crate)| {
PerNs::macros(
it.into(),
it,
Visibility::Public,
// FIXME?
None, // extern_crate.map(ImportOrExternCrate::ExternCrate),

View File

@ -16,19 +16,13 @@ pub enum Namespace {
Macros,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
pub struct PerNs {
pub types: Option<(ModuleDefId, Visibility, Option<ImportOrExternCrate>)>,
pub values: Option<(ModuleDefId, Visibility, Option<ImportId>)>,
pub macros: Option<(MacroId, Visibility, Option<ImportId>)>,
}
impl Default for PerNs {
fn default() -> Self {
PerNs { types: None, values: None, macros: None }
}
}
impl PerNs {
pub fn none() -> PerNs {
PerNs { types: None, values: None, macros: None }
@ -131,13 +125,11 @@ impl PerNs {
.into_iter()
.chain(
self.values
.map(|it| (ItemInNs::Values(it.0), it.2.map(ImportOrExternCrate::Import)))
.into_iter(),
.map(|it| (ItemInNs::Values(it.0), it.2.map(ImportOrExternCrate::Import))),
)
.chain(
self.macros
.map(|it| (ItemInNs::Macros(it.0), it.2.map(ImportOrExternCrate::Import)))
.into_iter(),
.map(|it| (ItemInNs::Macros(it.0), it.2.map(ImportOrExternCrate::Import))),
)
}
}

View File

@ -41,13 +41,13 @@ impl Default for TestDB {
impl Upcast<dyn ExpandDatabase> for TestDB {
fn upcast(&self) -> &(dyn ExpandDatabase + 'static) {
&*self
self
}
}
impl Upcast<dyn DefDatabase> for TestDB {
fn upcast(&self) -> &(dyn DefDatabase + 'static) {
&*self
self
}
}

View File

@ -230,7 +230,7 @@ impl Attr {
)
)
})
.unwrap_or_else(|| tt.len());
.unwrap_or(tt.len());
let (path, input) = tt.split_at(path_end);
let path = Interned::new(ModPath::from_tt(db, path)?);

View File

@ -84,8 +84,7 @@ impl TypeVisitor<Interner> for UninhabitedFrom<'_> {
Some(0) | None => CONTINUE_OPAQUELY_INHABITED,
Some(1..) => item_ty.super_visit_with(self, outer_binder),
},
TyKind::Ref(..) | _ => CONTINUE_OPAQUELY_INHABITED,
_ => CONTINUE_OPAQUELY_INHABITED,
};
self.recursive_ty.remove(ty);
self.max_depth += 1;

View File

@ -114,7 +114,7 @@ impl MirLowerCtx<'_> {
index: i as u32,
}))
}),
&mut cond_place,
&cond_place,
mode,
)?
}

View File

@ -440,7 +440,7 @@ pub(crate) fn detect_variant_from_bytes<'a>(
(db.enum_data(e).variants[index.0].0, layout)
}
hir_def::layout::Variants::Multiple { tag, tag_encoding, variants, .. } => {
let size = tag.size(&*target_data_layout).bytes_usize();
let size = tag.size(target_data_layout).bytes_usize();
let offset = layout.fields.offset(0).bytes_usize(); // The only field on enum variants is the tag field
let tag = i128::from_le_bytes(pad16(&b[offset..offset + size], false));
match tag_encoding {

View File

@ -239,10 +239,9 @@ fn resolve_impl_trait_item(
) -> Option<DocLinkDef> {
let canonical = ty.canonical();
let krate = ty.krate(db);
let environment = resolver.generic_def().map_or_else(
|| crate::TraitEnvironment::empty(krate.id).into(),
|d| db.trait_environment(d),
);
let environment = resolver
.generic_def()
.map_or_else(|| crate::TraitEnvironment::empty(krate.id), |d| db.trait_environment(d));
let traits_in_scope = resolver.traits_in_scope(db.upcast());
let mut result = None;
@ -297,7 +296,7 @@ fn as_module_def_if_namespace_matches(
AssocItem::TypeAlias(it) => (ModuleDef::TypeAlias(it), Namespace::Types),
};
(ns.unwrap_or(expected_ns) == expected_ns).then(|| DocLinkDef::ModuleDef(def))
(ns.unwrap_or(expected_ns) == expected_ns).then_some(DocLinkDef::ModuleDef(def))
}
fn modpath_from_str(link: &str) -> Option<ModPath> {

View File

@ -236,7 +236,7 @@ impl Crate {
query: import_map::Query,
) -> impl Iterator<Item = Either<ModuleDef, Macro>> {
let _p = tracing::span!(tracing::Level::INFO, "query_external_importables");
import_map::search_dependencies(db, self.into(), query).into_iter().map(|item| {
import_map::search_dependencies(db, self.into(), &query).into_iter().map(|item| {
match ItemInNs::from(item) {
ItemInNs::Types(mod_id) | ItemInNs::Values(mod_id) => Either::Left(mod_id),
ItemInNs::Macros(mac_id) => Either::Right(mac_id),
@ -903,7 +903,7 @@ fn emit_def_diagnostic_(
}
DefDiagnosticKind::InvalidDeriveTarget { ast, id } => {
let node = ast.to_node(db.upcast());
let derive = node.attrs().nth(*id as usize);
let derive = node.attrs().nth(*id);
match derive {
Some(derive) => {
acc.push(
@ -918,7 +918,7 @@ fn emit_def_diagnostic_(
}
DefDiagnosticKind::MalformedDerive { ast, id } => {
let node = ast.to_node(db.upcast());
let derive = node.attrs().nth(*id as usize);
let derive = node.attrs().nth(*id);
match derive {
Some(derive) => {
acc.push(

View File

@ -2,6 +2,7 @@
use std::{fmt, fs, path::Path};
use stdx::format_to_acc;
use test_utils::project_root;
#[test]
@ -172,8 +173,7 @@ impl fmt::Display for Assist {
fn hide_hash_comments(text: &str) -> String {
text.split('\n') // want final newline
.filter(|&it| !(it.starts_with("# ") || it == "#"))
.map(|it| format!("{it}\n"))
.collect()
.fold(String::new(), |mut acc, it| format_to_acc!(acc, "{it}\n"))
}
fn reveal_hash_comments(text: &str) -> String {
@ -187,6 +187,5 @@ fn reveal_hash_comments(text: &str) -> String {
it
}
})
.map(|it| format!("{it}\n"))
.collect()
.fold(String::new(), |mut acc, it| format_to_acc!(acc, "{it}\n"))
}

View File

@ -1267,8 +1267,7 @@ fn pattern_context_for(
pat
.syntax()
.ancestors()
.skip_while(|it| ast::Pat::can_cast(it.kind()))
.next()
.find(|it| !ast::Pat::can_cast(it.kind()))
.map_or((PatternRefutability::Irrefutable, false), |node| {
let refutability = match_ast! {
match node {

View File

@ -210,23 +210,14 @@ pub(crate) fn check_edit_with_config(
let mut combined_edit = completion.text_edit.clone();
resolve_completion_edits(
&db,
&config,
position,
completion
.import_to_add
.iter()
.cloned()
.filter_map(|(import_path, import_name)| Some((import_path, import_name))),
)
.into_iter()
.flatten()
.for_each(|text_edit| {
combined_edit.union(text_edit).expect(
"Failed to apply completion resolve changes: change ranges overlap, but should not",
)
});
resolve_completion_edits(&db, &config, position, completion.import_to_add.iter().cloned())
.into_iter()
.flatten()
.for_each(|text_edit| {
combined_edit.union(text_edit).expect(
"Failed to apply completion resolve changes: change ranges overlap, but should not",
)
});
combined_edit.apply(&mut actual);
assert_eq_text!(&ra_fixture_after, &actual)

View File

@ -467,8 +467,7 @@ fn recursive_normalize(use_tree: &ast::UseTree, style: NormalizationStyle) -> Op
}
ted::replace_all(start..=end, elements);
} else {
let new_use_tree_list =
make::use_tree_list(subtrees.into_iter()).clone_for_update();
let new_use_tree_list = make::use_tree_list(subtrees).clone_for_update();
ted::replace(use_tree_list.syntax(), new_use_tree_list.syntax());
}
modified = true;

View File

@ -120,6 +120,7 @@ pub(crate) fn hover(
Some(res)
}
#[allow(clippy::field_reassign_with_default)]
fn hover_simple(
sema: &Semantics<'_, RootDatabase>,
FilePosition { file_id, offset }: FilePosition,

View File

@ -184,7 +184,7 @@ impl ProcMacro {
.process
.lock()
.unwrap_or_else(|e| e.into_inner())
.send_task(msg::Request::ExpandMacro(task))?;
.send_task(msg::Request::ExpandMacro(Box::new(task)))?;
match response {
msg::Response::ExpandMacro(it) => {

View File

@ -29,7 +29,7 @@ pub enum Request {
/// Since [`NO_VERSION_CHECK_VERSION`]
ListMacros { dylib_path: PathBuf },
/// Since [`NO_VERSION_CHECK_VERSION`]
ExpandMacro(ExpandMacro),
ExpandMacro(Box<ExpandMacro>),
/// Since [`VERSION_CHECK_VERSION`]
ApiVersionCheck {},
/// Since [`RUST_ANALYZER_SPAN_SUPPORT`]

View File

@ -45,9 +45,11 @@ fn run() -> io::Result<()> {
msg::Response::ListMacros(srv.list_macros(&dylib_path))
}
msg::Request::ExpandMacro(task) => match srv.span_mode() {
msg::SpanMode::Id => msg::Response::ExpandMacro(srv.expand(task).map(|(it, _)| it)),
msg::SpanMode::Id => {
msg::Response::ExpandMacro(srv.expand(*task).map(|(it, _)| it))
}
msg::SpanMode::RustAnalyzer => msg::Response::ExpandMacroExtended(
srv.expand(task).map(|(tree, span_data_table)| msg::ExpandMacroExtended {
srv.expand(*task).map(|(tree, span_data_table)| msg::ExpandMacroExtended {
tree,
span_data_table,
}),

View File

@ -60,7 +60,7 @@ pub enum ProjectWorkspace {
cargo: CargoWorkspace,
build_scripts: WorkspaceBuildScripts,
sysroot: Result<Sysroot, Option<String>>,
rustc: Result<(CargoWorkspace, WorkspaceBuildScripts), Option<String>>,
rustc: Result<Box<(CargoWorkspace, WorkspaceBuildScripts)>, Option<String>>,
/// Holds cfg flags for the current target. We get those by running
/// `rustc --print cfg`.
///
@ -119,7 +119,7 @@ impl fmt::Debug for ProjectWorkspace {
.field("sysroot", &sysroot.is_ok())
.field(
"n_rustc_compiler_crates",
&rustc.as_ref().map_or(0, |(rc, _)| rc.packages().len()),
&rustc.as_ref().map(|a| a.as_ref()).map_or(0, |(rc, _)| rc.packages().len()),
)
.field("n_rustc_cfg", &rustc_cfg.len())
.field("n_cfg_overrides", &cfg_overrides.len())
@ -265,7 +265,7 @@ impl ProjectWorkspace {
cargo_toml.parent(),
&config.extra_env,
);
Ok((workspace, buildscripts))
Ok(Box::new((workspace, buildscripts)))
}
Err(e) => {
tracing::error!(
@ -603,7 +603,7 @@ impl ProjectWorkspace {
PackageRoot { is_local, include, exclude }
})
.chain(mk_sysroot(sysroot.as_ref(), Some(cargo.workspace_root())))
.chain(rustc.iter().flat_map(|(rustc, _)| {
.chain(rustc.iter().map(|a| a.as_ref()).flat_map(|(rustc, _)| {
rustc.packages().map(move |krate| PackageRoot {
is_local: false,
include: vec![rustc[krate].manifest.parent().to_path_buf()],
@ -631,7 +631,8 @@ impl ProjectWorkspace {
sysroot_package_len + project.n_crates()
}
ProjectWorkspace::Cargo { cargo, sysroot, rustc, .. } => {
let rustc_package_len = rustc.as_ref().map_or(0, |(it, _)| it.packages().len());
let rustc_package_len =
rustc.as_ref().map(|a| a.as_ref()).map_or(0, |(it, _)| it.packages().len());
let sysroot_package_len = sysroot.as_ref().map_or(0, |it| it.num_packages());
cargo.packages().len() + sysroot_package_len + rustc_package_len
}
@ -672,7 +673,7 @@ impl ProjectWorkspace {
target_layout,
} => cargo_to_crate_graph(
load,
rustc.as_ref().ok(),
rustc.as_ref().map(|a| a.as_ref()).ok(),
cargo,
sysroot.as_ref().ok(),
rustc_cfg.clone(),

View File

@ -58,12 +58,14 @@ impl flags::AnalysisStats {
Rand32::new(seed)
};
let mut cargo_config = CargoConfig::default();
cargo_config.sysroot = match self.no_sysroot {
true => None,
false => Some(RustLibSource::Discover),
let cargo_config = CargoConfig {
sysroot: match self.no_sysroot {
true => None,
false => Some(RustLibSource::Discover),
},
sysroot_query_metadata: self.query_sysroot_metadata,
..Default::default()
};
cargo_config.sysroot_query_metadata = self.query_sysroot_metadata;
let no_progress = &|_| ();
let mut db_load_sw = self.stop_watch();

View File

@ -13,8 +13,8 @@ use crate::cli::flags;
impl flags::Diagnostics {
pub fn run(self) -> anyhow::Result<()> {
let mut cargo_config = CargoConfig::default();
cargo_config.sysroot = Some(RustLibSource::Discover);
let cargo_config =
CargoConfig { sysroot: Some(RustLibSource::Discover), ..Default::default() };
let with_proc_macro_server = if let Some(p) = &self.proc_macro_srv {
let path = vfs::AbsPathBuf::assert(std::env::current_dir()?.join(p));
ProcMacroServerChoice::Explicit(path)

View File

@ -287,8 +287,8 @@ impl flags::Lsif {
pub fn run(self) -> anyhow::Result<()> {
eprintln!("Generating LSIF started...");
let now = Instant::now();
let mut cargo_config = CargoConfig::default();
cargo_config.sysroot = Some(RustLibSource::Discover);
let cargo_config =
CargoConfig { sysroot: Some(RustLibSource::Discover), ..Default::default() };
let no_progress = &|_| ();
let load_cargo_config = LoadCargoConfig {
load_out_dirs_from_check: true,

View File

@ -13,8 +13,8 @@ use crate::cli::{flags, full_name_of_item, Result};
impl flags::RunTests {
pub fn run(self) -> Result<()> {
let mut cargo_config = CargoConfig::default();
cargo_config.sysroot = Some(RustLibSource::Discover);
let cargo_config =
CargoConfig { sysroot: Some(RustLibSource::Discover), ..Default::default() };
let load_cargo_config = LoadCargoConfig {
load_out_dirs_from_check: true,
with_proc_macro_server: ProcMacroServerChoice::Sysroot,

View File

@ -59,8 +59,8 @@ impl Tester {
path.push("ra-rustc-test.rs");
let tmp_file = AbsPathBuf::try_from(path).unwrap();
std::fs::write(&tmp_file, "")?;
let mut cargo_config = CargoConfig::default();
cargo_config.sysroot = Some(RustLibSource::Discover);
let cargo_config =
CargoConfig { sysroot: Some(RustLibSource::Discover), ..Default::default() };
let workspace = ProjectWorkspace::DetachedFiles {
files: vec![tmp_file.clone()],
sysroot: Ok(Sysroot::discover(

View File

@ -10,8 +10,8 @@ use crate::cli::flags;
impl flags::Ssr {
pub fn run(self) -> anyhow::Result<()> {
use ide_db::base_db::SourceDatabaseExt;
let mut cargo_config = CargoConfig::default();
cargo_config.sysroot = Some(RustLibSource::Discover);
let cargo_config =
CargoConfig { sysroot: Some(RustLibSource::Discover), ..Default::default() };
let load_cargo_config = LoadCargoConfig {
load_out_dirs_from_check: true,
with_proc_macro_server: ProcMacroServerChoice::Sysroot,

View File

@ -32,6 +32,7 @@ use project_model::{
};
use rustc_hash::{FxHashMap, FxHashSet};
use serde::{de::DeserializeOwned, Deserialize};
use stdx::format_to_acc;
use vfs::{AbsPath, AbsPathBuf};
use crate::{
@ -1744,7 +1745,7 @@ impl Config {
}
pub fn main_loop_num_threads(&self) -> usize {
self.data.numThreads.unwrap_or(num_cpus::get_physical().try_into().unwrap_or(1))
self.data.numThreads.unwrap_or(num_cpus::get_physical())
}
pub fn typing_autoclose_angle(&self) -> bool {
@ -2563,14 +2564,13 @@ fn field_props(field: &str, ty: &str, doc: &[&str], default: &str) -> serde_json
#[cfg(test)]
fn manual(fields: &[(&'static str, &'static str, &[&str], &str)]) -> String {
fields
.iter()
.map(|(field, _ty, doc, default)| {
let name = format!("rust-analyzer.{}", field.replace('_', "."));
let doc = doc_comment_to_string(doc);
if default.contains('\n') {
format!(
r#"[[{name}]]{name}::
fields.iter().fold(String::new(), |mut acc, (field, _ty, doc, default)| {
let name = format!("rust-analyzer.{}", field.replace('_', "."));
let doc = doc_comment_to_string(doc);
if default.contains('\n') {
format_to_acc!(
acc,
r#"[[{name}]]{name}::
+
--
Default:
@ -2580,16 +2580,17 @@ Default:
{doc}
--
"#
)
} else {
format!("[[{name}]]{name} (default: `{default}`)::\n+\n--\n{doc}--\n")
}
})
.collect::<String>()
)
} else {
format_to_acc!(acc, "[[{name}]]{name} (default: `{default}`)::\n+\n--\n{doc}--\n")
}
})
}
fn doc_comment_to_string(doc: &[&str]) -> String {
doc.iter().map(|it| it.strip_prefix(' ').unwrap_or(it)).map(|it| format!("{it}\n")).collect()
doc.iter()
.map(|it| it.strip_prefix(' ').unwrap_or(it))
.fold(String::new(), |mut acc, it| format_to_acc!(acc, "{it}\n"))
}
#[cfg(test)]

View File

@ -312,16 +312,14 @@ fn completion_item(
set_score(&mut lsp_item, max_relevance, item.relevance);
if config.completion().enable_imports_on_the_fly && !item.import_to_add.is_empty() {
let imports: Vec<_> = item
let imports = item
.import_to_add
.into_iter()
.filter_map(|(import_path, import_name)| {
Some(lsp_ext::CompletionImport {
full_import_path: import_path,
imported_name: import_name,
})
.map(|(import_path, import_name)| lsp_ext::CompletionImport {
full_import_path: import_path,
imported_name: import_name,
})
.collect();
.collect::<Vec<_>>();
if !imports.is_empty() {
let data = lsp_ext::CompletionResolveData { position: tdpp.clone(), imports };
lsp_item.data = Some(to_value(data).unwrap());

View File

@ -31,6 +31,7 @@ use lsp_types::{
};
use rust_analyzer::lsp::ext::{OnEnter, Runnables, RunnablesParams};
use serde_json::json;
use stdx::format_to_acc;
use test_utils::skip_slow_tests;
use crate::{
@ -591,8 +592,10 @@ fn diagnostics_dont_block_typing() {
return;
}
let librs: String = (0..10).map(|i| format!("mod m{i};")).collect();
let libs: String = (0..10).map(|i| format!("//- /src/m{i}.rs\nfn foo() {{}}\n\n")).collect();
let librs: String = (0..10).fold(String::new(), |mut acc, i| format_to_acc!(acc, "mod m{i};"));
let libs: String = (0..10).fold(String::new(), |mut acc, i| {
format_to_acc!(acc, "//- /src/m{i}.rs\nfn foo() {{}}\n\n")
});
let server = Project::with_fixture(&format!(
r#"
//- /Cargo.toml

View File

@ -14,6 +14,22 @@ macro_rules! format_to {
};
}
/// Appends formatted string to a `String` and returns the `String`.
///
/// Useful for folding iterators into a `String`.
#[macro_export]
macro_rules! format_to_acc {
($buf:expr, $lit:literal $($arg:tt)*) => {
{
use ::std::fmt::Write as _;
// We can't do ::std::fmt::Write::write_fmt($buf, format_args!($lit $($arg)*))
// unfortunately, as that loses out on autoref behavior.
_ = $buf.write_fmt(format_args!($lit $($arg)*));
$buf
}
};
}
/// Generates `From` impls for `Enum E { Foo(Foo), Bar(Bar) }` enums
///
/// # Example

View File

@ -9,10 +9,11 @@
//! API should require to assemble every node piecewise. The trick of
//! `parse(format!())` we use internally is an implementation detail -- long
//! term, it will be replaced with direct tree manipulation.
use itertools::Itertools;
use parser::T;
use rowan::NodeOrToken;
use stdx::{format_to, never};
use stdx::{format_to, format_to_acc, never};
use crate::{ast, utils::is_raw_identifier, AstNode, SourceFile, SyntaxKind, SyntaxToken};
@ -759,15 +760,12 @@ pub fn match_arm_with_guard(
}
pub fn match_arm_list(arms: impl IntoIterator<Item = ast::MatchArm>) -> ast::MatchArmList {
let arms_str = arms
.into_iter()
.map(|arm| {
let needs_comma = arm.expr().map_or(true, |it| !it.is_block_like());
let comma = if needs_comma { "," } else { "" };
let arm = arm.syntax();
format!(" {arm}{comma}\n")
})
.collect::<String>();
let arms_str = arms.into_iter().fold(String::new(), |mut acc, arm| {
let needs_comma = arm.expr().map_or(true, |it| !it.is_block_like());
let comma = if needs_comma { "," } else { "" };
let arm = arm.syntax();
format_to_acc!(acc, " {arm}{comma}\n")
});
return from_text(&arms_str);
fn from_text(text: &str) -> ast::MatchArmList {

View File

@ -11,6 +11,7 @@ use std::{
use ast::HasName;
use expect_test::expect_file;
use rayon::prelude::*;
use stdx::format_to_acc;
use test_utils::{bench, bench_fixture, project_root};
use crate::{ast, fuzz, AstNode, SourceFile, SyntaxError};
@ -104,10 +105,9 @@ fn self_hosting_parsing() {
.collect::<Vec<_>>();
if !errors.is_empty() {
let errors = errors
.into_iter()
.map(|(path, err)| format!("{}: {:?}\n", path.display(), err[0]))
.collect::<String>();
let errors = errors.into_iter().fold(String::new(), |mut acc, (path, err)| {
format_to_acc!(acc, "{}: {:?}\n", path.display(), err[0])
});
panic!("Parsing errors:\n{errors}\n");
}
}