Skip to content

Commit

Permalink
let-else: use hir::Let in clippy
Browse files Browse the repository at this point in the history
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
  • Loading branch information
cormacrelf committed Dec 13, 2021
1 parent 9b45713 commit 34a9819
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 31 deletions.
16 changes: 8 additions & 8 deletions src/tools/clippy/clippy_lints/src/equatable_if_let.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,20 @@ fn is_structural_partial_eq(cx: &LateContext<'tcx>, ty: Ty<'tcx>, other: Ty<'tcx
impl<'tcx> LateLintPass<'tcx> for PatternEquality {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
if_chain! {
if let ExprKind::Let(pat, exp, _) = expr.kind;
if unary_pattern(pat);
let exp_ty = cx.typeck_results().expr_ty(exp);
let pat_ty = cx.typeck_results().pat_ty(pat);
if let ExprKind::Let(let_expr) = expr.kind;
if unary_pattern(let_expr.pat);
let exp_ty = cx.typeck_results().expr_ty(let_expr.init);
let pat_ty = cx.typeck_results().pat_ty(let_expr.pat);
if is_structural_partial_eq(cx, exp_ty, pat_ty);
then {

let mut applicability = Applicability::MachineApplicable;
let pat_str = match pat.kind {
let pat_str = match let_expr.pat.kind {
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(
cx,
Expand All @@ -90,7 +90,7 @@ impl<'tcx> LateLintPass<'tcx> for PatternEquality {
"try",
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,
),
applicability,
Expand Down
2 changes: 1 addition & 1 deletion src/tools/clippy/clippy_lints/src/loops/never_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@ fn never_loop_expr(expr: &Expr<'_>, main_loop_id: HirId) -> NeverLoopResult {
| ExprKind::Unary(_, e)
| ExprKind::Cast(e, _)
| ExprKind::Type(e, _)
| ExprKind::Let(_, e, _)
| ExprKind::Field(e, _)
| ExprKind::AddrOf(_, _, e)
| ExprKind::Struct(_, _, Some(e))
| ExprKind::Repeat(e, _)
| 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) => {
never_loop_expr_all(&mut es.iter(), main_loop_id)
},
Expand Down
2 changes: 1 addition & 1 deletion src/tools/clippy/clippy_lints/src/manual_assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl LateLintPass<'_> for ManualAssert {
..
} = &expr;
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 !cx.tcx.sess.source_map().is_multiline(cond.span);

Expand Down
6 changes: 3 additions & 3 deletions src/tools/clippy/clippy_lints/src/pattern_type_mismatch.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clippy_utils::diagnostics::span_lint_and_help;
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_middle::lint::in_external_macro;
Expand Down Expand Up @@ -104,8 +104,8 @@ impl<'tcx> LateLintPass<'tcx> for PatternTypeMismatch {
}
}
}
if let ExprKind::Let(let_pat, ..) = expr.kind {
apply_lint(cx, let_pat, DerefPossible::Possible);
if let ExprKind::Let(Let { pat, .. }) = expr.kind {
apply_lint(cx, pat, DerefPossible::Possible);
}
}

Expand Down
17 changes: 12 additions & 5 deletions src/tools/clippy/clippy_lints/src/utils/author.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,11 +373,18 @@ impl<'a, 'tcx> PrintVisitor<'a, 'tcx> {
}

match expr.value.kind {
ExprKind::Let(pat, expr, _) => {
bind!(self, pat, expr);
kind!("Let({pat}, {expr}, _)");
self.pat(pat);
self.expr(expr);
ExprKind::Let(let_expr) => {
bind!(self, let_expr);
kind!("Let({let_expr})");
self.pat(field!(let_expr.pat));
// 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) => {
bind!(self, inner);
Expand Down
7 changes: 5 additions & 2 deletions src/tools/clippy/clippy_lints/src/utils/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,12 @@ fn print_expr(cx: &LateContext<'_>, expr: &hir::Expr<'_>, indent: usize) {
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_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, _) => {
println!("{}MethodCall", ind);
Expand Down
14 changes: 12 additions & 2 deletions src/tools/clippy/clippy_utils/src/higher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,12 @@ impl<'hir> IfLet<'hir> {
pub fn hir(cx: &LateContext<'_>, expr: &Expr<'hir>) -> Option<Self> {
if let ExprKind::If(
Expr {
kind: ExprKind::Let(let_pat, let_expr, _),
kind:
ExprKind::Let(hir::Let {
pat: let_pat,
init: let_expr,
..
}),
..
},
if_then,
Expand Down Expand Up @@ -368,7 +373,12 @@ impl<'hir> WhileLet<'hir> {
kind:
ExprKind::If(
Expr {
kind: ExprKind::Let(let_pat, let_expr, _),
kind:
ExprKind::Let(hir::Let {
pat: let_pat,
init: let_expr,
..
}),
..
},
if_then,
Expand Down
13 changes: 9 additions & 4 deletions src/tools/clippy/clippy_utils/src/hir_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rustc_hir::def::Res;
use rustc_hir::HirIdMap;
use rustc_hir::{
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,
};
use rustc_lexer::{tokenize, TokenKind};
Expand Down Expand Up @@ -232,7 +232,9 @@ impl HirEqInterExpr<'_, '_, '_> {
(&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))
},
(&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::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)
Expand Down Expand Up @@ -666,8 +668,11 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
}
}
},
ExprKind::Let(pat, expr, _) => {
self.hash_expr(expr);
ExprKind::Let(Let { pat, init, ty, .. }) => {
self.hash_expr(init);
if let Some(ty) = ty {
self.hash_ty(ty);
}
self.hash_pat(pat);
},
ExprKind::LlvmInlineAsm(..) | ExprKind::Err => {},
Expand Down
4 changes: 2 additions & 2 deletions src/tools/clippy/clippy_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -871,8 +871,8 @@ pub fn capture_local_usage(cx: &LateContext<'tcx>, e: &Expr<'_>) -> CaptureKind
capture_expr_ty = e;
}
},
ExprKind::Let(pat, ..) => {
let mutability = match pat_capture_kind(cx, pat) {
ExprKind::Let(let_expr) => {
let mutability = match pat_capture_kind(cx, let_expr.pat) {
CaptureKind::Value => Mutability::Not,
CaptureKind::Ref(m) => m,
};
Expand Down
6 changes: 3 additions & 3 deletions src/tools/clippy/tests/ui/author/if.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ if_chain! {
}
if_chain! {
if let ExprKind::If(cond, then, Some(else_expr)) = expr.kind;
if let ExprKind::Let(pat, expr1, _) = cond.kind;
if let PatKind::Lit(lit_expr) = pat.kind;
if let ExprKind::Let(let_expr) = cond.kind;
if let PatKind::Lit(lit_expr) = let_expr.pat.kind;
if let ExprKind::Lit(ref lit) = lit_expr.kind;
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 let ExprKind::Block(block, None) = then.kind;
if block.stmts.is_empty();
Expand Down

0 comments on commit 34a9819

Please sign in to comment.