Fix obfuscated_if_else suggestion on left side of a binary expr

An `if … { … } else { … }` used as the left operand of a binary
expression requires parentheses to be parsed as an expression.
This commit is contained in:
Samuel Tardieu 2025-01-31 23:25:26 +01:00
parent aad3686823
commit ac0a11a8bc
4 changed files with 113 additions and 1 deletions

View file

@ -1,6 +1,7 @@
use super::OBFUSCATED_IF_ELSE;
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::eager_or_lazy::switch_to_eager_eval;
use clippy_utils::get_parent_expr;
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::sugg::Sugg;
use rustc_errors::Applicability;
@ -41,6 +42,17 @@ pub(super) fn check<'tcx>(
snippet_with_applicability(cx, unwrap_arg.span, "..", &mut applicability)
);
// To be parsed as an expression, the `if { … } else { … }` as the left operand of a binary operator
// requires parentheses.
let sugg = if let Some(parent_expr) = get_parent_expr(cx, expr)
&& let ExprKind::Binary(_, left, _) = parent_expr.kind
&& left.hir_id == expr.hir_id
{
format!("({sugg})")
} else {
sugg
};
span_lint_and_sugg(
cx,
OBFUSCATED_IF_ELSE,