mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-11 12:48:47 +00:00
clippy::complexity simplifications related to Iterators
This commit is contained in:
parent
bd407a9882
commit
ae7e55c1dd
12 changed files with 64 additions and 80 deletions
|
@ -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() + "/")
|
||||
}
|
||||
|
|
|
@ -638,7 +638,7 @@ fn collect_attrs(
|
|||
owner: &dyn ast::AttrsOwner,
|
||||
) -> impl Iterator<Item = Either<ast::Attr, ast::Comment>> {
|
||||
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
|
||||
|
|
|
@ -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();
|
||||
|
||||
|
|
|
@ -263,11 +263,10 @@ fn extend_list_item(node: &SyntaxNode) -> Option<TextRange> {
|
|||
) -> Option<SyntaxToken> {
|
||||
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)
|
||||
}
|
||||
|
|
|
@ -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<Comment> {
|
|||
.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<Comment> {
|
|||
.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();
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -95,7 +95,7 @@ fn compute_method_ranks(path: &ast::Path, ctx: &AssistContext) -> Option<FxHashM
|
|||
_ => None,
|
||||
})
|
||||
.enumerate()
|
||||
.map(|(idx, func)| ((func.name(ctx.db()).to_string(), idx)))
|
||||
.map(|(idx, func)| (func.name(ctx.db()).to_string(), idx))
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -67,7 +67,7 @@ impl ParsedRule {
|
|||
) -> Result<Vec<ParsedRule>, 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(),
|
||||
|
|
|
@ -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]
|
||||
|
|
|
@ -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::<CfgFlag>() {
|
||||
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::<CfgFlag>() {
|
||||
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;
|
||||
}
|
||||
|
||||
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(_) => {}
|
||||
_ => {}
|
||||
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(_) => {}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue