9165: Apply some clippy suggestions r=matklad a=clemenswasser



Co-authored-by: Clemens Wasser <clemens.wasser@gmail.com>
This commit is contained in:
bors[bot] 2021-06-21 15:11:21 +00:00 committed by GitHub
commit 56e61bdfea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 66 additions and 80 deletions

View File

@ -219,8 +219,7 @@ pub fn get_html_root_url(self: &Crate, db: &dyn HirDatabase) -> Option<String> {
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<Trait> {
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<I
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 @@ pub fn is_fn(&self) -> 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);

View File

@ -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;

View File

@ -15,12 +15,9 @@ fn lower(ra_fixture: &str) -> Arc<Body> {
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;
}
}
}

View File

@ -104,7 +104,7 @@ pub(crate) fn generic_params_query(
) -> Interned<GenericParams> {
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<SourceMap>) {

View File

@ -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

View File

@ -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)*); }
};
}

View File

@ -367,10 +367,7 @@ pub fn parent(&self) -> Option<ModuleId> {
pub fn containing_module(&self, local_mod: LocalModuleId) -> Option<ModuleId> {
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),
}
}

View File

@ -55,7 +55,7 @@ fn with(
segment_index: Option<usize>,
krate: Option<CrateId>,
) -> ResolvePathResult {
ResolvePathResult { resolved_def, reached_fixedpoint, segment_index, krate }
ResolvePathResult { resolved_def, segment_index, reached_fixedpoint, krate }
}
}

View File

@ -209,7 +209,7 @@ pub fn qualifier(&self) -> Option<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))
}
}

View File

@ -388,9 +388,9 @@ pub fn krate(&self) -> Option<CrateId> {
self.module_scope().map(|t| t.0.krate())
}
pub fn where_predicates_in_scope<'a>(
&'a self,
) -> impl Iterator<Item = &'a crate::generics::WherePredicate> + 'a {
pub fn where_predicates_in_scope(
&self,
) -> impl Iterator<Item = &crate::generics::WherePredicate> {
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()))
}
}

View File

@ -420,7 +420,7 @@ fn parse_string(tt: &tt::Subtree) -> Result<String, mbe::ExpandError> {
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))
})();

View File

@ -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(&macro_call.value);
let fragment = crate::to_fragment_kind(&macro_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()),

View File

@ -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,
};

View File

@ -241,6 +241,6 @@ fn push_fragment(buf: &mut Vec<tt::TokenTree>, fragment: Fragment) {
fn push_subtree(buf: &mut Vec<tt::TokenTree>, tt: tt::Subtree) {
match tt.delimiter {
None => buf.extend(tt.token_trees),
_ => buf.push(tt.into()),
Some(_) => buf.push(tt.into()),
}
}

View File

@ -135,7 +135,7 @@ fn max_id(subtree: &tt::Subtree) -> Option<u32> {
/// 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<MacroRules, ParseError> {
}
}
for rule in rules.iter() {
for rule in &rules {
validate(&rule.lhs)?;
}
@ -241,7 +241,7 @@ pub fn parse(tt: &tt::Subtree) -> Result<MacroDef, ParseError> {
}
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<Rule, ParseError> {
fn parse(src: &mut TtIter, expect_arrow: bool) -> Result<Self, ParseError> {
let lhs = src
.expect_subtree()
.map_err(|()| ParseError::Expected("expected subtree".to_string()))?;
@ -356,7 +356,7 @@ pub fn map<U>(self, f: impl FnOnce(T) -> U) -> ExpandResult<U> {
}
pub fn result(self) -> Result<T, ExpandError> {
self.err.map(Err).unwrap_or(Ok(self.value))
self.err.map_or(Ok(self.value), Err)
}
}

View File

@ -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,
}
}
}

View File

@ -283,7 +283,7 @@ fn collect_leaf(&mut self, result: &mut Vec<tt::TokenTree>) {
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() {

View File

@ -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![];

View File

@ -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) {