From ae7e55c1dd801c60092205ec8890179e10a47814 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Kr=C3=BCger?= Date: Sun, 21 Mar 2021 13:13:34 +0100 Subject: clippy::complexity simplifications related to Iterators --- crates/hir/src/lib.rs | 2 +- crates/hir_def/src/attr.rs | 2 +- crates/hir_ty/src/infer/pat.rs | 2 +- crates/ide/src/extend_selection.rs | 7 +- .../src/handlers/convert_comment_block.rs | 5 +- .../ide_assists/src/handlers/expand_glob_import.rs | 19 ++--- crates/ide_assists/src/handlers/reorder_impl.rs | 2 +- crates/ide_completion/src/patterns.rs | 2 +- crates/ide_ssr/src/parsing.rs | 2 +- crates/mbe/src/tests/expand.rs | 3 +- crates/project_model/src/build_data.rs | 91 ++++++++++------------ crates/syntax/src/ast/edit.rs | 3 +- 12 files changed, 62 insertions(+), 78 deletions(-) (limited to 'crates') diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index e34be7e42..44eaeffb9 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -213,7 +213,7 @@ impl Crate { Some(TokenTree::Leaf(Leaf::Literal(Literal{ref text, ..}))) => Some(text), _ => None } - }).flat_map(|t| t).next(); + }).flatten().next(); doc_url.map(|s| s.trim_matches('"').trim_end_matches('/').to_owned() + "/") } diff --git a/crates/hir_def/src/attr.rs b/crates/hir_def/src/attr.rs index 2c10f46d8..52a2bce9b 100644 --- a/crates/hir_def/src/attr.rs +++ b/crates/hir_def/src/attr.rs @@ -638,7 +638,7 @@ fn collect_attrs( owner: &dyn ast::AttrsOwner, ) -> impl Iterator> { let (inner_attrs, inner_docs) = inner_attributes(owner.syntax()) - .map_or((None, None), |(attrs, docs)| ((Some(attrs), Some(docs)))); + .map_or((None, None), |(attrs, docs)| (Some(attrs), Some(docs))); let outer_attrs = owner.attrs().filter(|attr| attr.excl_token().is_none()); let attrs = outer_attrs diff --git a/crates/hir_ty/src/infer/pat.rs b/crates/hir_ty/src/infer/pat.rs index befa0d69b..ec491648f 100644 --- a/crates/hir_ty/src/infer/pat.rs +++ b/crates/hir_ty/src/infer/pat.rs @@ -38,7 +38,7 @@ impl<'a> InferenceContext<'a> { let field_tys = def.map(|it| self.db.field_types(it)).unwrap_or_default(); let (pre, post) = match ellipsis { Some(idx) => subpats.split_at(idx), - None => (&subpats[..], &[][..]), + None => (subpats, &[][..]), }; let post_idx_offset = field_tys.iter().count() - post.len(); diff --git a/crates/ide/src/extend_selection.rs b/crates/ide/src/extend_selection.rs index e187243cb..5201ce587 100644 --- a/crates/ide/src/extend_selection.rs +++ b/crates/ide/src/extend_selection.rs @@ -263,11 +263,10 @@ fn extend_list_item(node: &SyntaxNode) -> Option { ) -> Option { node.siblings_with_tokens(dir) .skip(1) - .skip_while(|node| match node { - NodeOrToken::Node(_) => false, - NodeOrToken::Token(it) => is_single_line_ws(it), + .find(|node| match node { + NodeOrToken::Node(_) => true, + NodeOrToken::Token(it) => !is_single_line_ws(it), }) - .next() .and_then(|it| it.into_token()) .filter(|node| node.kind() == delimiter_kind) } diff --git a/crates/ide_assists/src/handlers/convert_comment_block.rs b/crates/ide_assists/src/handlers/convert_comment_block.rs index cdc45fc42..9dc3ee28f 100644 --- a/crates/ide_assists/src/handlers/convert_comment_block.rs +++ b/crates/ide_assists/src/handlers/convert_comment_block.rs @@ -1,5 +1,4 @@ use itertools::Itertools; -use std::convert::identity; use syntax::{ ast::{ self, @@ -140,7 +139,7 @@ fn relevant_line_comments(comment: &ast::Comment) -> Vec { .filter(|s| !skippable(s)) .map(|not| not.into_token().and_then(Comment::cast).filter(same_prefix)) .take_while(|opt_com| opt_com.is_some()) - .filter_map(identity) + .flatten() .skip(1); // skip the first element so we don't duplicate it in next_comments let next_comments = comment @@ -149,7 +148,7 @@ fn relevant_line_comments(comment: &ast::Comment) -> Vec { .filter(|s| !skippable(s)) .map(|not| not.into_token().and_then(Comment::cast).filter(same_prefix)) .take_while(|opt_com| opt_com.is_some()) - .filter_map(identity); + .flatten(); let mut comments: Vec<_> = prev_comments.collect(); comments.reverse(); diff --git a/crates/ide_assists/src/handlers/expand_glob_import.rs b/crates/ide_assists/src/handlers/expand_glob_import.rs index 83aa11d52..98389e4f7 100644 --- a/crates/ide_assists/src/handlers/expand_glob_import.rs +++ b/crates/ide_assists/src/handlers/expand_glob_import.rs @@ -136,18 +136,13 @@ impl Refs { .into_iter() .filter(|r| { if let Def::ModuleDef(ModuleDef::Trait(tr)) = r.def { - if tr - .items(ctx.db()) - .into_iter() - .find(|ai| { - if let AssocItem::Function(f) = *ai { - Def::ModuleDef(ModuleDef::Function(f)).is_referenced_in(ctx) - } else { - false - } - }) - .is_some() - { + if tr.items(ctx.db()).into_iter().any(|ai| { + if let AssocItem::Function(f) = ai { + Def::ModuleDef(ModuleDef::Function(f)).is_referenced_in(ctx) + } else { + false + } + }) { return true; } } diff --git a/crates/ide_assists/src/handlers/reorder_impl.rs b/crates/ide_assists/src/handlers/reorder_impl.rs index edf4b0bfe..f976e73ad 100644 --- a/crates/ide_assists/src/handlers/reorder_impl.rs +++ b/crates/ide_assists/src/handlers/reorder_impl.rs @@ -95,7 +95,7 @@ fn compute_method_ranks(path: &ast::Path, ctx: &AssistContext) -> Option None, }) .enumerate() - .map(|(idx, func)| ((func.name(ctx.db()).to_string(), idx))) + .map(|(idx, func)| (func.name(ctx.db()).to_string(), idx)) .collect(), ) } diff --git a/crates/ide_completion/src/patterns.rs b/crates/ide_completion/src/patterns.rs index cf5ef07b7..d82564381 100644 --- a/crates/ide_completion/src/patterns.rs +++ b/crates/ide_completion/src/patterns.rs @@ -71,7 +71,7 @@ fn test_has_block_expr_parent() { } pub(crate) fn has_bind_pat_parent(element: SyntaxElement) -> bool { - element.ancestors().find(|it| it.kind() == IDENT_PAT).is_some() + element.ancestors().any(|it| it.kind() == IDENT_PAT) } #[test] fn test_has_bind_pat_parent() { diff --git a/crates/ide_ssr/src/parsing.rs b/crates/ide_ssr/src/parsing.rs index 5ff25cb6d..5e757e701 100644 --- a/crates/ide_ssr/src/parsing.rs +++ b/crates/ide_ssr/src/parsing.rs @@ -67,7 +67,7 @@ impl ParsedRule { ) -> Result, SsrError> { let raw_pattern = pattern.as_rust_code(); let raw_template = template.map(|t| t.as_rust_code()); - let raw_template = raw_template.as_ref().map(|s| s.as_str()); + let raw_template = raw_template.as_deref(); let mut builder = RuleBuilder { placeholders_by_stand_in: pattern.placeholders_by_stand_in(), rules: Vec::new(), diff --git a/crates/mbe/src/tests/expand.rs b/crates/mbe/src/tests/expand.rs index 2cce62781..8951f3813 100644 --- a/crates/mbe/src/tests/expand.rs +++ b/crates/mbe/src/tests/expand.rs @@ -1225,8 +1225,7 @@ macro_rules! m { ) .expand_statements(r#"m!(C("0"))"#) .descendants() - .find(|token| token.kind() == ERROR) - .is_some()); + .any(|token| token.kind() == ERROR)); } #[test] diff --git a/crates/project_model/src/build_data.rs b/crates/project_model/src/build_data.rs index 728a258ea..f7050be4e 100644 --- a/crates/project_model/src/build_data.rs +++ b/crates/project_model/src/build_data.rs @@ -137,60 +137,53 @@ fn collect_from_workspace( let stdout = BufReader::new(child_stdout); let mut res = BuildDataMap::default(); - for message in cargo_metadata::Message::parse_stream(stdout) { - if let Ok(message) = message { - match message { - Message::BuildScriptExecuted(BuildScript { - package_id, - out_dir, - cfgs, - env, - .. - }) => { - let cfgs = { - let mut acc = Vec::new(); - for cfg in cfgs { - match cfg.parse::() { - Ok(it) => acc.push(it), - Err(err) => { - anyhow::bail!("invalid cfg from cargo-metadata: {}", err) - } - }; - } - acc - }; - let res = res.entry(package_id.repr.clone()).or_default(); - // cargo_metadata crate returns default (empty) path for - // older cargos, which is not absolute, so work around that. - if !out_dir.as_str().is_empty() { - let out_dir = AbsPathBuf::assert(PathBuf::from(out_dir.into_os_string())); - res.out_dir = Some(out_dir); - res.cfgs = cfgs; + for message in cargo_metadata::Message::parse_stream(stdout).flatten() { + match message { + Message::BuildScriptExecuted(BuildScript { + package_id, out_dir, cfgs, env, .. + }) => { + let cfgs = { + let mut acc = Vec::new(); + for cfg in cfgs { + match cfg.parse::() { + Ok(it) => acc.push(it), + Err(err) => { + anyhow::bail!("invalid cfg from cargo-metadata: {}", err) + } + }; } - - res.envs = env; + acc + }; + let res = res.entry(package_id.repr.clone()).or_default(); + // cargo_metadata crate returns default (empty) path for + // older cargos, which is not absolute, so work around that. + if !out_dir.as_str().is_empty() { + let out_dir = AbsPathBuf::assert(PathBuf::from(out_dir.into_os_string())); + res.out_dir = Some(out_dir); + res.cfgs = cfgs; } - Message::CompilerArtifact(message) => { - progress(format!("metadata {}", message.target.name)); - - if message.target.kind.contains(&"proc-macro".to_string()) { - let package_id = message.package_id; - // Skip rmeta file - if let Some(filename) = message.filenames.iter().find(|name| is_dylib(name)) - { - let filename = AbsPathBuf::assert(PathBuf::from(&filename)); - let res = res.entry(package_id.repr.clone()).or_default(); - res.proc_macro_dylib_path = Some(filename); - } + + res.envs = env; + } + Message::CompilerArtifact(message) => { + progress(format!("metadata {}", message.target.name)); + + if message.target.kind.contains(&"proc-macro".to_string()) { + let package_id = message.package_id; + // Skip rmeta file + if let Some(filename) = message.filenames.iter().find(|name| is_dylib(name)) { + let filename = AbsPathBuf::assert(PathBuf::from(&filename)); + let res = res.entry(package_id.repr.clone()).or_default(); + res.proc_macro_dylib_path = Some(filename); } } - Message::CompilerMessage(message) => { - progress(message.target.name.clone()); - } - Message::BuildFinished(_) => {} - Message::TextLine(_) => {} - _ => {} } + Message::CompilerMessage(message) => { + progress(message.target.name.clone()); + } + Message::BuildFinished(_) => {} + Message::TextLine(_) => {} + _ => {} } } diff --git a/crates/syntax/src/ast/edit.rs b/crates/syntax/src/ast/edit.rs index 80be8b79c..365de4463 100644 --- a/crates/syntax/src/ast/edit.rs +++ b/crates/syntax/src/ast/edit.rs @@ -461,8 +461,7 @@ impl ast::MatchArmList { let end = if let Some(comma) = start .siblings_with_tokens(Direction::Next) .skip(1) - .skip_while(|it| it.kind().is_trivia()) - .next() + .find(|it| !it.kind().is_trivia()) .filter(|it| it.kind() == T![,]) { comma -- cgit v1.2.3