Skip to content

Commit 0671d78

Browse files
committed
check for try blocks in LintPass methods
1 parent 4c1d05c commit 0671d78

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

clippy_lints/src/question_mark.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,7 @@ impl QuestionMark {
224224
///
225225
/// If it matches, it will suggest to use the question mark operator instead
226226
fn check_is_none_or_err_and_early_return<'tcx>(&self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) {
227-
if !self.inside_try_block()
228-
&& let Some(higher::If { cond, then, r#else }) = higher::If::hir(expr)
227+
if let Some(higher::If { cond, then, r#else }) = higher::If::hir(expr)
229228
&& !is_else_clause(cx.tcx, expr)
230229
&& let ExprKind::MethodCall(segment, caller, ..) = &cond.kind
231230
&& let caller_ty = cx.typeck_results().expr_ty(caller)
@@ -259,8 +258,7 @@ impl QuestionMark {
259258
}
260259

261260
fn check_if_let_some_or_err_and_early_return<'tcx>(&self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) {
262-
if !self.inside_try_block()
263-
&& let Some(higher::IfLet {
261+
if let Some(higher::IfLet {
264262
let_pat,
265263
let_expr,
266264
if_then,
@@ -324,13 +322,13 @@ impl<'tcx> LateLintPass<'tcx> for QuestionMark {
324322
return;
325323
}
326324

327-
if !in_constant(cx, stmt.hir_id) {
325+
if !self.inside_try_block() && !in_constant(cx, stmt.hir_id) {
328326
check_let_some_else_return_none(cx, stmt);
329327
}
330328
self.check_manual_let_else(cx, stmt);
331329
}
332330
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
333-
if !in_constant(cx, expr.hir_id) && is_lint_allowed(cx, QUESTION_MARK_USED, expr.hir_id) {
331+
if !self.inside_try_block() && !in_constant(cx, expr.hir_id) && is_lint_allowed(cx, QUESTION_MARK_USED, expr.hir_id) {
334332
self.check_is_none_or_err_and_early_return(cx, expr);
335333
self.check_if_let_some_or_err_and_early_return(cx, expr);
336334
}

tests/ui/question_mark.fixed

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,13 @@ const fn issue9175(option: Option<()>) -> Option<()> {
273273
//stuff
274274
Some(())
275275
}
276+
277+
fn issue12337() -> Option<i32> {
278+
let _: Option<i32> = try {
279+
let Some(_) = Some(42) else {
280+
return None;
281+
};
282+
123
283+
};
284+
Some(42)
285+
}

tests/ui/question_mark.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,3 +313,13 @@ const fn issue9175(option: Option<()>) -> Option<()> {
313313
//stuff
314314
Some(())
315315
}
316+
317+
fn issue12337() -> Option<i32> {
318+
let _: Option<i32> = try {
319+
let Some(_) = Some(42) else {
320+
return None;
321+
};
322+
123
323+
};
324+
Some(42)
325+
}

0 commit comments

Comments
 (0)