Skip to content

Commit 34a9819

Browse files
committed
let-else: use hir::Let in clippy
fix clippy format using `cargo fmt -p clippy_{lints,utils}` manually revert rustfmt line truncations rename to hir::Let in clippy Undo the shadowing of various `expr` variables after renaming `scrutinee` reduce destructuring of hir::Let to avoid `expr` collisions cargo fmt -p clippy_{lints,utils} bless new clippy::author output
1 parent 9b45713 commit 34a9819

File tree

10 files changed

+56
-31
lines changed

10 files changed

+56
-31
lines changed

src/tools/clippy/clippy_lints/src/equatable_if_let.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,20 @@ fn is_structural_partial_eq(cx: &LateContext<'tcx>, ty: Ty<'tcx>, other: Ty<'tcx
6767
impl<'tcx> LateLintPass<'tcx> for PatternEquality {
6868
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
6969
if_chain! {
70-
if let ExprKind::Let(pat, exp, _) = expr.kind;
71-
if unary_pattern(pat);
72-
let exp_ty = cx.typeck_results().expr_ty(exp);
73-
let pat_ty = cx.typeck_results().pat_ty(pat);
70+
if let ExprKind::Let(let_expr) = expr.kind;
71+
if unary_pattern(let_expr.pat);
72+
let exp_ty = cx.typeck_results().expr_ty(let_expr.init);
73+
let pat_ty = cx.typeck_results().pat_ty(let_expr.pat);
7474
if is_structural_partial_eq(cx, exp_ty, pat_ty);
7575
then {
7676

7777
let mut applicability = Applicability::MachineApplicable;
78-
let pat_str = match pat.kind {
78+
let pat_str = match let_expr.pat.kind {
7979
PatKind::Struct(..) => format!(
8080
"({})",
81-
snippet_with_context(cx, pat.span, expr.span.ctxt(), "..", &mut applicability).0,
81+
snippet_with_context(cx, let_expr.pat.span, expr.span.ctxt(), "..", &mut applicability).0,
8282
),
83-
_ => snippet_with_context(cx, pat.span, expr.span.ctxt(), "..", &mut applicability).0.to_string(),
83+
_ => snippet_with_context(cx, let_expr.pat.span, expr.span.ctxt(), "..", &mut applicability).0.to_string(),
8484
};
8585
span_lint_and_sugg(
8686
cx,
@@ -90,7 +90,7 @@ impl<'tcx> LateLintPass<'tcx> for PatternEquality {
9090
"try",
9191
format!(
9292
"{} == {}",
93-
snippet_with_context(cx, exp.span, expr.span.ctxt(), "..", &mut applicability).0,
93+
snippet_with_context(cx, let_expr.init.span, expr.span.ctxt(), "..", &mut applicability).0,
9494
pat_str,
9595
),
9696
applicability,

src/tools/clippy/clippy_lints/src/loops/never_loop.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,12 @@ fn never_loop_expr(expr: &Expr<'_>, main_loop_id: HirId) -> NeverLoopResult {
117117
| ExprKind::Unary(_, e)
118118
| ExprKind::Cast(e, _)
119119
| ExprKind::Type(e, _)
120-
| ExprKind::Let(_, e, _)
121120
| ExprKind::Field(e, _)
122121
| ExprKind::AddrOf(_, _, e)
123122
| ExprKind::Struct(_, _, Some(e))
124123
| ExprKind::Repeat(e, _)
125124
| ExprKind::DropTemps(e) => never_loop_expr(e, main_loop_id),
125+
ExprKind::Let(let_expr) => never_loop_expr(let_expr.init, main_loop_id),
126126
ExprKind::Array(es) | ExprKind::MethodCall(_, _, es, _) | ExprKind::Tup(es) => {
127127
never_loop_expr_all(&mut es.iter(), main_loop_id)
128128
},

src/tools/clippy/clippy_lints/src/manual_assert.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl LateLintPass<'_> for ManualAssert {
5050
..
5151
} = &expr;
5252
if is_expn_of(stmt.span, "panic").is_some();
53-
if !matches!(cond.kind, ExprKind::Let(_, _, _));
53+
if !matches!(cond.kind, ExprKind::Let(_));
5454
if let StmtKind::Semi(semi) = stmt.kind;
5555
if !cx.tcx.sess.source_map().is_multiline(cond.span);
5656

src/tools/clippy/clippy_lints/src/pattern_type_mismatch.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
22
use rustc_hir::{
3-
intravisit, Body, Expr, ExprKind, FnDecl, HirId, LocalSource, Mutability, Pat, PatKind, Stmt, StmtKind,
3+
intravisit, Body, Expr, ExprKind, FnDecl, HirId, Let, LocalSource, Mutability, Pat, PatKind, Stmt, StmtKind,
44
};
55
use rustc_lint::{LateContext, LateLintPass, LintContext};
66
use rustc_middle::lint::in_external_macro;
@@ -104,8 +104,8 @@ impl<'tcx> LateLintPass<'tcx> for PatternTypeMismatch {
104104
}
105105
}
106106
}
107-
if let ExprKind::Let(let_pat, ..) = expr.kind {
108-
apply_lint(cx, let_pat, DerefPossible::Possible);
107+
if let ExprKind::Let(Let { pat, .. }) = expr.kind {
108+
apply_lint(cx, pat, DerefPossible::Possible);
109109
}
110110
}
111111

src/tools/clippy/clippy_lints/src/utils/author.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -373,11 +373,18 @@ impl<'a, 'tcx> PrintVisitor<'a, 'tcx> {
373373
}
374374

375375
match expr.value.kind {
376-
ExprKind::Let(pat, expr, _) => {
377-
bind!(self, pat, expr);
378-
kind!("Let({pat}, {expr}, _)");
379-
self.pat(pat);
380-
self.expr(expr);
376+
ExprKind::Let(let_expr) => {
377+
bind!(self, let_expr);
378+
kind!("Let({let_expr})");
379+
self.pat(field!(let_expr.pat));
380+
// Does what ExprKind::Cast does, only adds a clause for the type
381+
// if it's a path
382+
if let Some(TyKind::Path(ref qpath)) = let_expr.value.ty.as_ref().map(|ty| &ty.kind) {
383+
bind!(self, qpath);
384+
out!("if let TyKind::Path(ref {qpath}) = {let_expr}.ty.kind;");
385+
self.qpath(qpath);
386+
}
387+
self.expr(field!(let_expr.init));
381388
},
382389
ExprKind::Box(inner) => {
383390
bind!(self, inner);

src/tools/clippy/clippy_lints/src/utils/inspector.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,12 @@ fn print_expr(cx: &LateContext<'_>, expr: &hir::Expr<'_>, indent: usize) {
142142
print_expr(cx, arg, indent + 1);
143143
}
144144
},
145-
hir::ExprKind::Let(pat, expr, _) => {
145+
hir::ExprKind::Let(hir::Let { pat, init, ty, .. }) => {
146146
print_pat(cx, pat, indent + 1);
147-
print_expr(cx, expr, indent + 1);
147+
if let Some(ty) = ty {
148+
println!("{} type annotation: {:?}", ind, ty);
149+
}
150+
print_expr(cx, init, indent + 1);
148151
},
149152
hir::ExprKind::MethodCall(path, _, args, _) => {
150153
println!("{}MethodCall", ind);

src/tools/clippy/clippy_utils/src/higher.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,12 @@ impl<'hir> IfLet<'hir> {
101101
pub fn hir(cx: &LateContext<'_>, expr: &Expr<'hir>) -> Option<Self> {
102102
if let ExprKind::If(
103103
Expr {
104-
kind: ExprKind::Let(let_pat, let_expr, _),
104+
kind:
105+
ExprKind::Let(hir::Let {
106+
pat: let_pat,
107+
init: let_expr,
108+
..
109+
}),
105110
..
106111
},
107112
if_then,
@@ -368,7 +373,12 @@ impl<'hir> WhileLet<'hir> {
368373
kind:
369374
ExprKind::If(
370375
Expr {
371-
kind: ExprKind::Let(let_pat, let_expr, _),
376+
kind:
377+
ExprKind::Let(hir::Let {
378+
pat: let_pat,
379+
init: let_expr,
380+
..
381+
}),
372382
..
373383
},
374384
if_then,

src/tools/clippy/clippy_utils/src/hir_utils.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_hir::def::Res;
77
use rustc_hir::HirIdMap;
88
use rustc_hir::{
99
BinOpKind, Block, BodyId, Expr, ExprField, ExprKind, FnRetTy, GenericArg, GenericArgs, Guard, HirId,
10-
InlineAsmOperand, Lifetime, LifetimeName, ParamName, Pat, PatField, PatKind, Path, PathSegment, QPath, Stmt,
10+
InlineAsmOperand, Let, Lifetime, LifetimeName, ParamName, Pat, PatField, PatKind, Path, PathSegment, QPath, Stmt,
1111
StmtKind, Ty, TyKind, TypeBinding,
1212
};
1313
use rustc_lexer::{tokenize, TokenKind};
@@ -232,7 +232,9 @@ impl HirEqInterExpr<'_, '_, '_> {
232232
(&ExprKind::If(lc, lt, ref le), &ExprKind::If(rc, rt, ref re)) => {
233233
self.eq_expr(lc, rc) && self.eq_expr(&**lt, &**rt) && both(le, re, |l, r| self.eq_expr(l, r))
234234
},
235-
(&ExprKind::Let(lp, le, _), &ExprKind::Let(rp, re, _)) => self.eq_pat(lp, rp) && self.eq_expr(le, re),
235+
(&ExprKind::Let(l), &ExprKind::Let(r)) => {
236+
self.eq_pat(l.pat, r.pat) && both(&l.ty, &r.ty, |l, r| self.eq_ty(l, r)) && self.eq_expr(l.init, r.init)
237+
},
236238
(&ExprKind::Lit(ref l), &ExprKind::Lit(ref r)) => l.node == r.node,
237239
(&ExprKind::Loop(lb, ref ll, ref lls, _), &ExprKind::Loop(rb, ref rl, ref rls, _)) => {
238240
lls == rls && self.eq_block(lb, rb) && both(ll, rl, |l, r| l.ident.name == r.ident.name)
@@ -666,8 +668,11 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
666668
}
667669
}
668670
},
669-
ExprKind::Let(pat, expr, _) => {
670-
self.hash_expr(expr);
671+
ExprKind::Let(Let { pat, init, ty, .. }) => {
672+
self.hash_expr(init);
673+
if let Some(ty) = ty {
674+
self.hash_ty(ty);
675+
}
671676
self.hash_pat(pat);
672677
},
673678
ExprKind::LlvmInlineAsm(..) | ExprKind::Err => {},

src/tools/clippy/clippy_utils/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -871,8 +871,8 @@ pub fn capture_local_usage(cx: &LateContext<'tcx>, e: &Expr<'_>) -> CaptureKind
871871
capture_expr_ty = e;
872872
}
873873
},
874-
ExprKind::Let(pat, ..) => {
875-
let mutability = match pat_capture_kind(cx, pat) {
874+
ExprKind::Let(let_expr) => {
875+
let mutability = match pat_capture_kind(cx, let_expr.pat) {
876876
CaptureKind::Value => Mutability::Not,
877877
CaptureKind::Ref(m) => m,
878878
};

src/tools/clippy/tests/ui/author/if.stdout

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ if_chain! {
3232
}
3333
if_chain! {
3434
if let ExprKind::If(cond, then, Some(else_expr)) = expr.kind;
35-
if let ExprKind::Let(pat, expr1, _) = cond.kind;
36-
if let PatKind::Lit(lit_expr) = pat.kind;
35+
if let ExprKind::Let(let_expr) = cond.kind;
36+
if let PatKind::Lit(lit_expr) = let_expr.pat.kind;
3737
if let ExprKind::Lit(ref lit) = lit_expr.kind;
3838
if let LitKind::Bool(true) = lit.node;
39-
if let ExprKind::Path(ref qpath) = expr1.kind;
39+
if let ExprKind::Path(ref qpath) = let_expr.init.kind;
4040
if match_qpath(qpath, &["a"]);
4141
if let ExprKind::Block(block, None) = then.kind;
4242
if block.stmts.is_empty();

0 commit comments

Comments
 (0)