Skip to content

Commit 3ca6b9d

Browse files
committed
Silence [question_mark] in const context
1 parent 481dc2e commit 3ca6b9d

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;
@@ -223,7 +223,9 @@ fn expr_return_none_or_err(
223223

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

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)