Skip to content

Commit 78dc616

Browse files
committed
Auto merge of #9487 - kraktus:question_mark, r=Jarcho
Silence [`question_mark`] in const context fix #9175 When `const_try` is stabilised can be turned into a MSRV changelog: Silence [`question_mark`] in const context
2 parents cf93865 + 3ca6b9d commit 78dc616

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

clippy_lints/src/question_mark.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use clippy_utils::higher;
33
use clippy_utils::source::snippet_with_applicability;
44
use clippy_utils::ty::is_type_diagnostic_item;
55
use clippy_utils::{
6-
eq_expr_value, get_parent_node, is_else_clause, is_lang_ctor, path_to_local, path_to_local_id, peel_blocks,
7-
peel_blocks_with_stmt,
6+
eq_expr_value, get_parent_node, in_constant, is_else_clause, is_lang_ctor, path_to_local, path_to_local_id,
7+
peel_blocks, peel_blocks_with_stmt,
88
};
99
use if_chain::if_chain;
1010
use rustc_errors::Applicability;
@@ -222,7 +222,9 @@ fn expr_return_none_or_err(
222222

223223
impl<'tcx> LateLintPass<'tcx> for QuestionMark {
224224
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
225-
check_is_none_or_err_and_early_return(cx, expr);
226-
check_if_let_some_or_err_and_early_return(cx, expr);
225+
if !in_constant(cx, expr.hir_id) {
226+
check_is_none_or_err_and_early_return(cx, expr);
227+
check_if_let_some_or_err_and_early_return(cx, expr);
228+
}
227229
}
228230
}

tests/ui/question_mark.fixed

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,12 @@ fn pattern() -> Result<(), PatternedError> {
223223
}
224224

225225
fn main() {}
226+
227+
// should not lint, `?` operator not available in const context
228+
const fn issue9175(option: Option<()>) -> Option<()> {
229+
if option.is_none() {
230+
return None;
231+
}
232+
//stuff
233+
Some(())
234+
}

tests/ui/question_mark.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,12 @@ fn pattern() -> Result<(), PatternedError> {
259259
}
260260

261261
fn main() {}
262+
263+
// should not lint, `?` operator not available in const context
264+
const fn issue9175(option: Option<()>) -> Option<()> {
265+
if option.is_none() {
266+
return None;
267+
}
268+
//stuff
269+
Some(())
270+
}

0 commit comments

Comments
 (0)