Skip to content

Commit f48d13f

Browse files
committed
Replace manual let else patterns with let else
1 parent cf72565 commit f48d13f

32 files changed

+73
-150
lines changed

clippy_dev/src/setup/intellij.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ impl ClippyProjectInfo {
3636
}
3737

3838
pub fn setup_rustc_src(rustc_path: &str) {
39-
let rustc_source_dir = match check_and_get_rustc_dir(rustc_path) {
40-
Ok(path) => path,
41-
Err(_) => return,
39+
let Ok(rustc_source_dir) = check_and_get_rustc_dir(rustc_path) else {
40+
return
4241
};
4342

4443
for project in CLIPPY_PROJECTS {
@@ -172,24 +171,18 @@ pub fn remove_rustc_src() {
172171
}
173172

174173
fn remove_rustc_src_from_project(project: &ClippyProjectInfo) -> bool {
175-
let mut cargo_content = if let Ok(content) = read_project_file(project.cargo_file) {
176-
content
177-
} else {
174+
let Ok(mut cargo_content) = read_project_file(project.cargo_file) else {
178175
return false;
179176
};
180-
let section_start = if let Some(section_start) = cargo_content.find(RUSTC_PATH_SECTION) {
181-
section_start
182-
} else {
177+
let Some(section_start) = cargo_content.find(RUSTC_PATH_SECTION) else {
183178
println!(
184179
"info: dependencies could not be found in `{}` for {}, skipping file",
185180
project.cargo_file, project.name
186181
);
187182
return true;
188183
};
189184

190-
let end_point = if let Some(end_point) = cargo_content.find(DEPENDENCIES_SECTION) {
191-
end_point
192-
} else {
185+
let Some(end_point) = cargo_content.find(DEPENDENCIES_SECTION) else {
193186
eprintln!(
194187
"error: the end of the rustc dependencies section could not be found in `{}`",
195188
project.cargo_file

clippy_dev/src/update_lints.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -869,13 +869,11 @@ fn clippy_lints_src_files() -> impl Iterator<Item = (PathBuf, DirEntry)> {
869869
macro_rules! match_tokens {
870870
($iter:ident, $($token:ident $({$($fields:tt)*})? $(($capture:ident))?)*) => {
871871
{
872-
$($(let $capture =)? if let Some(LintDeclSearchResult {
872+
$(#[allow(clippy::redundant_pattern)] let Some(LintDeclSearchResult {
873873
token_kind: TokenKind::$token $({$($fields)*})?,
874-
content: _x,
874+
content: $($capture @)? _,
875875
..
876-
}) = $iter.next() {
877-
_x
878-
} else {
876+
}) = $iter.next() else {
879877
continue;
880878
};)*
881879
#[allow(clippy::unused_unit)]

clippy_lints/src/derive.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,10 +339,7 @@ fn check_copy_clone<'tcx>(cx: &LateContext<'tcx>, item: &Item<'_>, trait_ref: &h
339339
Some(id) if trait_ref.trait_def_id() == Some(id) => id,
340340
_ => return,
341341
};
342-
let copy_id = match cx.tcx.lang_items().copy_trait() {
343-
Some(id) => id,
344-
None => return,
345-
};
342+
let Some(copy_id) = cx.tcx.lang_items().copy_trait() else { return };
346343
let (ty_adt, ty_subs) = match *ty.kind() {
347344
// Unions can't derive clone.
348345
ty::Adt(adt, subs) if !adt.is_union() => (adt, subs),

clippy_lints/src/disallowed_methods.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,8 @@ impl<'tcx> LateLintPass<'tcx> for DisallowedMethods {
9494
} else {
9595
path_def_id(cx, expr)
9696
};
97-
let def_id = match uncalled_path.or_else(|| fn_def_id(cx, expr)) {
98-
Some(def_id) => def_id,
99-
None => return,
97+
let Some(def_id) = uncalled_path.or_else(|| fn_def_id(cx, expr)) else {
98+
return
10099
};
101100
let conf = match self.disallowed.get(&def_id) {
102101
Some(&index) => &self.conf_disallowed[index],

clippy_lints/src/entry.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,28 +65,24 @@ declare_lint_pass!(HashMapPass => [MAP_ENTRY]);
6565
impl<'tcx> LateLintPass<'tcx> for HashMapPass {
6666
#[expect(clippy::too_many_lines)]
6767
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
68-
let (cond_expr, then_expr, else_expr) = match higher::If::hir(expr) {
69-
Some(higher::If { cond, then, r#else }) => (cond, then, r#else),
70-
_ => return,
68+
let Some(higher::If { cond: cond_expr, then: then_expr, r#else: else_expr }) = higher::If::hir(expr) else {
69+
return
7170
};
7271

73-
let (map_ty, contains_expr) = match try_parse_contains(cx, cond_expr) {
74-
Some(x) => x,
75-
None => return,
72+
let Some((map_ty, contains_expr)) = try_parse_contains(cx, cond_expr) else {
73+
return
7674
};
7775

78-
let then_search = match find_insert_calls(cx, &contains_expr, then_expr) {
79-
Some(x) => x,
80-
None => return,
76+
let Some(then_search) = find_insert_calls(cx, &contains_expr, then_expr) else {
77+
return
8178
};
8279

8380
let mut app = Applicability::MachineApplicable;
8481
let map_str = snippet_with_context(cx, contains_expr.map.span, contains_expr.call_ctxt, "..", &mut app).0;
8582
let key_str = snippet_with_context(cx, contains_expr.key.span, contains_expr.call_ctxt, "..", &mut app).0;
8683
let sugg = if let Some(else_expr) = else_expr {
87-
let else_search = match find_insert_calls(cx, &contains_expr, else_expr) {
88-
Some(search) => search,
89-
None => return,
84+
let Some(else_search) = find_insert_calls(cx, &contains_expr, else_expr) else {
85+
return;
9086
};
9187

9288
if then_search.edits.is_empty() && else_search.edits.is_empty() {

clippy_lints/src/eta_reduction.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,8 @@ fn check_sig<'tcx>(cx: &LateContext<'tcx>, closure_ty: Ty<'tcx>, call_ty: Ty<'tc
213213
if !closure_ty.has_late_bound_regions() {
214214
return true;
215215
}
216-
let substs = match closure_ty.kind() {
217-
ty::Closure(_, substs) => substs,
218-
_ => return false,
216+
let ty::Closure(_, substs) = closure_ty.kind() else {
217+
return false;
219218
};
220219
let closure_sig = cx.tcx.signature_unclosure(substs.as_closure().sig(), Unsafety::Normal);
221220
cx.tcx.erase_late_bound_regions(closure_sig) == cx.tcx.erase_late_bound_regions(call_sig)

clippy_lints/src/functions/too_many_lines.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ pub(super) fn check_fn(
2222
return;
2323
}
2424

25-
let code_snippet = match snippet_opt(cx, body.value.span) {
26-
Some(s) => s,
27-
_ => return,
25+
let Some(code_snippet) = snippet_opt(cx, body.value.span) else {
26+
return
2827
};
2928
let mut line_count: u64 = 0;
3029
let mut in_comment = false;

clippy_lints/src/invalid_upcast_comparisons.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,7 @@ impl<'tcx> LateLintPass<'tcx> for InvalidUpcastComparisons {
145145
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
146146
if let ExprKind::Binary(ref cmp, lhs, rhs) = expr.kind {
147147
let normalized = comparisons::normalize_comparison(cmp.node, lhs, rhs);
148-
let (rel, normalized_lhs, normalized_rhs) = if let Some(val) = normalized {
149-
val
150-
} else {
148+
let Some((rel, normalized_lhs, normalized_rhs)) = normalized else {
151149
return;
152150
};
153151

clippy_lints/src/large_enum_variant.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,8 @@ impl<'tcx> LateLintPass<'tcx> for LargeEnumVariant {
124124
}
125125
if let ItemKind::Enum(ref def, _) = item.kind {
126126
let ty = cx.tcx.type_of(item.def_id);
127-
let (adt, subst) = match ty.kind() {
128-
Adt(adt, subst) => (adt, subst),
129-
_ => panic!("already checked whether this is an enum"),
127+
let Adt(adt, subst) = ty.kind() else {
128+
panic!("already checked whether this is an enum")
130129
};
131130
if adt.variants().len() <= 1 {
132131
return;

clippy_lints/src/loops/while_let_on_iterator.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,8 @@ fn needs_mutable_borrow(cx: &LateContext<'_>, iter_expr: &IterExpr, loop_expr: &
331331
}
332332

333333
if let Some(e) = get_enclosing_loop_or_multi_call_closure(cx, loop_expr) {
334-
let local_id = match iter_expr.path {
335-
Res::Local(id) => id,
336-
_ => return true,
334+
let Res::Local(local_id) = iter_expr.path else {
335+
return true
337336
};
338337
let mut v = NestedLoopVisitor {
339338
cx,

0 commit comments

Comments
 (0)