Skip to content

Commit

Permalink
hotfix: update is_same function
Browse files Browse the repository at this point in the history
  • Loading branch information
chansuke committed Jan 29, 2023
1 parent 5e3bb6e commit 030a716
Showing 1 changed file with 16 additions and 35 deletions.
51 changes: 16 additions & 35 deletions clippy_lints/src/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ fn check_suspicious_swap(cx: &LateContext<'_>, block: &Block<'_>) {
};

let Some(sugg) = std_or_core(cx) else { return };
if !ExprOrIdent::span(first.span, second.span) { return };

if with_local(first, second) {
lint_almost_swapped_note(cx, span, &what, sugg, &lhs, &rhs);
Expand All @@ -239,42 +240,16 @@ fn with_local<'a, 'hir>(stmt_l: &'a Stmt<'hir>, stmt_r: &'a Stmt<'hir>) -> bool
}

fn is_same<'a, 'hir>(cx: &LateContext<'_>, lhs: &'a ExprOrIdent<'hir>, rhs: &'a Expr<'hir>) -> bool {
if let ExprOrIdent::Ident(ident_l) = lhs
&& ident_l.span.ctxt() == rhs.span.ctxt()
&& let ExprKind::Path(QPath::Resolved(None, path_r)) = rhs.kind
&& ident_l.name.as_str()
== path_r
.segments
.iter()
.map(|el| el.ident.to_string())
.collect::<Vec<_>>()
.join("::")
{
match lhs {
ExprOrIdent::Expr(expr) => eq_expr_value(cx, expr, rhs),
ExprOrIdent::Ident(ident) =>
if let ExprKind::Path(QPath::Resolved(None, path)) = rhs.kind &&
&path.segments.iter().map(|segment| segment.ident).collect::<Vec<_>>()[0] == ident {
true
} else if let ExprOrIdent::Expr(expr_l) = lhs
&& expr_l.span.ctxt() == rhs.span.ctxt()
&& let ExprKind::Path(QPath::Resolved(None, path_l)) = expr_l.kind
&& let ExprKind::Path(QPath::Resolved(None, path_r)) = rhs.kind
&& path_l
.segments
.iter()
.map(|el| el.ident.to_string())
.collect::<Vec<_>>()
.join("::")
== path_r
.segments
.iter()
.map(|el| el.ident.to_string())
.collect::<Vec<_>>()
.join("::")
{
true
} else if let ExprOrIdent::Expr(expr_l) = lhs
&& eq_expr_value(cx, expr_l, rhs) {
true
} else {
false
}
} else {
false
}
}
}

fn lint_almost_swapped_note(cx: &LateContext<'_>, span: Span, what: &str, sugg: &str, lhs: &str, rhs: &str) {
Expand Down Expand Up @@ -319,6 +294,12 @@ pub enum ExprOrIdent<'a> {
Ident(Ident),
}

impl<'a> ExprOrIdent<'a> {
pub fn span(span_l: Span, span_r: Span) -> bool {
span_l.ctxt() == span_r.ctxt()
}
}

fn parse<'a, 'hir>(stmt: &'a Stmt<'hir>) -> Option<(ExprOrIdent<'hir>, &'a Expr<'hir>)> {
if let StmtKind::Semi(expr) = stmt.kind {
if let ExprKind::Assign(lhs, rhs, _) = expr.kind {
Expand Down

0 comments on commit 030a716

Please sign in to comment.