diff options
| author | bors <bors@rust-lang.org> | 2024-01-07 13:49:58 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-01-07 13:49:58 +0000 |
| commit | adc5bc93fc92ee7cb09de956f31b570c0c775407 (patch) | |
| tree | 23e612d0687345835079fc15092d6d46244df1d1 | |
| parent | 0e5dc8e630038338ac4432c5f7553573ef15a61c (diff) | |
| parent | 5b7a0de3e776f3cd231722d66812db9f37c4d6a0 (diff) | |
| download | rust-adc5bc93fc92ee7cb09de956f31b570c0c775407.tar.gz rust-adc5bc93fc92ee7cb09de956f31b570c0c775407.zip | |
Auto merge of #12108 - samueltardieu:issue-12103, r=xFrednet
Do not suggest `bool::then()` and `bool::then_some` in `const` contexts Fix #12103 changelog: [`if_then_some_else_none`]: Do not trigger in `const` contexts
| -rw-r--r-- | clippy_lints/src/if_then_some_else_none.rs | 7 | ||||
| -rw-r--r-- | tests/ui/if_then_some_else_none.rs | 5 |
2 files changed, 11 insertions, 1 deletions
diff --git a/clippy_lints/src/if_then_some_else_none.rs b/clippy_lints/src/if_then_some_else_none.rs index cd6c46a71a8..8f48941c4a9 100644 --- a/clippy_lints/src/if_then_some_else_none.rs +++ b/clippy_lints/src/if_then_some_else_none.rs @@ -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 { diff --git a/tests/ui/if_then_some_else_none.rs b/tests/ui/if_then_some_else_none.rs index abc92459148..ccde154bd56 100644 --- a/tests/ui/if_then_some_else_none.rs +++ b/tests/ui/if_then_some_else_none.rs @@ -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 } +} |
