Update clippy for hir::Guard removal
This commit is contained in:
parent
1a267e3f40
commit
44bba5486e
11 changed files with 56 additions and 112 deletions
|
@ -8,7 +8,7 @@ use core::fmt::{self, Write};
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
use rustc_hir::hir_id::HirIdSet;
|
use rustc_hir::hir_id::HirIdSet;
|
||||||
use rustc_hir::intravisit::{walk_expr, Visitor};
|
use rustc_hir::intravisit::{walk_expr, Visitor};
|
||||||
use rustc_hir::{Block, Expr, ExprKind, Guard, HirId, Let, Pat, Stmt, StmtKind, UnOp};
|
use rustc_hir::{Block, Expr, ExprKind, HirId, Pat, Stmt, StmtKind, UnOp};
|
||||||
use rustc_lint::{LateContext, LateLintPass};
|
use rustc_lint::{LateContext, LateLintPass};
|
||||||
use rustc_session::declare_lint_pass;
|
use rustc_session::declare_lint_pass;
|
||||||
use rustc_span::{Span, SyntaxContext, DUMMY_SP};
|
use rustc_span::{Span, SyntaxContext, DUMMY_SP};
|
||||||
|
@ -465,7 +465,7 @@ impl<'tcx> Visitor<'tcx> for InsertSearcher<'_, 'tcx> {
|
||||||
let mut is_map_used = self.is_map_used;
|
let mut is_map_used = self.is_map_used;
|
||||||
for arm in arms {
|
for arm in arms {
|
||||||
self.visit_pat(arm.pat);
|
self.visit_pat(arm.pat);
|
||||||
if let Some(Guard::If(guard) | Guard::IfLet(&Let { init: guard, .. })) = arm.guard {
|
if let Some(guard) = arm.guard {
|
||||||
self.visit_non_tail_expr(guard);
|
self.visit_non_tail_expr(guard);
|
||||||
}
|
}
|
||||||
is_map_used |= self.visit_cond_arm(arm.body);
|
is_map_used |= self.visit_cond_arm(arm.body);
|
||||||
|
|
|
@ -11,7 +11,7 @@ use clippy_utils::{
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use rustc_errors::{Applicability, Diagnostic};
|
use rustc_errors::{Applicability, Diagnostic};
|
||||||
use rustc_hir::def::Res;
|
use rustc_hir::def::Res;
|
||||||
use rustc_hir::{Arm, BinOpKind, Block, Expr, ExprKind, Guard, HirId, PatKind, PathSegment, PrimTy, QPath, StmtKind};
|
use rustc_hir::{Arm, BinOpKind, Block, Expr, ExprKind, HirId, PatKind, PathSegment, PrimTy, QPath, StmtKind};
|
||||||
use rustc_lint::{LateContext, LateLintPass};
|
use rustc_lint::{LateContext, LateLintPass};
|
||||||
use rustc_middle::ty::Ty;
|
use rustc_middle::ty::Ty;
|
||||||
use rustc_session::impl_lint_pass;
|
use rustc_session::impl_lint_pass;
|
||||||
|
@ -394,7 +394,7 @@ fn is_match_pattern<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> Opt
|
||||||
// Find possible min/max branches
|
// Find possible min/max branches
|
||||||
let minmax_values = |a: &'tcx Arm<'tcx>| {
|
let minmax_values = |a: &'tcx Arm<'tcx>| {
|
||||||
if let PatKind::Binding(_, var_hir_id, _, None) = &a.pat.kind
|
if let PatKind::Binding(_, var_hir_id, _, None) = &a.pat.kind
|
||||||
&& let Some(Guard::If(e)) = a.guard
|
&& let Some(e) = a.guard
|
||||||
{
|
{
|
||||||
Some((e, var_hir_id, a.body))
|
Some((e, var_hir_id, a.body))
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -7,7 +7,7 @@ use clippy_utils::{
|
||||||
};
|
};
|
||||||
use rustc_errors::MultiSpan;
|
use rustc_errors::MultiSpan;
|
||||||
use rustc_hir::LangItem::OptionNone;
|
use rustc_hir::LangItem::OptionNone;
|
||||||
use rustc_hir::{Arm, Expr, Guard, HirId, Let, Pat, PatKind};
|
use rustc_hir::{Arm, Expr, HirId, Pat, PatKind};
|
||||||
use rustc_lint::LateContext;
|
use rustc_lint::LateContext;
|
||||||
use rustc_span::Span;
|
use rustc_span::Span;
|
||||||
|
|
||||||
|
@ -16,7 +16,7 @@ use super::COLLAPSIBLE_MATCH;
|
||||||
pub(super) fn check_match<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'_>]) {
|
pub(super) fn check_match<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'_>]) {
|
||||||
if let Some(els_arm) = arms.iter().rfind(|arm| arm_is_wild_like(cx, arm)) {
|
if let Some(els_arm) = arms.iter().rfind(|arm| arm_is_wild_like(cx, arm)) {
|
||||||
for arm in arms {
|
for arm in arms {
|
||||||
check_arm(cx, true, arm.pat, arm.body, arm.guard.as_ref(), Some(els_arm.body));
|
check_arm(cx, true, arm.pat, arm.body, arm.guard, Some(els_arm.body));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,7 @@ fn check_arm<'tcx>(
|
||||||
outer_is_match: bool,
|
outer_is_match: bool,
|
||||||
outer_pat: &'tcx Pat<'tcx>,
|
outer_pat: &'tcx Pat<'tcx>,
|
||||||
outer_then_body: &'tcx Expr<'tcx>,
|
outer_then_body: &'tcx Expr<'tcx>,
|
||||||
outer_guard: Option<&'tcx Guard<'tcx>>,
|
outer_guard: Option<&'tcx Expr<'tcx>>,
|
||||||
outer_else_body: Option<&'tcx Expr<'tcx>>,
|
outer_else_body: Option<&'tcx Expr<'tcx>>,
|
||||||
) {
|
) {
|
||||||
let inner_expr = peel_blocks_with_stmt(outer_then_body);
|
let inner_expr = peel_blocks_with_stmt(outer_then_body);
|
||||||
|
@ -71,7 +71,7 @@ fn check_arm<'tcx>(
|
||||||
// the binding must not be used in the if guard
|
// the binding must not be used in the if guard
|
||||||
&& outer_guard.map_or(
|
&& outer_guard.map_or(
|
||||||
true,
|
true,
|
||||||
|(Guard::If(e) | Guard::IfLet(Let { init: e, .. }))| !is_local_used(cx, *e, binding_id)
|
|e| !is_local_used(cx, e, binding_id)
|
||||||
)
|
)
|
||||||
// ...or anywhere in the inner expression
|
// ...or anywhere in the inner expression
|
||||||
&& match inner {
|
&& match inner {
|
||||||
|
|
|
@ -4,7 +4,7 @@ use clippy_utils::source::snippet_with_applicability;
|
||||||
use clippy_utils::{is_lint_allowed, is_wild, span_contains_comment};
|
use clippy_utils::{is_lint_allowed, is_wild, span_contains_comment};
|
||||||
use rustc_ast::{Attribute, LitKind};
|
use rustc_ast::{Attribute, LitKind};
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
use rustc_hir::{Arm, BorrowKind, Expr, ExprKind, Guard, Pat, PatKind, QPath};
|
use rustc_hir::{Arm, BorrowKind, Expr, ExprKind, Pat, PatKind, QPath};
|
||||||
use rustc_lint::{LateContext, LintContext};
|
use rustc_lint::{LateContext, LintContext};
|
||||||
use rustc_middle::ty;
|
use rustc_middle::ty;
|
||||||
use rustc_span::source_map::Spanned;
|
use rustc_span::source_map::Spanned;
|
||||||
|
@ -41,14 +41,8 @@ pub(super) fn check_match<'tcx>(
|
||||||
find_matches_sugg(
|
find_matches_sugg(
|
||||||
cx,
|
cx,
|
||||||
scrutinee,
|
scrutinee,
|
||||||
arms.iter().map(|arm| {
|
arms.iter()
|
||||||
(
|
.map(|arm| (cx.tcx.hir().attrs(arm.hir_id), Some(arm.pat), arm.body, arm.guard)),
|
||||||
cx.tcx.hir().attrs(arm.hir_id),
|
|
||||||
Some(arm.pat),
|
|
||||||
arm.body,
|
|
||||||
arm.guard.as_ref(),
|
|
||||||
)
|
|
||||||
}),
|
|
||||||
e,
|
e,
|
||||||
false,
|
false,
|
||||||
)
|
)
|
||||||
|
@ -67,14 +61,7 @@ where
|
||||||
I: Clone
|
I: Clone
|
||||||
+ DoubleEndedIterator
|
+ DoubleEndedIterator
|
||||||
+ ExactSizeIterator
|
+ ExactSizeIterator
|
||||||
+ Iterator<
|
+ Iterator<Item = (&'a [Attribute], Option<&'a Pat<'b>>, &'a Expr<'b>, Option<&'a Expr<'b>>)>,
|
||||||
Item = (
|
|
||||||
&'a [Attribute],
|
|
||||||
Option<&'a Pat<'b>>,
|
|
||||||
&'a Expr<'b>,
|
|
||||||
Option<&'a Guard<'b>>,
|
|
||||||
),
|
|
||||||
>,
|
|
||||||
{
|
{
|
||||||
if !span_contains_comment(cx.sess().source_map(), expr.span)
|
if !span_contains_comment(cx.sess().source_map(), expr.span)
|
||||||
&& iter.len() >= 2
|
&& iter.len() >= 2
|
||||||
|
@ -115,7 +102,7 @@ where
|
||||||
})
|
})
|
||||||
.join(" | ")
|
.join(" | ")
|
||||||
};
|
};
|
||||||
let pat_and_guard = if let Some(Guard::If(g)) = first_guard {
|
let pat_and_guard = if let Some(g) = first_guard {
|
||||||
format!(
|
format!(
|
||||||
"{pat} if {}",
|
"{pat} if {}",
|
||||||
snippet_with_applicability(cx, g.span, "..", &mut applicability)
|
snippet_with_applicability(cx, g.span, "..", &mut applicability)
|
||||||
|
|
|
@ -8,7 +8,7 @@ use clippy_utils::{
|
||||||
};
|
};
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
use rustc_hir::LangItem::OptionNone;
|
use rustc_hir::LangItem::OptionNone;
|
||||||
use rustc_hir::{Arm, BindingAnnotation, ByRef, Expr, ExprKind, Guard, ItemKind, Node, Pat, PatKind, Path, QPath};
|
use rustc_hir::{Arm, BindingAnnotation, ByRef, Expr, ExprKind, ItemKind, Node, Pat, PatKind, Path, QPath};
|
||||||
use rustc_lint::LateContext;
|
use rustc_lint::LateContext;
|
||||||
use rustc_span::sym;
|
use rustc_span::sym;
|
||||||
|
|
||||||
|
@ -66,18 +66,9 @@ fn check_all_arms(cx: &LateContext<'_>, match_expr: &Expr<'_>, arms: &[Arm<'_>])
|
||||||
let arm_expr = peel_blocks_with_stmt(arm.body);
|
let arm_expr = peel_blocks_with_stmt(arm.body);
|
||||||
|
|
||||||
if let Some(guard_expr) = &arm.guard {
|
if let Some(guard_expr) = &arm.guard {
|
||||||
match guard_expr {
|
if guard_expr.can_have_side_effects() {
|
||||||
// gives up if `pat if expr` can have side effects
|
return false;
|
||||||
Guard::If(if_cond) => {
|
}
|
||||||
if if_cond.can_have_side_effects() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
// gives up `pat if let ...` arm
|
|
||||||
Guard::IfLet(_) => {
|
|
||||||
return false;
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if let PatKind::Wild = arm.pat.kind {
|
if let PatKind::Wild = arm.pat.kind {
|
||||||
|
|
|
@ -5,7 +5,7 @@ use clippy_utils::visitors::{for_each_expr, is_local_used};
|
||||||
use rustc_ast::{BorrowKind, LitKind};
|
use rustc_ast::{BorrowKind, LitKind};
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
use rustc_hir::def::{DefKind, Res};
|
use rustc_hir::def::{DefKind, Res};
|
||||||
use rustc_hir::{Arm, BinOpKind, Expr, ExprKind, Guard, MatchSource, Node, Pat, PatKind};
|
use rustc_hir::{Arm, BinOpKind, Expr, ExprKind, MatchSource, Node, Pat, PatKind};
|
||||||
use rustc_lint::LateContext;
|
use rustc_lint::LateContext;
|
||||||
use rustc_span::symbol::Ident;
|
use rustc_span::symbol::Ident;
|
||||||
use rustc_span::{Span, Symbol};
|
use rustc_span::{Span, Symbol};
|
||||||
|
@ -21,20 +21,19 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'tcx>]) {
|
||||||
};
|
};
|
||||||
|
|
||||||
// `Some(x) if matches!(x, y)`
|
// `Some(x) if matches!(x, y)`
|
||||||
if let Guard::If(if_expr) = guard
|
if let ExprKind::Match(
|
||||||
&& let ExprKind::Match(
|
scrutinee,
|
||||||
scrutinee,
|
[
|
||||||
[
|
arm,
|
||||||
arm,
|
Arm {
|
||||||
Arm {
|
pat: Pat {
|
||||||
pat: Pat {
|
kind: PatKind::Wild, ..
|
||||||
kind: PatKind::Wild, ..
|
|
||||||
},
|
|
||||||
..
|
|
||||||
},
|
},
|
||||||
],
|
..
|
||||||
MatchSource::Normal,
|
},
|
||||||
) = if_expr.kind
|
],
|
||||||
|
MatchSource::Normal,
|
||||||
|
) = guard.kind
|
||||||
&& let Some(binding) = get_pat_binding(cx, scrutinee, outer_arm)
|
&& let Some(binding) = get_pat_binding(cx, scrutinee, outer_arm)
|
||||||
{
|
{
|
||||||
let pat_span = match (arm.pat.kind, binding.byref_ident) {
|
let pat_span = match (arm.pat.kind, binding.byref_ident) {
|
||||||
|
@ -45,14 +44,14 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'tcx>]) {
|
||||||
emit_redundant_guards(
|
emit_redundant_guards(
|
||||||
cx,
|
cx,
|
||||||
outer_arm,
|
outer_arm,
|
||||||
if_expr.span,
|
guard.span,
|
||||||
snippet(cx, pat_span, "<binding>"),
|
snippet(cx, pat_span, "<binding>"),
|
||||||
&binding,
|
&binding,
|
||||||
arm.guard,
|
arm.guard,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
// `Some(x) if let Some(2) = x`
|
// `Some(x) if let Some(2) = x`
|
||||||
else if let Guard::IfLet(let_expr) = guard
|
else if let ExprKind::Let(let_expr) = guard.kind
|
||||||
&& let Some(binding) = get_pat_binding(cx, let_expr.init, outer_arm)
|
&& let Some(binding) = get_pat_binding(cx, let_expr.init, outer_arm)
|
||||||
{
|
{
|
||||||
let pat_span = match (let_expr.pat.kind, binding.byref_ident) {
|
let pat_span = match (let_expr.pat.kind, binding.byref_ident) {
|
||||||
|
@ -71,8 +70,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'tcx>]) {
|
||||||
}
|
}
|
||||||
// `Some(x) if x == Some(2)`
|
// `Some(x) if x == Some(2)`
|
||||||
// `Some(x) if Some(2) == x`
|
// `Some(x) if Some(2) == x`
|
||||||
else if let Guard::If(if_expr) = guard
|
else if let ExprKind::Binary(bin_op, local, pat) = guard.kind
|
||||||
&& let ExprKind::Binary(bin_op, local, pat) = if_expr.kind
|
|
||||||
&& matches!(bin_op.node, BinOpKind::Eq)
|
&& matches!(bin_op.node, BinOpKind::Eq)
|
||||||
// Ensure they have the same type. If they don't, we'd need deref coercion which isn't
|
// Ensure they have the same type. If they don't, we'd need deref coercion which isn't
|
||||||
// possible (currently) in a pattern. In some cases, you can use something like
|
// possible (currently) in a pattern. In some cases, you can use something like
|
||||||
|
@ -96,16 +94,15 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'tcx>]) {
|
||||||
emit_redundant_guards(
|
emit_redundant_guards(
|
||||||
cx,
|
cx,
|
||||||
outer_arm,
|
outer_arm,
|
||||||
if_expr.span,
|
guard.span,
|
||||||
snippet(cx, pat_span, "<binding>"),
|
snippet(cx, pat_span, "<binding>"),
|
||||||
&binding,
|
&binding,
|
||||||
None,
|
None,
|
||||||
);
|
);
|
||||||
} else if let Guard::If(if_expr) = guard
|
} else if let ExprKind::MethodCall(path, recv, args, ..) = guard.kind
|
||||||
&& let ExprKind::MethodCall(path, recv, args, ..) = if_expr.kind
|
|
||||||
&& let Some(binding) = get_pat_binding(cx, recv, outer_arm)
|
&& let Some(binding) = get_pat_binding(cx, recv, outer_arm)
|
||||||
{
|
{
|
||||||
check_method_calls(cx, outer_arm, path.ident.name, recv, args, if_expr, &binding);
|
check_method_calls(cx, outer_arm, path.ident.name, recv, args, guard, &binding);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -216,7 +213,7 @@ fn emit_redundant_guards<'tcx>(
|
||||||
guard_span: Span,
|
guard_span: Span,
|
||||||
binding_replacement: Cow<'static, str>,
|
binding_replacement: Cow<'static, str>,
|
||||||
pat_binding: &PatBindingInfo,
|
pat_binding: &PatBindingInfo,
|
||||||
inner_guard: Option<Guard<'_>>,
|
inner_guard: Option<&Expr<'_>>,
|
||||||
) {
|
) {
|
||||||
span_lint_and_then(
|
span_lint_and_then(
|
||||||
cx,
|
cx,
|
||||||
|
@ -242,12 +239,7 @@ fn emit_redundant_guards<'tcx>(
|
||||||
(
|
(
|
||||||
guard_span.source_callsite().with_lo(outer_arm.pat.span.hi()),
|
guard_span.source_callsite().with_lo(outer_arm.pat.span.hi()),
|
||||||
inner_guard.map_or_else(String::new, |guard| {
|
inner_guard.map_or_else(String::new, |guard| {
|
||||||
let (prefix, span) = match guard {
|
format!(" if {}", snippet(cx, guard.span, "<guard>"))
|
||||||
Guard::If(e) => ("if", e.span),
|
|
||||||
Guard::IfLet(l) => ("if let", l.span),
|
|
||||||
};
|
|
||||||
|
|
||||||
format!(" {prefix} {}", snippet(cx, span, "<guard>"))
|
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
|
|
@ -9,7 +9,7 @@ use rustc_ast::ast::LitKind;
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
use rustc_hir::def::{DefKind, Res};
|
use rustc_hir::def::{DefKind, Res};
|
||||||
use rustc_hir::LangItem::{self, OptionNone, OptionSome, PollPending, PollReady, ResultErr, ResultOk};
|
use rustc_hir::LangItem::{self, OptionNone, OptionSome, PollPending, PollReady, ResultErr, ResultOk};
|
||||||
use rustc_hir::{Arm, Expr, ExprKind, Guard, Node, Pat, PatKind, QPath, UnOp};
|
use rustc_hir::{Arm, Expr, ExprKind, Node, Pat, PatKind, QPath, UnOp};
|
||||||
use rustc_lint::LateContext;
|
use rustc_lint::LateContext;
|
||||||
use rustc_middle::ty::{self, GenericArgKind, Ty};
|
use rustc_middle::ty::{self, GenericArgKind, Ty};
|
||||||
use rustc_span::{sym, Span, Symbol};
|
use rustc_span::{sym, Span, Symbol};
|
||||||
|
@ -277,8 +277,6 @@ pub(super) fn check_match<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, op
|
||||||
let mut sugg = format!("{}.{good_method}", snippet(cx, result_expr.span, "_"));
|
let mut sugg = format!("{}.{good_method}", snippet(cx, result_expr.span, "_"));
|
||||||
|
|
||||||
if let Some(guard) = maybe_guard {
|
if let Some(guard) = maybe_guard {
|
||||||
let Guard::If(guard) = *guard else { return }; // `...is_none() && let ...` is a syntax error
|
|
||||||
|
|
||||||
// wow, the HIR for match guards in `PAT if let PAT = expr && expr => ...` is annoying!
|
// wow, the HIR for match guards in `PAT if let PAT = expr && expr => ...` is annoying!
|
||||||
// `guard` here is `Guard::If` with the let expression somewhere deep in the tree of exprs,
|
// `guard` here is `Guard::If` with the let expression somewhere deep in the tree of exprs,
|
||||||
// counter to the intuition that it should be `Guard::IfLet`, so we need another check
|
// counter to the intuition that it should be `Guard::IfLet`, so we need another check
|
||||||
|
@ -319,7 +317,7 @@ fn found_good_method<'tcx>(
|
||||||
cx: &LateContext<'_>,
|
cx: &LateContext<'_>,
|
||||||
arms: &'tcx [Arm<'tcx>],
|
arms: &'tcx [Arm<'tcx>],
|
||||||
node: (&PatKind<'_>, &PatKind<'_>),
|
node: (&PatKind<'_>, &PatKind<'_>),
|
||||||
) -> Option<(&'static str, Option<&'tcx Guard<'tcx>>)> {
|
) -> Option<(&'static str, Option<&'tcx Expr<'tcx>>)> {
|
||||||
match node {
|
match node {
|
||||||
(
|
(
|
||||||
PatKind::TupleStruct(ref path_left, patterns_left, _),
|
PatKind::TupleStruct(ref path_left, patterns_left, _),
|
||||||
|
@ -409,7 +407,7 @@ fn get_good_method<'tcx>(
|
||||||
cx: &LateContext<'_>,
|
cx: &LateContext<'_>,
|
||||||
arms: &'tcx [Arm<'tcx>],
|
arms: &'tcx [Arm<'tcx>],
|
||||||
path_left: &QPath<'_>,
|
path_left: &QPath<'_>,
|
||||||
) -> Option<(&'static str, Option<&'tcx Guard<'tcx>>)> {
|
) -> Option<(&'static str, Option<&'tcx Expr<'tcx>>)> {
|
||||||
if let Some(name) = get_ident(path_left) {
|
if let Some(name) = get_ident(path_left) {
|
||||||
let (expected_item_left, should_be_left, should_be_right) = match name.as_str() {
|
let (expected_item_left, should_be_left, should_be_right) = match name.as_str() {
|
||||||
"Ok" => (Item::Lang(ResultOk), "is_ok()", "is_err()"),
|
"Ok" => (Item::Lang(ResultOk), "is_ok()", "is_err()"),
|
||||||
|
@ -478,7 +476,7 @@ fn find_good_method_for_match<'a, 'tcx>(
|
||||||
expected_item_right: Item,
|
expected_item_right: Item,
|
||||||
should_be_left: &'a str,
|
should_be_left: &'a str,
|
||||||
should_be_right: &'a str,
|
should_be_right: &'a str,
|
||||||
) -> Option<(&'a str, Option<&'tcx Guard<'tcx>>)> {
|
) -> Option<(&'a str, Option<&'tcx Expr<'tcx>>)> {
|
||||||
let first_pat = arms[0].pat;
|
let first_pat = arms[0].pat;
|
||||||
let second_pat = arms[1].pat;
|
let second_pat = arms[1].pat;
|
||||||
|
|
||||||
|
@ -496,8 +494,8 @@ fn find_good_method_for_match<'a, 'tcx>(
|
||||||
|
|
||||||
match body_node_pair {
|
match body_node_pair {
|
||||||
(ExprKind::Lit(lit_left), ExprKind::Lit(lit_right)) => match (&lit_left.node, &lit_right.node) {
|
(ExprKind::Lit(lit_left), ExprKind::Lit(lit_right)) => match (&lit_left.node, &lit_right.node) {
|
||||||
(LitKind::Bool(true), LitKind::Bool(false)) => Some((should_be_left, arms[0].guard.as_ref())),
|
(LitKind::Bool(true), LitKind::Bool(false)) => Some((should_be_left, arms[0].guard)),
|
||||||
(LitKind::Bool(false), LitKind::Bool(true)) => Some((should_be_right, arms[1].guard.as_ref())),
|
(LitKind::Bool(false), LitKind::Bool(true)) => Some((should_be_right, arms[1].guard)),
|
||||||
_ => None,
|
_ => None,
|
||||||
},
|
},
|
||||||
_ => None,
|
_ => None,
|
||||||
|
@ -511,7 +509,7 @@ fn find_good_method_for_matches_macro<'a, 'tcx>(
|
||||||
expected_item_left: Item,
|
expected_item_left: Item,
|
||||||
should_be_left: &'a str,
|
should_be_left: &'a str,
|
||||||
should_be_right: &'a str,
|
should_be_right: &'a str,
|
||||||
) -> Option<(&'a str, Option<&'tcx Guard<'tcx>>)> {
|
) -> Option<(&'a str, Option<&'tcx Expr<'tcx>>)> {
|
||||||
let first_pat = arms[0].pat;
|
let first_pat = arms[0].pat;
|
||||||
|
|
||||||
let body_node_pair = if is_pat_variant(cx, first_pat, path_left, expected_item_left) {
|
let body_node_pair = if is_pat_variant(cx, first_pat, path_left, expected_item_left) {
|
||||||
|
@ -522,8 +520,8 @@ fn find_good_method_for_matches_macro<'a, 'tcx>(
|
||||||
|
|
||||||
match body_node_pair {
|
match body_node_pair {
|
||||||
(ExprKind::Lit(lit_left), ExprKind::Lit(lit_right)) => match (&lit_left.node, &lit_right.node) {
|
(ExprKind::Lit(lit_left), ExprKind::Lit(lit_right)) => match (&lit_left.node, &lit_right.node) {
|
||||||
(LitKind::Bool(true), LitKind::Bool(false)) => Some((should_be_left, arms[0].guard.as_ref())),
|
(LitKind::Bool(true), LitKind::Bool(false)) => Some((should_be_left, arms[0].guard)),
|
||||||
(LitKind::Bool(false), LitKind::Bool(true)) => Some((should_be_right, arms[1].guard.as_ref())),
|
(LitKind::Bool(false), LitKind::Bool(true)) => Some((should_be_right, arms[1].guard)),
|
||||||
_ => None,
|
_ => None,
|
||||||
},
|
},
|
||||||
_ => None,
|
_ => None,
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use clippy_utils::diagnostics::{span_lint, span_lint_and_note};
|
use clippy_utils::diagnostics::{span_lint, span_lint_and_note};
|
||||||
use clippy_utils::{get_parent_expr, path_to_local, path_to_local_id};
|
use clippy_utils::{get_parent_expr, path_to_local, path_to_local_id};
|
||||||
use rustc_hir::intravisit::{walk_expr, Visitor};
|
use rustc_hir::intravisit::{walk_expr, Visitor};
|
||||||
use rustc_hir::{BinOpKind, Block, Expr, ExprKind, Guard, HirId, Local, Node, Stmt, StmtKind};
|
use rustc_hir::{BinOpKind, Block, Expr, ExprKind, HirId, Local, Node, Stmt, StmtKind};
|
||||||
use rustc_lint::{LateContext, LateLintPass};
|
use rustc_lint::{LateContext, LateLintPass};
|
||||||
use rustc_middle::ty;
|
use rustc_middle::ty;
|
||||||
use rustc_session::declare_lint_pass;
|
use rustc_session::declare_lint_pass;
|
||||||
|
@ -119,7 +119,7 @@ impl<'a, 'tcx> DivergenceVisitor<'a, 'tcx> {
|
||||||
ExprKind::Match(e, arms, _) => {
|
ExprKind::Match(e, arms, _) => {
|
||||||
self.visit_expr(e);
|
self.visit_expr(e);
|
||||||
for arm in arms {
|
for arm in arms {
|
||||||
if let Some(Guard::If(if_expr)) = arm.guard {
|
if let Some(if_expr) = arm.guard {
|
||||||
self.visit_expr(if_expr);
|
self.visit_expr(if_expr);
|
||||||
}
|
}
|
||||||
// make sure top level arm expressions aren't linted
|
// make sure top level arm expressions aren't linted
|
||||||
|
|
|
@ -318,17 +318,11 @@ impl<'a, 'tcx> PrintVisitor<'a, 'tcx> {
|
||||||
self.pat(field!(arm.pat));
|
self.pat(field!(arm.pat));
|
||||||
match arm.value.guard {
|
match arm.value.guard {
|
||||||
None => chain!(self, "{arm}.guard.is_none()"),
|
None => chain!(self, "{arm}.guard.is_none()"),
|
||||||
Some(hir::Guard::If(expr)) => {
|
Some(expr) => {
|
||||||
bind!(self, expr);
|
bind!(self, expr);
|
||||||
chain!(self, "let Some(Guard::If({expr})) = {arm}.guard");
|
chain!(self, "let Some({expr}) = {arm}.guard");
|
||||||
self.expr(expr);
|
self.expr(expr);
|
||||||
},
|
},
|
||||||
Some(hir::Guard::IfLet(let_expr)) => {
|
|
||||||
bind!(self, let_expr);
|
|
||||||
chain!(self, "let Some(Guard::IfLet({let_expr}) = {arm}.guard");
|
|
||||||
self.pat(field!(let_expr.pat));
|
|
||||||
self.expr(field!(let_expr.init));
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
self.expr(field!(arm.body));
|
self.expr(field!(arm.body));
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,7 @@ use rustc_hir::def::Res;
|
||||||
use rustc_hir::MatchSource::TryDesugar;
|
use rustc_hir::MatchSource::TryDesugar;
|
||||||
use rustc_hir::{
|
use rustc_hir::{
|
||||||
ArrayLen, BinOpKind, BindingAnnotation, Block, BodyId, Closure, Expr, ExprField, ExprKind, FnRetTy, GenericArg,
|
ArrayLen, BinOpKind, BindingAnnotation, Block, BodyId, Closure, Expr, ExprField, ExprKind, FnRetTy, GenericArg,
|
||||||
GenericArgs, Guard, HirId, HirIdMap, InlineAsmOperand, Let, Lifetime, LifetimeName, Pat, PatField, PatKind, Path,
|
GenericArgs, HirId, HirIdMap, InlineAsmOperand, Let, Lifetime, LifetimeName, Pat, PatField, PatKind, Path,
|
||||||
PathSegment, PrimTy, QPath, Stmt, StmtKind, Ty, TyKind, TypeBinding,
|
PathSegment, PrimTy, QPath, Stmt, StmtKind, Ty, TyKind, TypeBinding,
|
||||||
};
|
};
|
||||||
use rustc_lexer::{tokenize, TokenKind};
|
use rustc_lexer::{tokenize, TokenKind};
|
||||||
|
@ -320,7 +320,7 @@ impl HirEqInterExpr<'_, '_, '_> {
|
||||||
&& self.eq_expr(le, re)
|
&& self.eq_expr(le, re)
|
||||||
&& over(la, ra, |l, r| {
|
&& over(la, ra, |l, r| {
|
||||||
self.eq_pat(l.pat, r.pat)
|
self.eq_pat(l.pat, r.pat)
|
||||||
&& both(&l.guard, &r.guard, |l, r| self.eq_guard(l, r))
|
&& both(&l.guard, &r.guard, |l, r| self.eq_expr(l, r))
|
||||||
&& self.eq_expr(l.body, r.body)
|
&& self.eq_expr(l.body, r.body)
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
@ -410,16 +410,6 @@ impl HirEqInterExpr<'_, '_, '_> {
|
||||||
left.ident.name == right.ident.name && self.eq_expr(left.expr, right.expr)
|
left.ident.name == right.ident.name && self.eq_expr(left.expr, right.expr)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn eq_guard(&mut self, left: &Guard<'_>, right: &Guard<'_>) -> bool {
|
|
||||||
match (left, right) {
|
|
||||||
(Guard::If(l), Guard::If(r)) => self.eq_expr(l, r),
|
|
||||||
(Guard::IfLet(l), Guard::IfLet(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)
|
|
||||||
},
|
|
||||||
_ => false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn eq_generic_arg(&mut self, left: &GenericArg<'_>, right: &GenericArg<'_>) -> bool {
|
fn eq_generic_arg(&mut self, left: &GenericArg<'_>, right: &GenericArg<'_>) -> bool {
|
||||||
match (left, right) {
|
match (left, right) {
|
||||||
(GenericArg::Const(l), GenericArg::Const(r)) => self.eq_body(l.value.body, r.value.body),
|
(GenericArg::Const(l), GenericArg::Const(r)) => self.eq_body(l.value.body, r.value.body),
|
||||||
|
@ -876,7 +866,7 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
|
||||||
for arm in arms {
|
for arm in arms {
|
||||||
self.hash_pat(arm.pat);
|
self.hash_pat(arm.pat);
|
||||||
if let Some(ref e) = arm.guard {
|
if let Some(ref e) = arm.guard {
|
||||||
self.hash_guard(e);
|
self.hash_expr(e);
|
||||||
}
|
}
|
||||||
self.hash_expr(arm.body);
|
self.hash_expr(arm.body);
|
||||||
}
|
}
|
||||||
|
@ -1056,14 +1046,6 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn hash_guard(&mut self, g: &Guard<'_>) {
|
|
||||||
match g {
|
|
||||||
Guard::If(expr) | Guard::IfLet(Let { init: expr, .. }) => {
|
|
||||||
self.hash_expr(expr);
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn hash_lifetime(&mut self, lifetime: &Lifetime) {
|
pub fn hash_lifetime(&mut self, lifetime: &Lifetime) {
|
||||||
lifetime.ident.name.hash(&mut self.s);
|
lifetime.ident.name.hash(&mut self.s);
|
||||||
std::mem::discriminant(&lifetime.res).hash(&mut self.s);
|
std::mem::discriminant(&lifetime.res).hash(&mut self.s);
|
||||||
|
|
|
@ -3164,7 +3164,7 @@ pub fn is_never_expr<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> Option<
|
||||||
self.is_never = false;
|
self.is_never = false;
|
||||||
if let Some(guard) = arm.guard {
|
if let Some(guard) = arm.guard {
|
||||||
let in_final_expr = mem::replace(&mut self.in_final_expr, false);
|
let in_final_expr = mem::replace(&mut self.in_final_expr, false);
|
||||||
self.visit_expr(guard.body());
|
self.visit_expr(guard);
|
||||||
self.in_final_expr = in_final_expr;
|
self.in_final_expr = in_final_expr;
|
||||||
// The compiler doesn't consider diverging guards as causing the arm to diverge.
|
// The compiler doesn't consider diverging guards as causing the arm to diverge.
|
||||||
self.is_never = false;
|
self.is_never = false;
|
||||||
|
@ -3223,7 +3223,7 @@ pub fn is_never_expr<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> Option<
|
||||||
fn visit_arm(&mut self, arm: &Arm<'tcx>) {
|
fn visit_arm(&mut self, arm: &Arm<'tcx>) {
|
||||||
if let Some(guard) = arm.guard {
|
if let Some(guard) = arm.guard {
|
||||||
let in_final_expr = mem::replace(&mut self.in_final_expr, false);
|
let in_final_expr = mem::replace(&mut self.in_final_expr, false);
|
||||||
self.visit_expr(guard.body());
|
self.visit_expr(guard);
|
||||||
self.in_final_expr = in_final_expr;
|
self.in_final_expr = in_final_expr;
|
||||||
}
|
}
|
||||||
self.visit_expr(arm.body);
|
self.visit_expr(arm.body);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue