Skip to content

Commit adc5bc9

Browse files
committed
Auto merge of rust-lang#12108 - samueltardieu:issue-12103, r=xFrednet
Do not suggest `bool::then()` and `bool::then_some` in `const` contexts Fix rust-lang#12103 changelog: [`if_then_some_else_none`]: Do not trigger in `const` contexts
2 parents 0e5dc8e + 5b7a0de commit adc5bc9

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

clippy_lints/src/if_then_some_else_none.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use clippy_utils::diagnostics::span_lint_and_help;
33
use clippy_utils::eager_or_lazy::switch_to_eager_eval;
44
use clippy_utils::source::snippet_with_context;
55
use clippy_utils::sugg::Sugg;
6-
use clippy_utils::{contains_return, higher, is_else_clause, is_res_lang_ctor, path_res, peel_blocks};
6+
use clippy_utils::{contains_return, higher, in_constant, is_else_clause, is_res_lang_ctor, path_res, peel_blocks};
77
use rustc_errors::Applicability;
88
use rustc_hir::LangItem::{OptionNone, OptionSome};
99
use rustc_hir::{Expr, ExprKind};
@@ -74,6 +74,11 @@ impl<'tcx> LateLintPass<'tcx> for IfThenSomeElseNone {
7474
return;
7575
}
7676

77+
// `bool::then()` and `bool::then_some()` are not const
78+
if in_constant(cx, expr.hir_id) {
79+
return;
80+
}
81+
7782
let ctxt = expr.span.ctxt();
7883

7984
if let Some(higher::If {

tests/ui/if_then_some_else_none.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,8 @@ fn issue11394(b: bool, v: Result<(), ()>) -> Result<(), ()> {
130130

131131
Ok(())
132132
}
133+
134+
const fn issue12103(x: u32) -> Option<u32> {
135+
// Should not issue an error in `const` context
136+
if x > 42 { Some(150) } else { None }
137+
}

0 commit comments

Comments
 (0)