about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-09-27 01:12:54 +0000
committerbors <bors@rust-lang.org>2022-09-27 01:12:54 +0000
commit78dc616a7ae9815ea6bacccbb3aa40d27ed5eb6c (patch)
tree27108e7df49bf2388f2a37a1b1457a32076593b2
parentcf93865a5c9f651dfec6f59ce13db4b379339218 (diff)
parent3ca6b9d03193128e00d48101306d6d3cefc2f241 (diff)
downloadrust-78dc616a7ae9815ea6bacccbb3aa40d27ed5eb6c.tar.gz
rust-78dc616a7ae9815ea6bacccbb3aa40d27ed5eb6c.zip
Auto merge of #9487 - kraktus:question_mark, r=Jarcho
Silence [`question_mark`] in const context

fix https://github.com/rust-lang/rust-clippy/issues/9175

When `const_try` is stabilised can be turned into a MSRV

changelog: Silence [`question_mark`] in const context
-rw-r--r--clippy_lints/src/question_mark.rs10
-rw-r--r--tests/ui/question_mark.fixed9
-rw-r--r--tests/ui/question_mark.rs9
3 files changed, 24 insertions, 4 deletions
diff --git a/clippy_lints/src/question_mark.rs b/clippy_lints/src/question_mark.rs
index d53614722aa..d8f34a15fa6 100644
--- a/clippy_lints/src/question_mark.rs
+++ b/clippy_lints/src/question_mark.rs
@@ -3,8 +3,8 @@ use clippy_utils::higher;
 use clippy_utils::source::snippet_with_applicability;
 use clippy_utils::ty::is_type_diagnostic_item;
 use clippy_utils::{
-    eq_expr_value, get_parent_node, is_else_clause, is_lang_ctor, path_to_local, path_to_local_id, peel_blocks,
-    peel_blocks_with_stmt,
+    eq_expr_value, get_parent_node, in_constant, is_else_clause, is_lang_ctor, path_to_local, path_to_local_id,
+    peel_blocks, peel_blocks_with_stmt,
 };
 use if_chain::if_chain;
 use rustc_errors::Applicability;
@@ -222,7 +222,9 @@ fn expr_return_none_or_err(
 
 impl<'tcx> LateLintPass<'tcx> for QuestionMark {
     fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
-        check_is_none_or_err_and_early_return(cx, expr);
-        check_if_let_some_or_err_and_early_return(cx, expr);
+        if !in_constant(cx, expr.hir_id) {
+            check_is_none_or_err_and_early_return(cx, expr);
+            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 57f23bd1916..993389232cc 100644
--- a/tests/ui/question_mark.fixed
+++ b/tests/ui/question_mark.fixed
@@ -223,3 +223,12 @@ fn pattern() -> Result<(), PatternedError> {
 }
 
 fn main() {}
+
+// should not lint, `?` operator not available in const context
+const fn issue9175(option: Option<()>) -> Option<()> {
+    if option.is_none() {
+        return None;
+    }
+    //stuff
+    Some(())
+}
diff --git a/tests/ui/question_mark.rs b/tests/ui/question_mark.rs
index 436f027c215..9ae0d88829a 100644
--- a/tests/ui/question_mark.rs
+++ b/tests/ui/question_mark.rs
@@ -259,3 +259,12 @@ fn pattern() -> Result<(), PatternedError> {
 }
 
 fn main() {}
+
+// should not lint, `?` operator not available in const context
+const fn issue9175(option: Option<()>) -> Option<()> {
+    if option.is_none() {
+        return None;
+    }
+    //stuff
+    Some(())
+}