Skip to content

Commit 86c534f

Browse files
Merge #10440
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](rust-lang/rust-clippy#5537) and [rust-analyzer/rowan#57 (comment)](rust-analyzer/rowan#57 (comment))), 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 <[email protected]>
2 parents bf81723 + 9583dd5 commit 86c534f

File tree

95 files changed

+399
-478
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+399
-478
lines changed

crates/flycheck/src/lib.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ impl FlycheckActor {
179179
tracing::error!(
180180
"Flycheck failed to run the following command: {:?}",
181181
self.check_command()
182-
)
182+
);
183183
}
184184
self.progress(Progress::DidFinish(res));
185185
}
@@ -253,14 +253,14 @@ impl FlycheckActor {
253253
}
254254

255255
fn send(&self, check_task: Message) {
256-
(self.sender)(check_task)
256+
(self.sender)(check_task);
257257
}
258258
}
259259

260260
struct CargoHandle {
261261
child: JodChild,
262262
#[allow(unused)]
263-
thread: jod_thread::JoinHandle<io::Result<bool>>,
263+
thread: jod_thread::JoinHandle<bool>,
264264
receiver: Receiver<CargoMessage>,
265265
}
266266

@@ -279,7 +279,7 @@ impl CargoHandle {
279279
// It is okay to ignore the result, as it only errors if the process is already dead
280280
let _ = self.child.kill();
281281
let exit_status = self.child.wait()?;
282-
let read_at_least_one_message = self.thread.join()?;
282+
let read_at_least_one_message = self.thread.join();
283283
if !exit_status.success() && !read_at_least_one_message {
284284
// FIXME: Read the stderr to display the reason, see `read2()` reference in PR comment:
285285
// https://github.com/rust-analyzer/rust-analyzer/pull/3632#discussion_r395605298
@@ -304,7 +304,7 @@ impl CargoActor {
304304
fn new(child_stdout: process::ChildStdout, sender: Sender<CargoMessage>) -> CargoActor {
305305
CargoActor { child_stdout, sender }
306306
}
307-
fn run(self) -> io::Result<bool> {
307+
fn run(self) -> bool {
308308
// We manually read a line at a time, instead of using serde's
309309
// stream deserializers, because the deserializer cannot recover
310310
// from an error, resulting in it getting stuck, because we try to
@@ -334,20 +334,20 @@ impl CargoActor {
334334
// Skip certain kinds of messages to only spend time on what's useful
335335
JsonMessage::Cargo(message) => match message {
336336
cargo_metadata::Message::CompilerArtifact(artifact) if !artifact.fresh => {
337-
self.sender.send(CargoMessage::CompilerArtifact(artifact)).unwrap()
337+
self.sender.send(CargoMessage::CompilerArtifact(artifact)).unwrap();
338338
}
339339
cargo_metadata::Message::CompilerMessage(msg) => {
340-
self.sender.send(CargoMessage::Diagnostic(msg.message)).unwrap()
340+
self.sender.send(CargoMessage::Diagnostic(msg.message)).unwrap();
341341
}
342342
_ => (),
343343
},
344344
JsonMessage::Rustc(message) => {
345-
self.sender.send(CargoMessage::Diagnostic(message)).unwrap()
345+
self.sender.send(CargoMessage::Diagnostic(message)).unwrap();
346346
}
347347
}
348348
}
349349
}
350-
Ok(read_at_least_one_message)
350+
read_at_least_one_message
351351
}
352352
}
353353

crates/hir/src/lib.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -1119,7 +1119,7 @@ impl DefWithBody {
11191119
if let ast::Expr::RecordExpr(record_expr) =
11201120
&source_ptr.value.to_node(&root)
11211121
{
1122-
if let Some(_) = record_expr.record_expr_field_list() {
1122+
if record_expr.record_expr_field_list().is_some() {
11231123
acc.push(
11241124
MissingFields {
11251125
file: source_ptr.file_id,
@@ -1143,7 +1143,7 @@ impl DefWithBody {
11431143
if let Some(expr) = source_ptr.value.as_ref().left() {
11441144
let root = source_ptr.file_syntax(db.upcast());
11451145
if let ast::Pat::RecordPat(record_pat) = expr.to_node(&root) {
1146-
if let Some(_) = record_pat.record_pat_field_list() {
1146+
if record_pat.record_pat_field_list().is_some() {
11471147
acc.push(
11481148
MissingFields {
11491149
file: source_ptr.file_id,
@@ -2119,10 +2119,9 @@ impl Impl {
21192119
};
21202120

21212121
let fp = TyFingerprint::for_inherent_impl(&ty);
2122-
let fp = if let Some(fp) = fp {
2123-
fp
2124-
} else {
2125-
return Vec::new();
2122+
let fp = match fp {
2123+
Some(fp) => fp,
2124+
None => return Vec::new(),
21262125
};
21272126

21282127
let mut all = Vec::new();

crates/hir_def/src/body/lower.rs

+12-16
Original file line numberDiff line numberDiff line change
@@ -474,10 +474,9 @@ impl ExprCollector<'_> {
474474
}
475475
ast::Expr::PrefixExpr(e) => {
476476
let expr = self.collect_expr_opt(e.expr());
477-
if let Some(op) = e.op_kind() {
478-
self.alloc_expr(Expr::UnaryOp { expr, op }, syntax_ptr)
479-
} else {
480-
self.alloc_expr(Expr::Missing, syntax_ptr)
477+
match e.op_kind() {
478+
Some(op) => self.alloc_expr(Expr::UnaryOp { expr, op }, syntax_ptr),
479+
None => self.alloc_expr(Expr::Missing, syntax_ptr),
481480
}
482481
}
483482
ast::Expr::ClosureExpr(e) => {
@@ -624,10 +623,9 @@ impl ExprCollector<'_> {
624623
}
625624

626625
fn collect_expr_opt(&mut self, expr: Option<ast::Expr>) -> ExprId {
627-
if let Some(expr) = expr {
628-
self.collect_expr(expr)
629-
} else {
630-
self.missing_expr()
626+
match expr {
627+
Some(expr) => self.collect_expr(expr),
628+
None => self.missing_expr(),
631629
}
632630
}
633631

@@ -724,10 +722,9 @@ impl ExprCollector<'_> {
724722
}
725723

726724
fn collect_block_opt(&mut self, expr: Option<ast::BlockExpr>) -> ExprId {
727-
if let Some(block) = expr {
728-
self.collect_block(block)
729-
} else {
730-
self.missing_expr()
725+
match expr {
726+
Some(block) => self.collect_block(block),
727+
None => self.missing_expr(),
731728
}
732729
}
733730

@@ -890,10 +887,9 @@ impl ExprCollector<'_> {
890887
}
891888

892889
fn collect_pat_opt(&mut self, pat: Option<ast::Pat>) -> PatId {
893-
if let Some(pat) = pat {
894-
self.collect_pat(pat)
895-
} else {
896-
self.missing_pat()
890+
match pat {
891+
Some(pat) => self.collect_pat(pat),
892+
None => self.missing_pat(),
897893
}
898894
}
899895

crates/hir_def/src/find_path.rs

+16-21
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,9 @@ fn find_path_inner(
209209
) {
210210
path.push_segment(name);
211211

212-
let new_path = if let Some(best_path) = best_path {
213-
select_best_path(best_path, path, prefer_no_std)
214-
} else {
215-
path
212+
let new_path = match best_path {
213+
Some(best_path) => select_best_path(best_path, path, prefer_no_std),
214+
None => path,
216215
};
217216
best_path_len = new_path.len();
218217
best_path = Some(new_path);
@@ -243,10 +242,9 @@ fn find_path_inner(
243242
});
244243

245244
for path in extern_paths {
246-
let new_path = if let Some(best_path) = best_path {
247-
select_best_path(best_path, path, prefer_no_std)
248-
} else {
249-
path
245+
let new_path = match best_path {
246+
Some(best_path) => select_best_path(best_path, path, prefer_no_std),
247+
None => path,
250248
};
251249
best_path = Some(new_path);
252250
}
@@ -261,12 +259,11 @@ fn find_path_inner(
261259
}
262260
}
263261

264-
if let Some(prefix) = prefixed.map(PrefixKind::prefix) {
265-
best_path.or_else(|| {
262+
match prefixed.map(PrefixKind::prefix) {
263+
Some(prefix) => best_path.or_else(|| {
266264
scope_name.map(|scope_name| ModPath::from_segments(prefix, vec![scope_name]))
267-
})
268-
} else {
269-
best_path
265+
}),
266+
None => best_path,
270267
}
271268
}
272269

@@ -346,15 +343,13 @@ fn find_local_import_locations(
346343

347344
if let Some((name, vis)) = data.scope.name_of(item) {
348345
if vis.is_visible_from(db, from) {
349-
let is_private = if let Visibility::Module(private_to) = vis {
350-
private_to.local_id == module.local_id
351-
} else {
352-
false
346+
let is_private = match vis {
347+
Visibility::Module(private_to) => private_to.local_id == module.local_id,
348+
Visibility::Public => false,
353349
};
354-
let is_original_def = if let Some(module_def_id) = item.as_module_def_id() {
355-
data.scope.declarations().any(|it| it == module_def_id)
356-
} else {
357-
false
350+
let is_original_def = match item.as_module_def_id() {
351+
Some(module_def_id) => data.scope.declarations().any(|it| it == module_def_id),
352+
None => false,
358353
};
359354

360355
// Ignore private imports. these could be used if we are

crates/hir_def/src/item_tree.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -475,10 +475,9 @@ macro_rules! mod_items {
475475
}
476476

477477
fn id_from_mod_item(mod_item: ModItem) -> Option<FileItemTreeId<Self>> {
478-
if let ModItem::$typ(id) = mod_item {
479-
Some(id)
480-
} else {
481-
None
478+
match mod_item {
479+
ModItem::$typ(id) => Some(id),
480+
_ => None,
482481
}
483482
}
484483

crates/hir_def/src/nameres/path_resolution.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -400,13 +400,10 @@ impl DefMap {
400400
};
401401
let from_scope_or_builtin = match shadow {
402402
BuiltinShadowMode::Module => from_scope.or(from_builtin),
403-
BuiltinShadowMode::Other => {
404-
if let Some(ModuleDefId::ModuleId(_)) = from_scope.take_types() {
405-
from_builtin.or(from_scope)
406-
} else {
407-
from_scope.or(from_builtin)
408-
}
409-
}
403+
BuiltinShadowMode::Other => match from_scope.take_types() {
404+
Some(ModuleDefId::ModuleId(_)) => from_builtin.or(from_scope),
405+
Some(_) | None => from_scope.or(from_builtin),
406+
},
410407
};
411408
let from_extern_prelude = self
412409
.extern_prelude

crates/hir_def/src/path/lower/lower_use.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ pub(crate) fn convert_path(
1818
path: ast::Path,
1919
hygiene: &Hygiene,
2020
) -> Option<ModPath> {
21-
let prefix = if let Some(qual) = path.qualifier() {
22-
Some(convert_path(db, prefix, qual, hygiene)?)
23-
} else {
24-
prefix
21+
let prefix = match path.qualifier() {
22+
Some(qual) => Some(convert_path(db, prefix, qual, hygiene)?),
23+
None => prefix,
2524
};
2625

2726
let segment = path.segment()?;

crates/hir_def/src/type_ref.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,9 @@ impl TypeRef {
214214
}
215215

216216
pub(crate) fn from_ast_opt(ctx: &LowerCtx, node: Option<ast::Type>) -> Self {
217-
if let Some(node) = node {
218-
TypeRef::from_ast(ctx, node)
219-
} else {
220-
TypeRef::Error
217+
match node {
218+
Some(node) => TypeRef::from_ast(ctx, node),
219+
None => TypeRef::Error,
221220
}
222221
}
223222

crates/hir_expand/src/name.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,9 @@ impl Name {
4848

4949
/// Resolve a name from the text of token.
5050
fn resolve(raw_text: &str) -> Name {
51-
if let Some(text) = raw_text.strip_prefix("r#") {
52-
Name::new_text(SmolStr::new(text))
53-
} else {
54-
Name::new_text(raw_text.into())
51+
match raw_text.strip_prefix("r#") {
52+
Some(text) => Name::new_text(SmolStr::new(text)),
53+
None => Name::new_text(raw_text.into()),
5554
}
5655
}
5756

crates/hir_ty/src/autoderef.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,9 @@ pub(crate) fn deref(
109109
ty: InEnvironment<&Canonical<Ty>>,
110110
) -> Option<Canonical<Ty>> {
111111
let _p = profile::span("deref");
112-
if let Some(derefed) = builtin_deref(&ty.goal.value) {
113-
Some(Canonical { value: derefed, binders: ty.goal.binders.clone() })
114-
} else {
115-
deref_by_trait(db, krate, ty)
112+
match builtin_deref(&ty.goal.value) {
113+
Some(derefed) => Some(Canonical { value: derefed, binders: ty.goal.binders.clone() }),
114+
None => deref_by_trait(db, krate, ty),
116115
}
117116
}
118117

crates/hir_ty/src/chalk_ext.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,9 @@ impl TyExt for Ty {
104104
}
105105

106106
fn as_fn_def(&self, db: &dyn HirDatabase) -> Option<FunctionId> {
107-
if let Some(CallableDefId::FunctionId(func)) = self.callable_def(db) {
108-
Some(func)
109-
} else {
110-
None
107+
match self.callable_def(db) {
108+
Some(CallableDefId::FunctionId(func)) => Some(func),
109+
Some(CallableDefId::StructId(_) | CallableDefId::EnumVariantId(_)) | None => None,
111110
}
112111
}
113112
fn as_reference(&self) -> Option<(&Ty, Lifetime, Mutability)> {

crates/hir_ty/src/diagnostics/match_check/deconstruct_pat.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,9 @@ impl IntRange {
105105

106106
#[inline]
107107
fn from_range(lo: u128, hi: u128, scalar_ty: Scalar) -> IntRange {
108-
if let Scalar::Bool = scalar_ty {
109-
IntRange { range: lo..=hi }
110-
} else {
111-
unimplemented!()
108+
match scalar_ty {
109+
Scalar::Bool => IntRange { range: lo..=hi },
110+
_ => unimplemented!(),
112111
}
113112
}
114113

crates/hir_ty/src/display.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,9 @@ impl<'a> HirFormatter<'a> {
167167
}
168168

169169
pub fn should_truncate(&self) -> bool {
170-
if let Some(max_size) = self.max_size {
171-
self.curr_size >= max_size
172-
} else {
173-
false
170+
match self.max_size {
171+
Some(max_size) => self.curr_size >= max_size,
172+
None => false,
174173
}
175174
}
176175

crates/hir_ty/src/infer/expr.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,9 @@ impl<'a> InferenceContext<'a> {
264264

265265
// collect explicitly written argument types
266266
for arg_type in arg_types.iter() {
267-
let arg_ty = if let Some(type_ref) = arg_type {
268-
self.make_ty(type_ref)
269-
} else {
270-
self.table.new_type_var()
267+
let arg_ty = match arg_type {
268+
Some(type_ref) => self.make_ty(type_ref),
269+
None => self.table.new_type_var(),
271270
};
272271
sig_tys.push(arg_ty);
273272
}

crates/hir_ty/src/infer/pat.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,9 @@ impl<'a> InferenceContext<'a> {
204204
} else {
205205
BindingMode::convert(*mode)
206206
};
207-
let inner_ty = if let Some(subpat) = subpat {
208-
self.infer_pat(*subpat, &expected, default_bm)
209-
} else {
210-
expected
207+
let inner_ty = match subpat {
208+
Some(subpat) => self.infer_pat(*subpat, &expected, default_bm),
209+
None => expected,
211210
};
212211
let inner_ty = self.insert_type_vars_shallow(inner_ty);
213212

crates/hir_ty/src/infer/unify.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -324,10 +324,9 @@ impl<'a> InferenceTable<'a> {
324324

325325
/// Unify two types and register new trait goals that arise from that.
326326
pub(crate) fn unify(&mut self, ty1: &Ty, ty2: &Ty) -> bool {
327-
let result = if let Ok(r) = self.try_unify(ty1, ty2) {
328-
r
329-
} else {
330-
return false;
327+
let result = match self.try_unify(ty1, ty2) {
328+
Ok(r) => r,
329+
Err(_) => return false,
331330
};
332331
self.register_infer_ok(result);
333332
true

0 commit comments

Comments
 (0)