parent_node_is_if_expr now also recognizes if let as parent if

This commit is contained in:
xFrednet 2021-04-10 14:45:51 +02:00
parent b1c675f3fc
commit cbde4f2c67
3 changed files with 44 additions and 7 deletions

View File

@ -290,6 +290,13 @@ fn lint_same_then_else<'tcx>(
}
}
/// The return tuple is structured as follows:
/// 1. The amount of equal statements from the start
/// 2. The amount of equal statements from the end
/// 3. An indication if the block expressions are the same. This will also be true if both are `None`
///
/// This function can also trigger the `IF_SAME_THEN_ELSE` in which case it'll return `(0, 0, false)`
/// to aboard any further processing and avoid duplicate lint triggers.
fn scan_block_for_eq(cx: &LateContext<'tcx>, blocks: &[&Block<'tcx>]) -> (usize, usize, bool) {
let mut start_eq = usize::MAX;
let mut end_eq = usize::MAX;

View File

@ -1212,13 +1212,29 @@ pub fn parent_node_is_if_expr(expr: &Expr<'_>, cx: &LateContext<'_>) -> bool {
let map = cx.tcx.hir();
let parent_id = map.get_parent_node(expr.hir_id);
let parent_node = map.get(parent_id);
matches!(
parent_node,
Node::Expr(Expr {
kind: ExprKind::If(_, _, _),
..
})
)
// Check for `if`
if_chain! {
if let Node::Expr(expr) = parent_node;
if let ExprKind::If(_, _, _) = expr.kind;
then {
return true;
}
}
// Check for `if let`
if_chain! {
if let Node::Arm(arm) = parent_node;
let arm_parent_id = map.get_parent_node(arm.hir_id);
let arm_parent_node = map.get(arm_parent_id);
if let Node::Expr(expr) = arm_parent_node;
if let ExprKind::Match(_, _, MatchSource::IfLetDesugar { .. }) = expr.kind;
then {
return true;
}
}
false
}
// Finds the `#[must_use]` attribute, if any

View File

@ -206,4 +206,18 @@ fn fp_test() {
}
}
fn fp_if_let_issue7054() {
// This shouldn't trigger the lint
let string;
let _x = if let true = true {
""
} else if true {
string = "x".to_owned();
&string
} else {
string = "y".to_owned();
&string
};
}
fn main() {}