Do not suggest bool::then() and bool::then_some in const contexts

This commit is contained in:
Samuel Tardieu 2024-01-07 10:42:00 +01:00
parent 0e5dc8e630
commit 5b7a0de3e7
2 changed files with 11 additions and 1 deletions

View file

@ -3,7 +3,7 @@ use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::eager_or_lazy::switch_to_eager_eval;
use clippy_utils::source::snippet_with_context;
use clippy_utils::sugg::Sugg;
use clippy_utils::{contains_return, higher, is_else_clause, is_res_lang_ctor, path_res, peel_blocks};
use clippy_utils::{contains_return, higher, in_constant, is_else_clause, is_res_lang_ctor, path_res, peel_blocks};
use rustc_errors::Applicability;
use rustc_hir::LangItem::{OptionNone, OptionSome};
use rustc_hir::{Expr, ExprKind};
@ -74,6 +74,11 @@ impl<'tcx> LateLintPass<'tcx> for IfThenSomeElseNone {
return;
}
// `bool::then()` and `bool::then_some()` are not const
if in_constant(cx, expr.hir_id) {
return;
}
let ctxt = expr.span.ctxt();
if let Some(higher::If {

View file

@ -130,3 +130,8 @@ fn issue11394(b: bool, v: Result<(), ()>) -> Result<(), ()> {
Ok(())
}
const fn issue12103(x: u32) -> Option<u32> {
// Should not issue an error in `const` context
if x > 42 { Some(150) } else { None }
}