From 47747cd4120e260a144242aadd7732e11d133fe4 Mon Sep 17 00:00:00 2001 From: Clemens Wasser Date: Mon, 7 Jun 2021 13:59:01 +0200 Subject: [PATCH] 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(-) diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index b7eabaabbb0..88490fea98f 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -219,8 +219,7 @@ pub fn get_html_root_url(self: &Crate, db: &dyn HirDatabase) -> Option { 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 @@ pub fn ty(self, db: &dyn HirDatabase) -> Type { 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 @@ pub fn all_for_type(db: &dyn HirDatabase, Type { krate, ty, .. }: Type) -> Vec bool { } 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 @@ fn go(ty: &Ty) -> bool { } 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 @@ fn walk_bounds( 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 @@ fn walk_type(db: &dyn HirDatabase, type_: &Type, cb: &mut impl FnMut(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 bed4c49941c..f6e6cdbe2e3 100644 --- a/crates/hir_def/src/body/lower.rs +++ b/crates/hir_def/src/body/lower.rs @@ -690,9 +690,7 @@ fn collect_stmt(&mut self, s: ast::Stmt) { } } ast::Stmt::Item(item) => { - if self.check_cfg(&item).is_none() { - return; - } + self.check_cfg(&item); } } } @@ -717,7 +715,8 @@ fn collect_block(&mut self, block: ast::BlockExpr) -> ExprId { 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 27d837d47f4..673a7538616 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 0f04b2bae29..096ac796850 100644 --- a/crates/hir_def/src/generics.rs +++ b/crates/hir_def/src/generics.rs @@ -104,7 +104,7 @@ pub(crate) fn generic_params_query( ) -> 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 @@ pub(crate) fn generic_params_query( 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 08407ebfa64..567ae566034 100644 --- a/crates/hir_def/src/item_scope.rs +++ b/crates/hir_def/src/item_scope.rs @@ -241,10 +241,8 @@ macro_rules! check_changed { 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 e63bc8232b9..8b12e5a67cb 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 ebfcc26c413..9e6aa96075b 100644 --- a/crates/hir_def/src/nameres.rs +++ b/crates/hir_def/src/nameres.rs @@ -367,10 +367,7 @@ pub fn parent(&self) -> Option { 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 629bc7952c2..229f57d7793 100644 --- a/crates/hir_def/src/nameres/path_resolution.rs +++ b/crates/hir_def/src/nameres/path_resolution.rs @@ -55,7 +55,7 @@ fn with( 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 4cdb5913deb..667092cd208 100644 --- a/crates/hir_def/src/path.rs +++ b/crates/hir_def/src/path.rs @@ -209,7 +209,7 @@ pub fn qualifier(&self) -> Option { 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 49c5730877b..a11439c3b54 100644 --- a/crates/hir_def/src/resolver.rs +++ b/crates/hir_def/src/resolver.rs @@ -388,9 +388,9 @@ pub fn krate(&self) -> Option { 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 @@ fn process_names(&self, db: &dyn DefDatabase, f: &mut dyn FnMut(Name, ScopeDef)) &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 4c83a2efe37..f556bc751db 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 584ddcf9f60..66601f4df37 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 b4f2fe9a4a7..0d694b1a782 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 @@ fn expect_separator(&mut self, separator: &Separator, idx: usize) -> bool { 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 @@ fn expect_separator(&mut self, separator: &Separator, idx: usize) -> bool { 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 49a1375778f..4894e2a0c37 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 8c8528aafe1..fcc596756ca 100644 --- a/crates/mbe/src/lib.rs +++ b/crates/mbe/src/lib.rs @@ -135,7 +135,7 @@ fn max_id(subtree: &tt::Subtree) -> Option { /// 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 @@ pub fn parse(tt: &tt::Subtree) -> Result { } } - for rule in rules.iter() { + for rule in &rules { validate(&rule.lhs)?; } @@ -241,7 +241,7 @@ pub fn parse(tt: &tt::Subtree) -> Result { } rules.push(rule); } - for rule in rules.iter() { + for rule in &rules { validate(&rule.lhs)?; } @@ -268,7 +268,7 @@ pub fn map_id_up(&self, id: tt::TokenId) -> (tt::TokenId, Origin) { } 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 @@ pub fn map(self, f: impl FnOnce(T) -> U) -> 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 ee80807ad42..ffc2a60172b 100644 --- a/crates/mbe/src/subtree_source.rs +++ b/crates/mbe/src/subtree_source.rs @@ -115,7 +115,7 @@ fn bump(&mut self) { 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 cdc22425d9a..7526bd8e6b5 100644 --- a/crates/mbe/src/syntax_bridge.rs +++ b/crates/mbe/src/syntax_bridge.rs @@ -283,7 +283,7 @@ fn collect_leaf(&mut self, result: &mut Vec) { 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 5a4eca7bf00..65da834765e 100644 --- a/crates/mbe/src/tt_iter.rs +++ b/crates/mbe/src/tt_iter.rs @@ -121,10 +121,11 @@ fn error(&mut self, _error: parser::ParseError) { 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 65ca3eb6c82..e252e89a569 100644 --- a/crates/proc_macro_srv/src/rustc_server.rs +++ b/crates/proc_macro_srv/src/rustc_server.rs @@ -248,7 +248,7 @@ fn subtree_replace_token_ids_with_unspecified(subtree: tt::Subtree) -> tt::Subtr 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 @@ fn stream(&mut self, group: &Self::Group) -> Self::TokenStream { } 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) {