check for try blocks in LintPass methods

This commit is contained in:
y21 2024-02-23 19:16:41 +01:00
parent 4c1d05cfa1
commit 0671d78283
3 changed files with 24 additions and 6 deletions

View file

@ -224,8 +224,7 @@ impl QuestionMark {
/// ///
/// If it matches, it will suggest to use the question mark operator instead /// If it matches, it will suggest to use the question mark operator instead
fn check_is_none_or_err_and_early_return<'tcx>(&self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) { fn check_is_none_or_err_and_early_return<'tcx>(&self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) {
if !self.inside_try_block() if let Some(higher::If { cond, then, r#else }) = higher::If::hir(expr)
&& let Some(higher::If { cond, then, r#else }) = higher::If::hir(expr)
&& !is_else_clause(cx.tcx, expr) && !is_else_clause(cx.tcx, expr)
&& let ExprKind::MethodCall(segment, caller, ..) = &cond.kind && let ExprKind::MethodCall(segment, caller, ..) = &cond.kind
&& let caller_ty = cx.typeck_results().expr_ty(caller) && let caller_ty = cx.typeck_results().expr_ty(caller)
@ -259,8 +258,7 @@ impl QuestionMark {
} }
fn check_if_let_some_or_err_and_early_return<'tcx>(&self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) { fn check_if_let_some_or_err_and_early_return<'tcx>(&self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) {
if !self.inside_try_block() if let Some(higher::IfLet {
&& let Some(higher::IfLet {
let_pat, let_pat,
let_expr, let_expr,
if_then, if_then,
@ -324,13 +322,13 @@ impl<'tcx> LateLintPass<'tcx> for QuestionMark {
return; return;
} }
if !in_constant(cx, stmt.hir_id) { if !self.inside_try_block() && !in_constant(cx, stmt.hir_id) {
check_let_some_else_return_none(cx, stmt); check_let_some_else_return_none(cx, stmt);
} }
self.check_manual_let_else(cx, stmt); self.check_manual_let_else(cx, stmt);
} }
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
if !in_constant(cx, expr.hir_id) && is_lint_allowed(cx, QUESTION_MARK_USED, expr.hir_id) { if !self.inside_try_block() && !in_constant(cx, expr.hir_id) && is_lint_allowed(cx, QUESTION_MARK_USED, expr.hir_id) {
self.check_is_none_or_err_and_early_return(cx, expr); self.check_is_none_or_err_and_early_return(cx, expr);
self.check_if_let_some_or_err_and_early_return(cx, expr); self.check_if_let_some_or_err_and_early_return(cx, expr);
} }

View file

@ -273,3 +273,13 @@ const fn issue9175(option: Option<()>) -> Option<()> {
//stuff //stuff
Some(()) Some(())
} }
fn issue12337() -> Option<i32> {
let _: Option<i32> = try {
let Some(_) = Some(42) else {
return None;
};
123
};
Some(42)
}

View file

@ -313,3 +313,13 @@ const fn issue9175(option: Option<()>) -> Option<()> {
//stuff //stuff
Some(()) Some(())
} }
fn issue12337() -> Option<i32> {
let _: Option<i32> = try {
let Some(_) = Some(42) else {
return None;
};
123
};
Some(42)
}