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
This commit is contained in:
Cormac Relf 2021-10-13 18:44:47 +11:00
parent 8fea1d94f3
commit 17c1ff9faa
10 changed files with 56 additions and 31 deletions

View File

@ -67,20 +67,20 @@ fn is_structural_partial_eq(cx: &LateContext<'tcx>, ty: Ty<'tcx>, other: Ty<'tcx
impl<'tcx> LateLintPass<'tcx> for PatternEquality { impl<'tcx> LateLintPass<'tcx> for PatternEquality {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) { fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
if_chain! { if_chain! {
if let ExprKind::Let(pat, exp, _) = expr.kind; if let ExprKind::Let(let_expr) = expr.kind;
if unary_pattern(pat); if unary_pattern(let_expr.pat);
let exp_ty = cx.typeck_results().expr_ty(exp); let exp_ty = cx.typeck_results().expr_ty(let_expr.init);
let pat_ty = cx.typeck_results().pat_ty(pat); let pat_ty = cx.typeck_results().pat_ty(let_expr.pat);
if is_structural_partial_eq(cx, exp_ty, pat_ty); if is_structural_partial_eq(cx, exp_ty, pat_ty);
then { then {
let mut applicability = Applicability::MachineApplicable; let mut applicability = Applicability::MachineApplicable;
let pat_str = match pat.kind { let pat_str = match let_expr.pat.kind {
PatKind::Struct(..) => format!( PatKind::Struct(..) => format!(
"({})", "({})",
snippet_with_context(cx, pat.span, expr.span.ctxt(), "..", &mut applicability).0, snippet_with_context(cx, let_expr.pat.span, expr.span.ctxt(), "..", &mut applicability).0,
), ),
_ => snippet_with_context(cx, pat.span, expr.span.ctxt(), "..", &mut applicability).0.to_string(), _ => snippet_with_context(cx, let_expr.pat.span, expr.span.ctxt(), "..", &mut applicability).0.to_string(),
}; };
span_lint_and_sugg( span_lint_and_sugg(
cx, cx,
@ -90,7 +90,7 @@ impl<'tcx> LateLintPass<'tcx> for PatternEquality {
"try", "try",
format!( format!(
"{} == {}", "{} == {}",
snippet_with_context(cx, exp.span, expr.span.ctxt(), "..", &mut applicability).0, snippet_with_context(cx, let_expr.init.span, expr.span.ctxt(), "..", &mut applicability).0,
pat_str, pat_str,
), ),
applicability, applicability,

View File

@ -117,12 +117,12 @@ fn never_loop_expr(expr: &Expr<'_>, main_loop_id: HirId) -> NeverLoopResult {
| ExprKind::Unary(_, e) | ExprKind::Unary(_, e)
| ExprKind::Cast(e, _) | ExprKind::Cast(e, _)
| ExprKind::Type(e, _) | ExprKind::Type(e, _)
| ExprKind::Let(_, e, _)
| ExprKind::Field(e, _) | ExprKind::Field(e, _)
| ExprKind::AddrOf(_, _, e) | ExprKind::AddrOf(_, _, e)
| ExprKind::Struct(_, _, Some(e)) | ExprKind::Struct(_, _, Some(e))
| ExprKind::Repeat(e, _) | ExprKind::Repeat(e, _)
| ExprKind::DropTemps(e) => never_loop_expr(e, main_loop_id), | ExprKind::DropTemps(e) => never_loop_expr(e, main_loop_id),
ExprKind::Let(let_expr) => never_loop_expr(let_expr.init, main_loop_id),
ExprKind::Array(es) | ExprKind::MethodCall(_, _, es, _) | ExprKind::Tup(es) => { ExprKind::Array(es) | ExprKind::MethodCall(_, _, es, _) | ExprKind::Tup(es) => {
never_loop_expr_all(&mut es.iter(), main_loop_id) never_loop_expr_all(&mut es.iter(), main_loop_id)
}, },

View File

@ -50,7 +50,7 @@ impl LateLintPass<'_> for ManualAssert {
.. ..
} = &expr; } = &expr;
if is_expn_of(stmt.span, "panic").is_some(); if is_expn_of(stmt.span, "panic").is_some();
if !matches!(cond.kind, ExprKind::Let(_, _, _)); if !matches!(cond.kind, ExprKind::Let(_));
if let StmtKind::Semi(semi) = stmt.kind; if let StmtKind::Semi(semi) = stmt.kind;
if !cx.tcx.sess.source_map().is_multiline(cond.span); if !cx.tcx.sess.source_map().is_multiline(cond.span);

View File

@ -1,6 +1,6 @@
use clippy_utils::diagnostics::span_lint_and_help; use clippy_utils::diagnostics::span_lint_and_help;
use rustc_hir::{ use rustc_hir::{
intravisit, Body, Expr, ExprKind, FnDecl, HirId, LocalSource, Mutability, Pat, PatKind, Stmt, StmtKind, intravisit, Body, Expr, ExprKind, FnDecl, HirId, Let, LocalSource, Mutability, Pat, PatKind, Stmt, StmtKind,
}; };
use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::lint::in_external_macro; use rustc_middle::lint::in_external_macro;
@ -104,8 +104,8 @@ impl<'tcx> LateLintPass<'tcx> for PatternTypeMismatch {
} }
} }
} }
if let ExprKind::Let(let_pat, ..) = expr.kind { if let ExprKind::Let(Let { pat, .. }) = expr.kind {
apply_lint(cx, let_pat, DerefPossible::Possible); apply_lint(cx, pat, DerefPossible::Possible);
} }
} }

View File

@ -373,11 +373,18 @@ impl<'a, 'tcx> PrintVisitor<'a, 'tcx> {
} }
match expr.value.kind { match expr.value.kind {
ExprKind::Let(pat, expr, _) => { ExprKind::Let(let_expr) => {
bind!(self, pat, expr); bind!(self, let_expr);
kind!("Let({pat}, {expr}, _)"); kind!("Let({let_expr})");
self.pat(pat); self.pat(field!(let_expr.pat));
self.expr(expr); // Does what ExprKind::Cast does, only adds a clause for the type
// if it's a path
if let Some(TyKind::Path(ref qpath)) = let_expr.value.ty.as_ref().map(|ty| &ty.kind) {
bind!(self, qpath);
out!("if let TyKind::Path(ref {qpath}) = {let_expr}.ty.kind;");
self.qpath(qpath);
}
self.expr(field!(let_expr.init));
}, },
ExprKind::Box(inner) => { ExprKind::Box(inner) => {
bind!(self, inner); bind!(self, inner);

View File

@ -142,9 +142,12 @@ fn print_expr(cx: &LateContext<'_>, expr: &hir::Expr<'_>, indent: usize) {
print_expr(cx, arg, indent + 1); print_expr(cx, arg, indent + 1);
} }
}, },
hir::ExprKind::Let(pat, expr, _) => { hir::ExprKind::Let(hir::Let { pat, init, ty, .. }) => {
print_pat(cx, pat, indent + 1); print_pat(cx, pat, indent + 1);
print_expr(cx, expr, indent + 1); if let Some(ty) = ty {
println!("{} type annotation: {:?}", ind, ty);
}
print_expr(cx, init, indent + 1);
}, },
hir::ExprKind::MethodCall(path, _, args, _) => { hir::ExprKind::MethodCall(path, _, args, _) => {
println!("{}MethodCall", ind); println!("{}MethodCall", ind);

View File

@ -101,7 +101,12 @@ impl<'hir> IfLet<'hir> {
pub fn hir(cx: &LateContext<'_>, expr: &Expr<'hir>) -> Option<Self> { pub fn hir(cx: &LateContext<'_>, expr: &Expr<'hir>) -> Option<Self> {
if let ExprKind::If( if let ExprKind::If(
Expr { Expr {
kind: ExprKind::Let(let_pat, let_expr, _), kind:
ExprKind::Let(hir::Let {
pat: let_pat,
init: let_expr,
..
}),
.. ..
}, },
if_then, if_then,
@ -368,7 +373,12 @@ impl<'hir> WhileLet<'hir> {
kind: kind:
ExprKind::If( ExprKind::If(
Expr { Expr {
kind: ExprKind::Let(let_pat, let_expr, _), kind:
ExprKind::Let(hir::Let {
pat: let_pat,
init: let_expr,
..
}),
.. ..
}, },
if_then, if_then,

View File

@ -7,7 +7,7 @@ use rustc_hir::def::Res;
use rustc_hir::HirIdMap; use rustc_hir::HirIdMap;
use rustc_hir::{ use rustc_hir::{
BinOpKind, Block, BodyId, Expr, ExprField, ExprKind, FnRetTy, GenericArg, GenericArgs, Guard, HirId, BinOpKind, Block, BodyId, Expr, ExprField, ExprKind, FnRetTy, GenericArg, GenericArgs, Guard, HirId,
InlineAsmOperand, Lifetime, LifetimeName, ParamName, Pat, PatField, PatKind, Path, PathSegment, QPath, Stmt, InlineAsmOperand, Let, Lifetime, LifetimeName, ParamName, Pat, PatField, PatKind, Path, PathSegment, QPath, Stmt,
StmtKind, Ty, TyKind, TypeBinding, StmtKind, Ty, TyKind, TypeBinding,
}; };
use rustc_lexer::{tokenize, TokenKind}; use rustc_lexer::{tokenize, TokenKind};
@ -232,7 +232,9 @@ impl HirEqInterExpr<'_, '_, '_> {
(&ExprKind::If(lc, lt, ref le), &ExprKind::If(rc, rt, ref re)) => { (&ExprKind::If(lc, lt, ref le), &ExprKind::If(rc, rt, ref re)) => {
self.eq_expr(lc, rc) && self.eq_expr(&**lt, &**rt) && both(le, re, |l, r| self.eq_expr(l, r)) self.eq_expr(lc, rc) && self.eq_expr(&**lt, &**rt) && both(le, re, |l, r| self.eq_expr(l, r))
}, },
(&ExprKind::Let(lp, le, _), &ExprKind::Let(rp, re, _)) => self.eq_pat(lp, rp) && self.eq_expr(le, re), (&ExprKind::Let(l), &ExprKind::Let(r)) => {
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)
},
(&ExprKind::Lit(ref l), &ExprKind::Lit(ref r)) => l.node == r.node, (&ExprKind::Lit(ref l), &ExprKind::Lit(ref r)) => l.node == r.node,
(&ExprKind::Loop(lb, ref ll, ref lls, _), &ExprKind::Loop(rb, ref rl, ref rls, _)) => { (&ExprKind::Loop(lb, ref ll, ref lls, _), &ExprKind::Loop(rb, ref rl, ref rls, _)) => {
lls == rls && self.eq_block(lb, rb) && both(ll, rl, |l, r| l.ident.name == r.ident.name) 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> {
} }
} }
}, },
ExprKind::Let(pat, expr, _) => { ExprKind::Let(Let { pat, init, ty, .. }) => {
self.hash_expr(expr); self.hash_expr(init);
if let Some(ty) = ty {
self.hash_ty(ty);
}
self.hash_pat(pat); self.hash_pat(pat);
}, },
ExprKind::LlvmInlineAsm(..) | ExprKind::Err => {}, ExprKind::LlvmInlineAsm(..) | ExprKind::Err => {},

View File

@ -871,8 +871,8 @@ pub fn capture_local_usage(cx: &LateContext<'tcx>, e: &Expr<'_>) -> CaptureKind
capture_expr_ty = e; capture_expr_ty = e;
} }
}, },
ExprKind::Let(pat, ..) => { ExprKind::Let(let_expr) => {
let mutability = match pat_capture_kind(cx, pat) { let mutability = match pat_capture_kind(cx, let_expr.pat) {
CaptureKind::Value => Mutability::Not, CaptureKind::Value => Mutability::Not,
CaptureKind::Ref(m) => m, CaptureKind::Ref(m) => m,
}; };

View File

@ -32,11 +32,11 @@ if_chain! {
} }
if_chain! { if_chain! {
if let ExprKind::If(cond, then, Some(else_expr)) = expr.kind; if let ExprKind::If(cond, then, Some(else_expr)) = expr.kind;
if let ExprKind::Let(pat, expr1, _) = cond.kind; if let ExprKind::Let(let_expr) = cond.kind;
if let PatKind::Lit(lit_expr) = pat.kind; if let PatKind::Lit(lit_expr) = let_expr.pat.kind;
if let ExprKind::Lit(ref lit) = lit_expr.kind; if let ExprKind::Lit(ref lit) = lit_expr.kind;
if let LitKind::Bool(true) = lit.node; if let LitKind::Bool(true) = lit.node;
if let ExprKind::Path(ref qpath) = expr1.kind; if let ExprKind::Path(ref qpath) = let_expr.init.kind;
if match_qpath(qpath, &["a"]); if match_qpath(qpath, &["a"]);
if let ExprKind::Block(block, None) = then.kind; if let ExprKind::Block(block, None) = then.kind;
if block.stmts.is_empty(); if block.stmts.is_empty();