Skip to content

Commit 854015c

Browse files
committed
Auto merge of #9629 - est31:let_else, r=Jarcho
Replace manual let else patterns with let else Clears the codebase from places where the lint added by #8437 is firing, by adopting let else. changelog: none
2 parents 122ae22 + f48d13f commit 854015c

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,

clippy_lints/src/matches/manual_utils.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,8 @@ where
6060
return None;
6161
}
6262

63-
let some_expr = match get_some_expr_fn(cx, some_pat, some_expr, expr_ctxt) {
64-
Some(expr) => expr,
65-
None => return None,
63+
let Some(some_expr) = get_some_expr_fn(cx, some_pat, some_expr, expr_ctxt) else {
64+
return None;
6665
};
6766

6867
// These two lints will go back and forth with each other.

clippy_lints/src/matches/match_same_arms.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,6 @@ fn iter_matching_struct_fields<'a>(
221221

222222
#[expect(clippy::similar_names)]
223223
impl<'a> NormalizedPat<'a> {
224-
#[expect(clippy::too_many_lines)]
225224
fn from_pat(cx: &LateContext<'_>, arena: &'a DroplessArena, pat: &'a Pat<'_>) -> Self {
226225
match pat.kind {
227226
PatKind::Wild | PatKind::Binding(.., None) => Self::Wild,
@@ -235,9 +234,8 @@ impl<'a> NormalizedPat<'a> {
235234
Self::Struct(cx.qpath_res(path, pat.hir_id).opt_def_id(), fields)
236235
},
237236
PatKind::TupleStruct(ref path, pats, wild_idx) => {
238-
let adt = match cx.typeck_results().pat_ty(pat).ty_adt_def() {
239-
Some(x) => x,
240-
None => return Self::Wild,
237+
let Some(adt) = cx.typeck_results().pat_ty(pat).ty_adt_def() else {
238+
return Self::Wild
241239
};
242240
let (var_id, variant) = if adt.is_enum() {
243241
match cx.qpath_res(path, pat.hir_id).opt_def_id() {

clippy_lints/src/methods/into_iter_on_ref.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ pub(super) fn check(
4242

4343
fn ty_has_iter_method(cx: &LateContext<'_>, self_ref_ty: Ty<'_>) -> Option<(Symbol, &'static str)> {
4444
has_iter_method(cx, self_ref_ty).map(|ty_name| {
45-
let mutbl = match self_ref_ty.kind() {
46-
ty::Ref(_, _, mutbl) => mutbl,
47-
_ => unreachable!(),
45+
let ty::Ref(_, _, mutbl) = self_ref_ty.kind() else {
46+
unreachable!()
4847
};
4948
let method_name = match mutbl {
5049
hir::Mutability::Not => "iter",

clippy_lints/src/methods/manual_saturating_arithmetic.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,15 @@ pub fn check(
2121
return;
2222
}
2323

24-
let mm = if let Some(mm) = is_min_or_max(cx, unwrap_arg) {
25-
mm
26-
} else {
27-
return;
28-
};
24+
let Some(mm) = is_min_or_max(cx, unwrap_arg) else { return };
2925

3026
if ty.is_signed() {
3127
use self::{
3228
MinMax::{Max, Min},
3329
Sign::{Neg, Pos},
3430
};
3531

36-
let sign = if let Some(sign) = lit_sign(arith_rhs) {
37-
sign
38-
} else {
32+
let Some(sign) = lit_sign(arith_rhs) else {
3933
return;
4034
};
4135

clippy_lints/src/methods/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3851,9 +3851,8 @@ impl SelfKind {
38513851
hir::Mutability::Mut => &paths::ASMUT_TRAIT,
38523852
};
38533853

3854-
let trait_def_id = match get_trait_def_id(cx, trait_path) {
3855-
Some(did) => did,
3856-
None => return false,
3854+
let Some(trait_def_id) = get_trait_def_id(cx, trait_path) else {
3855+
return false
38573856
};
38583857
implements_trait(cx, ty, trait_def_id, &[parent_ty.into()])
38593858
}

clippy_lints/src/methods/str_splitn.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,7 @@ fn parse_iter_usage<'tcx>(
289289
) -> Option<IterUsage> {
290290
let (kind, span) = match iter.next() {
291291
Some((_, Node::Expr(e))) if e.span.ctxt() == ctxt => {
292-
let (name, args) = if let ExprKind::MethodCall(name, _, [args @ ..], _) = e.kind {
293-
(name, args)
294-
} else {
292+
let ExprKind::MethodCall(name, _, [args @ ..], _) = e.kind else {
295293
return None;
296294
};
297295
let did = cx.typeck_results().type_dependent_def_id(e.hir_id)?;

clippy_lints/src/misc_early/literal_suffix.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ use rustc_lint::EarlyContext;
66
use super::{SEPARATED_LITERAL_SUFFIX, UNSEPARATED_LITERAL_SUFFIX};
77

88
pub(super) fn check(cx: &EarlyContext<'_>, lit: &Lit, lit_snip: &str, suffix: &str, sugg_type: &str) {
9-
let maybe_last_sep_idx = if let Some(val) = lit_snip.len().checked_sub(suffix.len() + 1) {
10-
val
11-
} else {
9+
let Some(maybe_last_sep_idx) = lit_snip.len().checked_sub(suffix.len() + 1) else {
1210
return; // It's useless so shouldn't lint.
1311
};
1412
// Do not lint when literal is unsuffixed.

clippy_lints/src/misc_early/mixed_case_hex_literals.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ use rustc_lint::EarlyContext;
55
use super::MIXED_CASE_HEX_LITERALS;
66

77
pub(super) fn check(cx: &EarlyContext<'_>, lit: &Lit, suffix: &str, lit_snip: &str) {
8-
let maybe_last_sep_idx = if let Some(val) = lit_snip.len().checked_sub(suffix.len() + 1) {
9-
val
10-
} else {
8+
let Some(maybe_last_sep_idx) = lit_snip.len().checked_sub(suffix.len() + 1) else {
119
return; // It's useless so shouldn't lint.
1210
};
1311
if maybe_last_sep_idx <= 2 {

clippy_lints/src/mismatching_type_param_order.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,8 @@ impl<'tcx> LateLintPass<'tcx> for TypeParamMismatch {
7070

7171
// find the type that the Impl is for
7272
// only lint on struct/enum/union for now
73-
let defid = match path.res {
74-
Res::Def(DefKind::Struct | DefKind::Enum | DefKind::Union, defid) => defid,
75-
_ => return,
73+
let Res::Def(DefKind::Struct | DefKind::Enum | DefKind::Union, defid) = path.res else {
74+
return
7675
};
7776

7877
// get the names of the generic parameters in the type

clippy_lints/src/mixed_read_write_in_expression.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,7 @@ fn check_for_unsequenced_reads(vis: &mut ReadVisitor<'_, '_>) {
190190
if parent_id == cur_id {
191191
break;
192192
}
193-
let parent_node = match map.find(parent_id) {
194-
Some(parent) => parent,
195-
None => break,
196-
};
193+
let Some(parent_node) = map.find(parent_id) else { break };
197194

198195
let stop_early = match parent_node {
199196
Node::Expr(expr) => check_expr(vis, expr),

clippy_lints/src/needless_for_each.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,8 @@ declare_lint_pass!(NeedlessForEach => [NEEDLESS_FOR_EACH]);
4949

5050
impl<'tcx> LateLintPass<'tcx> for NeedlessForEach {
5151
fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'_>) {
52-
let expr = match stmt.kind {
53-
StmtKind::Expr(expr) | StmtKind::Semi(expr) => expr,
54-
_ => return,
52+
let (StmtKind::Expr(expr) | StmtKind::Semi(expr)) = stmt.kind else {
53+
return
5554
};
5655

5756
if_chain! {

clippy_lints/src/non_copy_const.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,9 +357,8 @@ impl<'tcx> LateLintPass<'tcx> for NonCopyConst {
357357
}
358358

359359
// Make sure it is a const item.
360-
let item_def_id = match cx.qpath_res(qpath, expr.hir_id) {
361-
Res::Def(DefKind::Const | DefKind::AssocConst, did) => did,
362-
_ => return,
360+
let Res::Def(DefKind::Const | DefKind::AssocConst, item_def_id) = cx.qpath_res(qpath, expr.hir_id) else {
361+
return
363362
};
364363

365364
// Climb up to resolve any field access and explicit referencing.

clippy_lints/src/non_octal_unix_permissions.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,8 @@ impl<'tcx> LateLintPass<'tcx> for NonOctalUnixPermissions {
5555
if let ExprKind::Lit(_) = param.kind;
5656

5757
then {
58-
let snip = match snippet_opt(cx, param.span) {
59-
Some(s) => s,
60-
_ => return,
58+
let Some(snip) = snippet_opt(cx, param.span) else {
59+
return
6160
};
6261

6362
if !snip.starts_with("0o") {
@@ -72,16 +71,10 @@ impl<'tcx> LateLintPass<'tcx> for NonOctalUnixPermissions {
7271
if let Some(def_id) = cx.qpath_res(path, func.hir_id).opt_def_id();
7372
if match_def_path(cx, def_id, &paths::PERMISSIONS_FROM_MODE);
7473
if let ExprKind::Lit(_) = param.kind;
75-
74+
if let Some(snip) = snippet_opt(cx, param.span);
75+
if !snip.starts_with("0o");
7676
then {
77-
let snip = match snippet_opt(cx, param.span) {
78-
Some(s) => s,
79-
_ => return,
80-
};
81-
82-
if !snip.starts_with("0o") {
83-
show_error(cx, param);
84-
}
77+
show_error(cx, param);
8578
}
8679
}
8780
},

clippy_lints/src/ptr.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -552,9 +552,8 @@ fn check_ptr_arg_usage<'tcx>(cx: &LateContext<'tcx>, body: &'tcx Body<'_>, args:
552552
}
553553

554554
// Check if this is local we care about
555-
let args_idx = match path_to_local(e).and_then(|id| self.bindings.get(&id)) {
556-
Some(&i) => i,
557-
None => return walk_expr(self, e),
555+
let Some(&args_idx) = path_to_local(e).and_then(|id| self.bindings.get(&id)) else {
556+
return walk_expr(self, e);
558557
};
559558
let args = &self.args[args_idx];
560559
let result = &mut self.results[args_idx];
@@ -609,9 +608,7 @@ fn check_ptr_arg_usage<'tcx>(cx: &LateContext<'tcx>, body: &'tcx Body<'_>, args:
609608
}
610609
}
611610

612-
let id = if let Some(x) = self.cx.typeck_results().type_dependent_def_id(e.hir_id) {
613-
x
614-
} else {
611+
let Some(id) = self.cx.typeck_results().type_dependent_def_id(e.hir_id) else {
615612
set_skip_flag();
616613
return;
617614
};

0 commit comments

Comments
 (0)