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
95 changed files with 399 additions and 478 deletions

View File

@@ -192,10 +192,9 @@ impl FunctionTemplate {
Some(cap) => {
let cursor = if self.should_focus_return_type {
// Focus the return type if there is one
if let Some(ref ret_type) = self.ret_type {
ret_type.syntax()
} else {
self.tail_expr.syntax()
match self.ret_type {
Some(ref ret_type) => ret_type.syntax(),
None => self.tail_expr.syntax(),
}
} else {
self.tail_expr.syntax()
@@ -428,10 +427,9 @@ fn fn_args(
arg_types.push(match fn_arg_type(ctx, target_module, &arg) {
Some(ty) => {
if !ty.is_empty() && ty.starts_with('&') {
if let Some((new_ty, _)) = useless_type_special_case("", &ty[1..].to_owned()) {
new_ty
} else {
ty
match useless_type_special_case("", &ty[1..].to_owned()) {
Some((new_ty, _)) => new_ty,
None => ty,
}
} else {
ty
@@ -523,11 +521,7 @@ fn fn_arg_type(
return None;
}
if let Ok(rendered) = ty.display_source_code(ctx.db(), target_module.into()) {
Some(rendered)
} else {
None
}
ty.display_source_code(ctx.db(), target_module.into()).ok()
}
/// Returns the position inside the current mod or file
@@ -560,20 +554,14 @@ fn next_space_for_fn_in_module(
) -> Option<(FileId, GeneratedFunctionTarget)> {
let file = module_source.file_id.original_file(db);
let assist_item = match &module_source.value {
hir::ModuleSource::SourceFile(it) => {
if let Some(last_item) = it.items().last() {
GeneratedFunctionTarget::BehindItem(last_item.syntax().clone())
} else {
GeneratedFunctionTarget::BehindItem(it.syntax().clone())
}
}
hir::ModuleSource::Module(it) => {
if let Some(last_item) = it.item_list().and_then(|it| it.items().last()) {
GeneratedFunctionTarget::BehindItem(last_item.syntax().clone())
} else {
GeneratedFunctionTarget::InEmptyItemList(it.item_list()?.syntax().clone())
}
}
hir::ModuleSource::SourceFile(it) => match it.items().last() {
Some(last_item) => GeneratedFunctionTarget::BehindItem(last_item.syntax().clone()),
None => GeneratedFunctionTarget::BehindItem(it.syntax().clone()),
},
hir::ModuleSource::Module(it) => match it.item_list().and_then(|it| it.items().last()) {
Some(last_item) => GeneratedFunctionTarget::BehindItem(last_item.syntax().clone()),
None => GeneratedFunctionTarget::InEmptyItemList(it.item_list()?.syntax().clone()),
},
hir::ModuleSource::BlockExpr(it) => {
if let Some(last_item) =
it.statements().take_while(|stmt| matches!(stmt, ast::Stmt::Item(_))).last()