about summary refs log tree commit diff
diff options
context:
space:
mode:
authory21 <30553356+y21@users.noreply.github.com>2024-02-23 19:16:41 +0100
committery21 <30553356+y21@users.noreply.github.com>2024-02-27 23:49:07 +0100
commit0671d782836665600666223f3c9566a1d36559b4 (patch)
tree6356ed988188356fc4403c17d6889c7c2225c0fd
parent4c1d05cfa193012eb197119b4614dd979d7f6004 (diff)
downloadrust-0671d782836665600666223f3c9566a1d36559b4.tar.gz
rust-0671d782836665600666223f3c9566a1d36559b4.zip
check for try blocks in `LintPass` methods
-rw-r--r--clippy_lints/src/question_mark.rs10
-rw-r--r--tests/ui/question_mark.fixed10
-rw-r--r--tests/ui/question_mark.rs10
3 files changed, 24 insertions, 6 deletions
diff --git a/clippy_lints/src/question_mark.rs b/clippy_lints/src/question_mark.rs
index 1df2d5e4602..6adbc4a23af 100644
--- a/clippy_lints/src/question_mark.rs
+++ b/clippy_lints/src/question_mark.rs
@@ -224,8 +224,7 @@ impl QuestionMark {
     ///
     /// 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>) {
-        if !self.inside_try_block()
-            && let Some(higher::If { cond, then, r#else }) = higher::If::hir(expr)
+        if let Some(higher::If { cond, then, r#else }) = higher::If::hir(expr)
             && !is_else_clause(cx.tcx, expr)
             && let ExprKind::MethodCall(segment, caller, ..) = &cond.kind
             && 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>) {
-        if !self.inside_try_block()
-            && let Some(higher::IfLet {
+        if let Some(higher::IfLet {
                 let_pat,
                 let_expr,
                 if_then,
@@ -324,13 +322,13 @@ impl<'tcx> LateLintPass<'tcx> for QuestionMark {
             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);
         }
         self.check_manual_let_else(cx, stmt);
     }
     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_if_let_some_or_err_and_early_return(cx, expr);
         }
diff --git a/tests/ui/question_mark.fixed b/tests/ui/question_mark.fixed
index 2ef006c1419..567472a8af2 100644
--- a/tests/ui/question_mark.fixed
+++ b/tests/ui/question_mark.fixed
@@ -273,3 +273,13 @@ const fn issue9175(option: Option<()>) -> Option<()> {
     //stuff
     Some(())
 }
+
+fn issue12337() -> Option<i32> {
+    let _: Option<i32> = try {
+        let Some(_) = Some(42) else {
+            return None;
+        };
+        123
+    };
+    Some(42)
+}
diff --git a/tests/ui/question_mark.rs b/tests/ui/question_mark.rs
index c170669823f..abf8c270de8 100644
--- a/tests/ui/question_mark.rs
+++ b/tests/ui/question_mark.rs
@@ -313,3 +313,13 @@ const fn issue9175(option: Option<()>) -> Option<()> {
     //stuff
     Some(())
 }
+
+fn issue12337() -> Option<i32> {
+    let _: Option<i32> = try {
+        let Some(_) = Some(42) else {
+            return None;
+        };
+        123
+    };
+    Some(42)
+}