10440: Fix Clippy warnings and replace some `if let`s with `match` r=Veykril a=arzg

I decided to try fixing a bunch of Clippy warnings. I am aware of this project’s opinion of Clippy (I have read both [rust-lang/clippy#5537](https://github.com/rust-lang/rust-clippy/issues/5537) and [rust-analyzer/rowan#57 (comment)](https://github.com/rust-analyzer/rowan/pull/57#discussion_r415676159)), so I totally understand if part of or the entirety of this PR is rejected. In particular, I can see how the semicolons and `if let` vs `match` commits provide comparatively little benefit when compared to the ensuing churn.

I tried to separate each kind of change into its own commit to make it easier to discard certain changes. I also only applied Clippy suggestions where I thought they provided a definite improvement to the code (apart from semicolons, which is IMO more of a formatting/consistency question than a linting question). In the end I accumulated a list of 28 Clippy lints I ignored entirely.

Sidenote: I should really have asked about this on Zulip before going through all 1,555 `if let`s in the codebase to decide which ones definitely look better as `match` :P

Co-authored-by: Aramis Razzaghipour <aramisnoah@gmail.com>
This commit is contained in:
bors[bot] 2021-10-05 08:58:40 +00:00 committed by GitHub
commit 86c534f244
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
95 changed files with 399 additions and 478 deletions

View file

@ -109,10 +109,9 @@ pub(crate) fn deref(
ty: InEnvironment<&Canonical<Ty>>,
) -> Option<Canonical<Ty>> {
let _p = profile::span("deref");
if let Some(derefed) = builtin_deref(&ty.goal.value) {
Some(Canonical { value: derefed, binders: ty.goal.binders.clone() })
} else {
deref_by_trait(db, krate, ty)
match builtin_deref(&ty.goal.value) {
Some(derefed) => Some(Canonical { value: derefed, binders: ty.goal.binders.clone() }),
None => deref_by_trait(db, krate, ty),
}
}

View file

@ -104,10 +104,9 @@ impl TyExt for Ty {
}
fn as_fn_def(&self, db: &dyn HirDatabase) -> Option<FunctionId> {
if let Some(CallableDefId::FunctionId(func)) = self.callable_def(db) {
Some(func)
} else {
None
match self.callable_def(db) {
Some(CallableDefId::FunctionId(func)) => Some(func),
Some(CallableDefId::StructId(_) | CallableDefId::EnumVariantId(_)) | None => None,
}
}
fn as_reference(&self) -> Option<(&Ty, Lifetime, Mutability)> {

View file

@ -105,10 +105,9 @@ impl IntRange {
#[inline]
fn from_range(lo: u128, hi: u128, scalar_ty: Scalar) -> IntRange {
if let Scalar::Bool = scalar_ty {
IntRange { range: lo..=hi }
} else {
unimplemented!()
match scalar_ty {
Scalar::Bool => IntRange { range: lo..=hi },
_ => unimplemented!(),
}
}

View file

@ -167,10 +167,9 @@ impl<'a> HirFormatter<'a> {
}
pub fn should_truncate(&self) -> bool {
if let Some(max_size) = self.max_size {
self.curr_size >= max_size
} else {
false
match self.max_size {
Some(max_size) => self.curr_size >= max_size,
None => false,
}
}

View file

@ -264,10 +264,9 @@ impl<'a> InferenceContext<'a> {
// collect explicitly written argument types
for arg_type in arg_types.iter() {
let arg_ty = if let Some(type_ref) = arg_type {
self.make_ty(type_ref)
} else {
self.table.new_type_var()
let arg_ty = match arg_type {
Some(type_ref) => self.make_ty(type_ref),
None => self.table.new_type_var(),
};
sig_tys.push(arg_ty);
}

View file

@ -204,10 +204,9 @@ impl<'a> InferenceContext<'a> {
} else {
BindingMode::convert(*mode)
};
let inner_ty = if let Some(subpat) = subpat {
self.infer_pat(*subpat, &expected, default_bm)
} else {
expected
let inner_ty = match subpat {
Some(subpat) => self.infer_pat(*subpat, &expected, default_bm),
None => expected,
};
let inner_ty = self.insert_type_vars_shallow(inner_ty);

View file

@ -324,10 +324,9 @@ impl<'a> InferenceTable<'a> {
/// Unify two types and register new trait goals that arise from that.
pub(crate) fn unify(&mut self, ty1: &Ty, ty2: &Ty) -> bool {
let result = if let Ok(r) = self.try_unify(ty1, ty2) {
r
} else {
return false;
let result = match self.try_unify(ty1, ty2) {
Ok(r) => r,
Err(_) => return false,
};
self.register_infer_ok(result);
true

View file

@ -369,10 +369,9 @@ impl<'a> TyLoweringContext<'a> {
Some((it, None)) => it,
_ => return None,
};
if let TypeNs::GenericParam(param_id) = resolution {
Some(param_id)
} else {
None
match resolution {
TypeNs::GenericParam(param_id) => Some(param_id),
_ => None,
}
}

View file

@ -82,10 +82,9 @@ impl TyFingerprint {
TyKind::Ref(_, _, ty) => return TyFingerprint::for_trait_impl(ty),
TyKind::Tuple(_, subst) => {
let first_ty = subst.interned().get(0).map(|arg| arg.assert_ty_ref(&Interner));
if let Some(ty) = first_ty {
return TyFingerprint::for_trait_impl(ty);
} else {
TyFingerprint::Unit
match first_ty {
Some(ty) => return TyFingerprint::for_trait_impl(ty),
None => TyFingerprint::Unit,
}
}
TyKind::AssociatedType(_, _)

View file

@ -195,10 +195,9 @@ fn check_impl(ra_fixture: &str, allow_none: bool, only_types: bool, display_sour
mismatch.expected.display_test(&db),
mismatch.actual.display_test(&db)
);
if let Some(annotation) = mismatches.remove(&range) {
assert_eq!(actual, annotation);
} else {
format_to!(unexpected_type_mismatches, "{:?}: {}\n", range.range, actual);
match mismatches.remove(&range) {
Some(annotation) => assert_eq!(actual, annotation),
None => format_to!(unexpected_type_mismatches, "{:?}: {}\n", range.range, actual),
}
}
for (expr, mismatch) in inference_result.expr_type_mismatches() {
@ -215,10 +214,9 @@ fn check_impl(ra_fixture: &str, allow_none: bool, only_types: bool, display_sour
mismatch.expected.display_test(&db),
mismatch.actual.display_test(&db)
);
if let Some(annotation) = mismatches.remove(&range) {
assert_eq!(actual, annotation);
} else {
format_to!(unexpected_type_mismatches, "{:?}: {}\n", range.range, actual);
match mismatches.remove(&range) {
Some(annotation) => assert_eq!(actual, annotation),
None => format_to!(unexpected_type_mismatches, "{:?}: {}\n", range.range, actual),
}
}
}