From 47747cd4120e260a144242aadd7732e11d133fe4 Mon Sep 17 00:00:00 2001 From: Clemens Wasser Date: Mon, 7 Jun 2021 13:59:01 +0200 Subject: Apply some clippy suggestions --- crates/hir/src/lib.rs | 42 ++++++++++++--------------- crates/hir_def/src/body/lower.rs | 7 ++--- crates/hir_def/src/body/tests.rs | 9 ++---- crates/hir_def/src/generics.rs | 5 ++-- crates/hir_def/src/item_scope.rs | 6 ++-- crates/hir_def/src/item_tree/pretty.rs | 6 ++-- crates/hir_def/src/nameres.rs | 5 +--- crates/hir_def/src/nameres/path_resolution.rs | 2 +- crates/hir_def/src/path.rs | 2 +- crates/hir_def/src/resolver.rs | 12 ++++---- crates/hir_expand/src/builtin_macro.rs | 7 ++--- crates/hir_expand/src/eager.rs | 6 ++-- crates/mbe/src/expander/matcher.rs | 8 ++--- crates/mbe/src/expander/transcriber.rs | 2 +- crates/mbe/src/lib.rs | 10 +++---- crates/mbe/src/subtree_source.rs | 2 +- crates/mbe/src/syntax_bridge.rs | 2 +- crates/mbe/src/tt_iter.rs | 9 +++--- crates/proc_macro_srv/src/rustc_server.rs | 4 +-- 19 files changed, 66 insertions(+), 80 deletions(-) (limited to 'crates') diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index b7eabaabb..88490fea9 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -219,8 +219,7 @@ impl Crate { let doc_url = doc_attr_q.tt_values().map(|tt| { let name = tt.token_trees.iter() .skip_while(|tt| !matches!(tt, TokenTree::Leaf(Leaf::Ident(Ident{text: ref ident, ..})) if ident == "html_root_url")) - .skip(2) - .next(); + .nth(2); match name { Some(TokenTree::Leaf(Leaf::Literal(Literal{ref text, ..}))) => Some(text), @@ -1846,7 +1845,7 @@ impl TypeParam { pub fn trait_bounds(self, db: &dyn HirDatabase) -> Vec { db.generic_predicates_for_param(self.id) - .into_iter() + .iter() .filter_map(|pred| match &pred.skip_binders().skip_binders() { hir_ty::WhereClause::Implemented(trait_ref) => { Some(Trait::from(trait_ref.hir_trait_id())) @@ -1951,7 +1950,7 @@ impl Impl { all.extend( db.inherent_impls_in_crate(id) .for_self_ty(&ty) - .into_iter() + .iter() .cloned() .map(Self::from) .filter(filter), @@ -2232,8 +2231,8 @@ impl Type { } pub fn is_packed(&self, db: &dyn HirDatabase) -> bool { - let adt_id = match self.ty.kind(&Interner) { - &TyKind::Adt(hir_ty::AdtId(adt_id), ..) => adt_id, + let adt_id = match *self.ty.kind(&Interner) { + TyKind::Adt(hir_ty::AdtId(adt_id), ..) => adt_id, _ => return false, }; @@ -2287,9 +2286,9 @@ impl Type { } pub fn fields(&self, db: &dyn HirDatabase) -> Vec<(Field, Type)> { - let (variant_id, substs) = match self.ty.kind(&Interner) { - &TyKind::Adt(hir_ty::AdtId(AdtId::StructId(s)), ref substs) => (s.into(), substs), - &TyKind::Adt(hir_ty::AdtId(AdtId::UnionId(u)), ref substs) => (u.into(), substs), + let (variant_id, substs) = match *self.ty.kind(&Interner) { + TyKind::Adt(hir_ty::AdtId(AdtId::StructId(s)), ref substs) => (s.into(), substs), + TyKind::Adt(hir_ty::AdtId(AdtId::UnionId(u)), ref substs) => (u.into(), substs), _ => return Vec::new(), }; @@ -2488,20 +2487,17 @@ impl Type { cb: &mut impl FnMut(Type), ) { for pred in bounds { - match pred.skip_binders() { - WhereClause::Implemented(trait_ref) => { - cb(type_.clone()); - // skip the self type. it's likely the type we just got the bounds from - for ty in trait_ref - .substitution - .iter(&Interner) - .skip(1) - .filter_map(|a| a.ty(&Interner)) - { - walk_type(db, &type_.derived(ty.clone()), cb); - } + if let WhereClause::Implemented(trait_ref) = pred.skip_binders() { + cb(type_.clone()); + // skip the self type. it's likely the type we just got the bounds from + for ty in trait_ref + .substitution + .iter(&Interner) + .skip(1) + .filter_map(|a| a.ty(&Interner)) + { + walk_type(db, &type_.derived(ty.clone()), cb); } - _ => (), } } } @@ -2514,7 +2510,7 @@ impl Type { walk_substs(db, type_, substs, cb); } TyKind::AssociatedType(_, substs) => { - if let Some(_) = ty.associated_type_parent_trait(db) { + if ty.associated_type_parent_trait(db).is_some() { cb(type_.derived(ty.clone())); } walk_substs(db, type_, substs, cb); diff --git a/crates/hir_def/src/body/lower.rs b/crates/hir_def/src/body/lower.rs index bed4c4994..f6e6cdbe2 100644 --- a/crates/hir_def/src/body/lower.rs +++ b/crates/hir_def/src/body/lower.rs @@ -690,9 +690,7 @@ impl ExprCollector<'_> { } } ast::Stmt::Item(item) => { - if self.check_cfg(&item).is_none() { - return; - } + self.check_cfg(&item); } } } @@ -717,7 +715,8 @@ impl ExprCollector<'_> { block.statements().for_each(|s| self.collect_stmt(s)); block.tail_expr().and_then(|e| { let expr = self.maybe_collect_expr(e)?; - Some(self.statements_in_scope.push(Statement::Expr { expr, has_semi: false })) + self.statements_in_scope.push(Statement::Expr { expr, has_semi: false }); + Some(()) }); let mut tail = None; diff --git a/crates/hir_def/src/body/tests.rs b/crates/hir_def/src/body/tests.rs index 27d837d47..673a75386 100644 --- a/crates/hir_def/src/body/tests.rs +++ b/crates/hir_def/src/body/tests.rs @@ -15,12 +15,9 @@ fn lower(ra_fixture: &str) -> Arc { let mut fn_def = None; 'outer: for (_, module) in def_map.modules() { for decl in module.scope.declarations() { - match decl { - ModuleDefId::FunctionId(it) => { - fn_def = Some(it); - break 'outer; - } - _ => {} + if let ModuleDefId::FunctionId(it) = decl { + fn_def = Some(it); + break 'outer; } } } diff --git a/crates/hir_def/src/generics.rs b/crates/hir_def/src/generics.rs index 0f04b2bae..096ac7968 100644 --- a/crates/hir_def/src/generics.rs +++ b/crates/hir_def/src/generics.rs @@ -104,7 +104,7 @@ impl GenericParams { ) -> Interned { let _p = profile::span("generic_params_query"); - let generics = match def { + match def { GenericDefId::FunctionId(id) => { let id = id.lookup(db).id; let tree = id.item_tree(db); @@ -150,8 +150,7 @@ impl GenericParams { GenericDefId::EnumVariantId(_) | GenericDefId::ConstId(_) => { Interned::new(GenericParams::default()) } - }; - generics + } } fn new(db: &dyn DefDatabase, def: GenericDefId) -> (GenericParams, InFile) { diff --git a/crates/hir_def/src/item_scope.rs b/crates/hir_def/src/item_scope.rs index 08407ebfa..567ae5660 100644 --- a/crates/hir_def/src/item_scope.rs +++ b/crates/hir_def/src/item_scope.rs @@ -241,10 +241,8 @@ impl ItemScope { check_changed!(changed, (self / def).values, glob_imports[lookup], def_import_type); check_changed!(changed, (self / def).macros, glob_imports[lookup], def_import_type); - if def.is_none() { - if self.unresolved.insert(lookup.1) { - changed = true; - } + if def.is_none() && self.unresolved.insert(lookup.1) { + changed = true; } changed diff --git a/crates/hir_def/src/item_tree/pretty.rs b/crates/hir_def/src/item_tree/pretty.rs index e63bc8232..8b12e5a67 100644 --- a/crates/hir_def/src/item_tree/pretty.rs +++ b/crates/hir_def/src/item_tree/pretty.rs @@ -30,16 +30,16 @@ pub(super) fn print_item_tree(tree: &ItemTree) -> String { macro_rules! w { ($dst:expr, $($arg:tt)*) => { - drop(write!($dst, $($arg)*)) + { let _ = write!($dst, $($arg)*); } }; } macro_rules! wln { ($dst:expr) => { - drop(writeln!($dst)) + { let _ = writeln!($dst); } }; ($dst:expr, $($arg:tt)*) => { - drop(writeln!($dst, $($arg)*)) + { let _ = writeln!($dst, $($arg)*); } }; } diff --git a/crates/hir_def/src/nameres.rs b/crates/hir_def/src/nameres.rs index ebfcc26c4..9e6aa9607 100644 --- a/crates/hir_def/src/nameres.rs +++ b/crates/hir_def/src/nameres.rs @@ -367,10 +367,7 @@ impl DefMap { pub fn containing_module(&self, local_mod: LocalModuleId) -> Option { match &self[local_mod].parent { Some(parent) => Some(self.module_id(*parent)), - None => match &self.block { - Some(block) => Some(block.parent), - None => None, - }, + None => self.block.as_ref().map(|block| block.parent), } } diff --git a/crates/hir_def/src/nameres/path_resolution.rs b/crates/hir_def/src/nameres/path_resolution.rs index 629bc7952..229f57d77 100644 --- a/crates/hir_def/src/nameres/path_resolution.rs +++ b/crates/hir_def/src/nameres/path_resolution.rs @@ -55,7 +55,7 @@ impl ResolvePathResult { segment_index: Option, krate: Option, ) -> ResolvePathResult { - ResolvePathResult { resolved_def, reached_fixedpoint, segment_index, krate } + ResolvePathResult { resolved_def, segment_index, reached_fixedpoint, krate } } } diff --git a/crates/hir_def/src/path.rs b/crates/hir_def/src/path.rs index 4cdb5913d..667092cd2 100644 --- a/crates/hir_def/src/path.rs +++ b/crates/hir_def/src/path.rs @@ -209,7 +209,7 @@ impl Path { pub fn is_self_type(&self) -> bool { self.type_anchor.is_none() - && self.generic_args == &[None] + && self.generic_args == [None] && self.mod_path.as_ident() == Some(&name!(Self)) } } diff --git a/crates/hir_def/src/resolver.rs b/crates/hir_def/src/resolver.rs index 49c573087..a11439c3b 100644 --- a/crates/hir_def/src/resolver.rs +++ b/crates/hir_def/src/resolver.rs @@ -388,9 +388,9 @@ impl Resolver { self.module_scope().map(|t| t.0.krate()) } - pub fn where_predicates_in_scope<'a>( - &'a self, - ) -> impl Iterator + 'a { + pub fn where_predicates_in_scope( + &self, + ) -> impl Iterator { self.scopes .iter() .rev() @@ -464,16 +464,16 @@ impl Scope { &Scope::GenericParams { ref params, def: parent } => { for (local_id, param) in params.types.iter() { if let Some(ref name) = param.name { - let id = TypeParamId { local_id, parent }; + let id = TypeParamId { parent, local_id }; f(name.clone(), ScopeDef::GenericParam(id.into())) } } for (local_id, param) in params.consts.iter() { - let id = ConstParamId { local_id, parent }; + let id = ConstParamId { parent, local_id }; f(param.name.clone(), ScopeDef::GenericParam(id.into())) } for (local_id, param) in params.lifetimes.iter() { - let id = LifetimeParamId { local_id, parent }; + let id = LifetimeParamId { parent, local_id }; f(param.name.clone(), ScopeDef::GenericParam(id.into())) } } diff --git a/crates/hir_expand/src/builtin_macro.rs b/crates/hir_expand/src/builtin_macro.rs index 4c83a2efe..f556bc751 100644 --- a/crates/hir_expand/src/builtin_macro.rs +++ b/crates/hir_expand/src/builtin_macro.rs @@ -420,7 +420,7 @@ fn parse_string(tt: &tt::Subtree) -> Result { tt::TokenTree::Leaf(tt::Leaf::Literal(it)) => unquote_str(it), _ => None, }) - .ok_or_else(|| mbe::ExpandError::ConversionError) + .ok_or(mbe::ExpandError::ConversionError) } fn include_expand( @@ -432,9 +432,8 @@ fn include_expand( let path = parse_string(tt)?; let file_id = relative_file(db, arg_id, &path, false)?; - let subtree = parse_to_token_tree(&db.file_text(file_id)) - .ok_or_else(|| mbe::ExpandError::ConversionError)? - .0; + let subtree = + parse_to_token_tree(&db.file_text(file_id)).ok_or(mbe::ExpandError::ConversionError)?.0; Ok((subtree, file_id)) })(); diff --git a/crates/hir_expand/src/eager.rs b/crates/hir_expand/src/eager.rs index 584ddcf9f..66601f4df 100644 --- a/crates/hir_expand/src/eager.rs +++ b/crates/hir_expand/src/eager.rs @@ -128,7 +128,7 @@ pub fn expand_eager_macro( }), kind: MacroCallKind::FnLike { ast_id: call_id, fragment: FragmentKind::Expr }, }); - let arg_file_id: MacroCallId = arg_id; + let arg_file_id = arg_id; let parsed_args = diagnostic_sink.result(mbe::token_tree_to_syntax_node(&parsed_args, FragmentKind::Expr))?.0; @@ -177,7 +177,7 @@ fn lazy_expand( let ast_id = db.ast_id_map(macro_call.file_id).ast_id(¯o_call.value); let fragment = crate::to_fragment_kind(¯o_call.value); - let id: MacroCallId = def.as_lazy_macro( + let id = def.as_lazy_macro( db, krate, MacroCallKind::FnLike { ast_id: macro_call.with_value(ast_id), fragment }, @@ -207,7 +207,7 @@ fn eager_macro_recur( .option_with(|| macro_resolver(child.path()?), || err("failed to resolve macro"))?; let insert = match def.kind { MacroDefKind::BuiltInEager(..) => { - let id: MacroCallId = expand_eager_macro( + let id = expand_eager_macro( db, krate, curr.with_value(child.clone()), diff --git a/crates/mbe/src/expander/matcher.rs b/crates/mbe/src/expander/matcher.rs index b4f2fe9a4..0d694b1a7 100644 --- a/crates/mbe/src/expander/matcher.rs +++ b/crates/mbe/src/expander/matcher.rs @@ -645,7 +645,7 @@ fn match_loop(pattern: &MetaTemplate, src: &tt::Subtree) -> Match { None if match_res.err.is_none() => { bindings_builder.push_optional(&mut item.bindings, name); } - _ => {} + None => {} } if let Some(err) = match_res.err { res.add_err(err); @@ -756,7 +756,7 @@ impl<'a> TtIter<'a> { let ok = match separator { Separator::Ident(lhs) if idx == 0 => match fork.expect_ident_or_underscore() { Ok(rhs) => rhs.text == lhs.text, - _ => false, + Err(_) => false, }, Separator::Literal(lhs) if idx == 0 => match fork.expect_literal() { Ok(rhs) => match rhs { @@ -764,11 +764,11 @@ impl<'a> TtIter<'a> { tt::Leaf::Ident(rhs) => rhs.text == lhs.text, tt::Leaf::Punct(_) => false, }, - _ => false, + Err(_) => false, }, Separator::Puncts(lhss) if idx < lhss.len() => match fork.expect_punct() { Ok(rhs) => rhs.char == lhss[idx].char, - _ => false, + Err(_) => false, }, _ => false, }; diff --git a/crates/mbe/src/expander/transcriber.rs b/crates/mbe/src/expander/transcriber.rs index 49a137577..4894e2a0c 100644 --- a/crates/mbe/src/expander/transcriber.rs +++ b/crates/mbe/src/expander/transcriber.rs @@ -241,6 +241,6 @@ fn push_fragment(buf: &mut Vec, fragment: Fragment) { fn push_subtree(buf: &mut Vec, tt: tt::Subtree) { match tt.delimiter { None => buf.extend(tt.token_trees), - _ => buf.push(tt.into()), + Some(_) => buf.push(tt.into()), } } diff --git a/crates/mbe/src/lib.rs b/crates/mbe/src/lib.rs index 8c8528aaf..fcc596756 100644 --- a/crates/mbe/src/lib.rs +++ b/crates/mbe/src/lib.rs @@ -135,7 +135,7 @@ impl Shift { /// Shift given TokenTree token id fn shift_all(self, tt: &mut tt::Subtree) { - for t in tt.token_trees.iter_mut() { + for t in &mut tt.token_trees { match t { tt::TokenTree::Leaf(leaf) => match leaf { tt::Leaf::Ident(ident) => ident.id = self.shift(ident.id), @@ -188,7 +188,7 @@ impl MacroRules { } } - for rule in rules.iter() { + for rule in &rules { validate(&rule.lhs)?; } @@ -241,7 +241,7 @@ impl MacroDef { } rules.push(rule); } - for rule in rules.iter() { + for rule in &rules { validate(&rule.lhs)?; } @@ -268,7 +268,7 @@ impl MacroDef { } impl Rule { - fn parse(src: &mut TtIter, expect_arrow: bool) -> Result { + fn parse(src: &mut TtIter, expect_arrow: bool) -> Result { let lhs = src .expect_subtree() .map_err(|()| ParseError::Expected("expected subtree".to_string()))?; @@ -356,7 +356,7 @@ impl ExpandResult { } pub fn result(self) -> Result { - self.err.map(Err).unwrap_or(Ok(self.value)) + self.err.map_or(Ok(self.value), Err) } } diff --git a/crates/mbe/src/subtree_source.rs b/crates/mbe/src/subtree_source.rs index ee80807ad..ffc2a6017 100644 --- a/crates/mbe/src/subtree_source.rs +++ b/crates/mbe/src/subtree_source.rs @@ -115,7 +115,7 @@ impl<'a> TokenSource for SubtreeTokenSource { fn is_keyword(&self, kw: &str) -> bool { match self.cached.get(self.curr.1) { Some(t) => t.text == *kw, - _ => false, + None => false, } } } diff --git a/crates/mbe/src/syntax_bridge.rs b/crates/mbe/src/syntax_bridge.rs index cdc22425d..7526bd8e6 100644 --- a/crates/mbe/src/syntax_bridge.rs +++ b/crates/mbe/src/syntax_bridge.rs @@ -283,7 +283,7 @@ trait TokenConvertor { let (id, idx) = self.id_alloc().open_delim(range); subtree.delimiter = Some(tt::Delimiter { id, kind }); - while self.peek().map(|it| it.kind() != closed).unwrap_or(false) { + while self.peek().map_or(false, |it| it.kind() != closed) { self.collect_leaf(&mut subtree.token_trees); } let last_range = match self.bump() { diff --git a/crates/mbe/src/tt_iter.rs b/crates/mbe/src/tt_iter.rs index 5a4eca7bf..65da83476 100644 --- a/crates/mbe/src/tt_iter.rs +++ b/crates/mbe/src/tt_iter.rs @@ -121,10 +121,11 @@ impl<'a> TtIter<'a> { parser::parse_fragment(&mut src, &mut sink, fragment_kind); - let mut err = None; - if !sink.cursor.is_root() || sink.error { - err = Some(err!("expected {:?}", fragment_kind)); - } + let mut err = if !sink.cursor.is_root() || sink.error { + Some(err!("expected {:?}", fragment_kind)) + } else { + None + }; let mut curr = buffer.begin(); let mut res = vec![]; diff --git a/crates/proc_macro_srv/src/rustc_server.rs b/crates/proc_macro_srv/src/rustc_server.rs index 65ca3eb6c..e252e89a5 100644 --- a/crates/proc_macro_srv/src/rustc_server.rs +++ b/crates/proc_macro_srv/src/rustc_server.rs @@ -248,7 +248,7 @@ pub mod token_stream { token_trees: subtree .token_trees .into_iter() - .map(|t| token_tree_replace_token_ids_with_unspecified(t)) + .map(token_tree_replace_token_ids_with_unspecified) .collect(), } } @@ -457,7 +457,7 @@ impl server::Group for Rustc { } fn span(&mut self, group: &Self::Group) -> Self::Span { - group.delimiter.map(|it| it.id).unwrap_or_else(|| tt::TokenId::unspecified()) + group.delimiter.map(|it| it.id).unwrap_or_else(tt::TokenId::unspecified) } fn set_span(&mut self, _group: &mut Self::Group, _span: Self::Span) { -- cgit v1.2.3