Skip to content

Commit

Permalink
Auto merge of #9487 - kraktus:question_mark, r=Jarcho
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Sep 27, 2022
2 parents cf93865 + 3ca6b9d commit 78dc616
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
10 changes: 6 additions & 4 deletions clippy_lints/src/question_mark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
9 changes: 9 additions & 0 deletions tests/ui/question_mark.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
9 changes: 9 additions & 0 deletions tests/ui/question_mark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}

0 comments on commit 78dc616

Please sign in to comment.