Apply some clippy suggestions

This commit is contained in:
Clemens Wasser 2021-06-07 13:59:01 +02:00
parent 4402f2b280
commit 47747cd412
19 changed files with 66 additions and 80 deletions

View File

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

View File

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

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

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 @@ impl DefMap {
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 @@ impl ResolvePathResult {
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 @@ 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))
}
}

View File

@ -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<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 @@ 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()))
}
}

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

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 @@ 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<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 @@ impl<T> ExpandResult<T> {
}
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 @@ 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,
}
}
}

View File

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

View File

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

View File

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