Skip to content

Commit

Permalink
Fix question_mark_in_expression false positive
Browse files Browse the repository at this point in the history
  • Loading branch information
smoelius committed Dec 27, 2024
1 parent 6752e8d commit 0d252f4
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 7 deletions.
4 changes: 2 additions & 2 deletions examples/restriction/question_mark_in_expression/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ publish = false
crate-type = ["cdylib"]

[[example]]
name = "assign_op"
path = "ui/assign_op.rs"
name = "assign"
path = "ui/assign.rs"

[[example]]
name = "clone"
Expand Down
11 changes: 6 additions & 5 deletions examples/restriction/question_mark_in_expression/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,19 @@ dylint_linting::declare_late_lint! {
impl<'tcx> LateLintPass<'tcx> for QuestionMarkInExpression {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'_>) {
if !cx
.tcx
.hir()
.parent_iter(expr.hir_id)
.any(|(hir_id, _)| cx.tcx.hir().span(hir_id).in_derive_expansion())
.tcx
.hir()
.parent_iter(expr.hir_id)
.any(|(hir_id, _)| cx.tcx.hir().span(hir_id).in_derive_expansion())
&& let ExprKind::Match(_, _, MatchSource::TryDesugar(_)) = expr.kind
&& let Some((Node::Expr(ancestor), child_hir_id)) =
get_filtered_ancestor(cx, expr.hir_id)
// smoelius: `AssignOp`, `If`, `Let`, and `Match` expressions get a pass.
// smoelius: `Assign`, `AssignOp`, `If`, `Let`, and `Match` expressions get a pass.
&& !match ancestor.kind {
ExprKind::Let(..) => true,
ExprKind::If(condition, _, _) => condition.hir_id == child_hir_id,
ExprKind::Match(scrutinee, _, _) => scrutinee.hir_id == child_hir_id,
ExprKind::Assign(_, expr, _) => expr.hir_id == child_hir_id,
ExprKind::AssignOp(_, _, expr) => expr.hir_id == child_hir_id,
_ => false,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

fn main() -> Result<(), ()> {
let mut x = 0;
x = foo()?;
x += foo()?;
Ok(())
}
Expand Down

0 comments on commit 0d252f4

Please sign in to comment.