Skip to content

Commit

Permalink
Avoiding flagging elif statements as potential ternaries (#1694)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh authored Jan 6, 2023
1 parent 18a301a commit 8a3a6a9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 17 deletions.
15 changes: 15 additions & 0 deletions resources/test/fixtures/flake8_simplify/SIM108.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,18 @@
b = a
else:
b = d

if True:
pass
elif a:
b = 1
else:
b = 2

if True:
pass
else:
if a:
b = 1
else:
b = 2
44 changes: 27 additions & 17 deletions src/flake8_simplify/plugins/ast_if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ pub fn use_ternary_operator(checker: &mut Checker, stmt: &Stmt, parent: Option<&
return;
}
let StmtKind::Assign { targets: body_targets, value: body_value, .. } = &body[0].node else {
return;
};
return;
};
let StmtKind::Assign { targets: orelse_targets, value: orelse_value, .. } = &orelse[0].node else {
return;
};
return;
};
if body_targets.len() != 1 || orelse_targets.len() != 1 {
return;
}
Expand All @@ -109,20 +109,30 @@ pub fn use_ternary_operator(checker: &mut Checker, stmt: &Stmt, parent: Option<&
// It's part of a bigger if-elif block:
// https://github.com/MartinThoma/flake8-simplify/issues/115
if let Some(StmtKind::If {
body: parent_body, ..
}) = parent.map(|p| &p.node)
orelse: parent_orelse,
..
}) = parent.map(|parent| &parent.node)
{
for s in parent_body {
let StmtKind::Assign { targets: parent_targets, .. } = &s.node else {
continue;
};
let Some(ExprKind::Name { id: parent_id, .. }) =
parent_targets.get(0).map(|t| &t.node) else {
continue;
};
if body_id == parent_id {
return;
}
if parent_orelse.len() == 1 && stmt == &parent_orelse[0] {
// TODO(charlie): These two cases have the same AST:
//
// if True:
// pass
// elif a:
// b = 1
// else:
// b = 2
//
// if True:
// pass
// else:
// if a:
// b = 1
// else:
// b = 2
//
// We want to flag the latter, but not the former. Right now, we flag neither.
return;
}
}

Expand Down

0 comments on commit 8a3a6a9

Please sign in to comment.