Skip to content

Commit c3abb82

Browse files
authored
Rollup merge of rust-lang#76135 - CDirkx:const-option, r=dtolnay,oli-obk
Stabilize some Option methods as const Stabilize the following methods of `Option` as const: - `is_some` - `is_none` - `as_ref` These methods are currently const under the unstable feature `const_option` (tracking issue: rust-lang#67441). I believe these methods to be eligible for stabilization because of the stabilization of rust-lang#49146 (Allow if and match in constants) and the trivial implementations, see also: [PR#75463](rust-lang#75463). Related: rust-lang#76225
2 parents a409a23 + 43cba34 commit c3abb82

File tree

7 files changed

+126
-144
lines changed

7 files changed

+126
-144
lines changed

Diff for: library/core/src/option.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ impl<T> Option<T> {
175175
/// ```
176176
#[must_use = "if you intended to assert that this has a value, consider `.unwrap()` instead"]
177177
#[inline]
178-
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
178+
#[rustc_const_stable(feature = "const_option", since = "1.48.0")]
179179
#[stable(feature = "rust1", since = "1.0.0")]
180180
pub const fn is_some(&self) -> bool {
181181
matches!(*self, Some(_))
@@ -195,7 +195,7 @@ impl<T> Option<T> {
195195
#[must_use = "if you intended to assert that this doesn't have a value, consider \
196196
`.and_then(|| panic!(\"`Option` had a value when expected `None`\"))` instead"]
197197
#[inline]
198-
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
198+
#[rustc_const_stable(feature = "const_option", since = "1.48.0")]
199199
#[stable(feature = "rust1", since = "1.0.0")]
200200
pub const fn is_none(&self) -> bool {
201201
!self.is_some()
@@ -254,7 +254,7 @@ impl<T> Option<T> {
254254
/// println!("still can print text: {:?}", text);
255255
/// ```
256256
#[inline]
257-
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
257+
#[rustc_const_stable(feature = "const_option", since = "1.48.0")]
258258
#[stable(feature = "rust1", since = "1.0.0")]
259259
pub const fn as_ref(&self) -> Option<&T> {
260260
match *self {

Diff for: library/core/tests/option.rs

+16
Original file line numberDiff line numberDiff line change
@@ -356,3 +356,19 @@ fn test_replace() {
356356
assert_eq!(x, Some(3));
357357
assert_eq!(old, None);
358358
}
359+
360+
#[test]
361+
fn option_const() {
362+
// test that the methods of `Option` are usable in a const context
363+
364+
const OPTION: Option<usize> = Some(32);
365+
366+
const REF: Option<&usize> = OPTION.as_ref();
367+
assert_eq!(REF, Some(&32));
368+
369+
const IS_SOME: bool = OPTION.is_some();
370+
assert!(IS_SOME);
371+
372+
const IS_NONE: bool = OPTION.is_none();
373+
assert!(!IS_NONE);
374+
}

Diff for: src/test/ui/consts/const-option.rs

-14
This file was deleted.

Diff for: src/tools/clippy/clippy_lints/src/matches.rs

+16-62
Original file line numberDiff line numberDiff line change
@@ -1440,15 +1440,12 @@ where
14401440

14411441
mod redundant_pattern_match {
14421442
use super::REDUNDANT_PATTERN_MATCHING;
1443-
use crate::utils::{in_constant, match_qpath, match_trait_method, paths, snippet, span_lint_and_then};
1443+
use crate::utils::{match_qpath, match_trait_method, paths, snippet, span_lint_and_then};
14441444
use if_chain::if_chain;
14451445
use rustc_ast::ast::LitKind;
14461446
use rustc_errors::Applicability;
1447-
use rustc_hir::{Arm, Expr, ExprKind, HirId, MatchSource, PatKind, QPath};
1447+
use rustc_hir::{Arm, Expr, ExprKind, MatchSource, PatKind, QPath};
14481448
use rustc_lint::LateContext;
1449-
use rustc_middle::ty;
1450-
use rustc_mir::const_eval::is_const_fn;
1451-
use rustc_span::source_map::Symbol;
14521449

14531450
pub fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
14541451
if let ExprKind::Match(op, arms, ref match_source) = &expr.kind {
@@ -1468,37 +1465,24 @@ mod redundant_pattern_match {
14681465
arms: &[Arm<'_>],
14691466
keyword: &'static str,
14701467
) {
1471-
fn find_suggestion(cx: &LateContext<'_>, hir_id: HirId, path: &QPath<'_>) -> Option<&'static str> {
1472-
if match_qpath(path, &paths::RESULT_OK) {
1473-
return Some("is_ok()");
1474-
}
1475-
if match_qpath(path, &paths::RESULT_ERR) {
1476-
return Some("is_err()");
1477-
}
1478-
if match_qpath(path, &paths::OPTION_SOME) && can_suggest(cx, hir_id, sym!(option_type), "is_some") {
1479-
return Some("is_some()");
1480-
}
1481-
if match_qpath(path, &paths::OPTION_NONE) && can_suggest(cx, hir_id, sym!(option_type), "is_none") {
1482-
return Some("is_none()");
1483-
}
1484-
None
1485-
}
1486-
1487-
let hir_id = expr.hir_id;
14881468
let good_method = match arms[0].pat.kind {
14891469
PatKind::TupleStruct(ref path, ref patterns, _) if patterns.len() == 1 => {
14901470
if let PatKind::Wild = patterns[0].kind {
1491-
find_suggestion(cx, hir_id, path)
1471+
if match_qpath(path, &paths::RESULT_OK) {
1472+
"is_ok()"
1473+
} else if match_qpath(path, &paths::RESULT_ERR) {
1474+
"is_err()"
1475+
} else if match_qpath(path, &paths::OPTION_SOME) {
1476+
"is_some()"
1477+
} else {
1478+
return;
1479+
}
14921480
} else {
1493-
None
1481+
return;
14941482
}
14951483
},
1496-
PatKind::Path(ref path) => find_suggestion(cx, hir_id, path),
1497-
_ => None,
1498-
};
1499-
let good_method = match good_method {
1500-
Some(method) => method,
1501-
None => return,
1484+
PatKind::Path(ref path) if match_qpath(path, &paths::OPTION_NONE) => "is_none()",
1485+
_ => return,
15021486
};
15031487

15041488
// check that `while_let_on_iterator` lint does not trigger
@@ -1547,7 +1531,6 @@ mod redundant_pattern_match {
15471531
if arms.len() == 2 {
15481532
let node_pair = (&arms[0].pat.kind, &arms[1].pat.kind);
15491533

1550-
let hir_id = expr.hir_id;
15511534
let found_good_method = match node_pair {
15521535
(
15531536
PatKind::TupleStruct(ref path_left, ref patterns_left, _),
@@ -1562,8 +1545,6 @@ mod redundant_pattern_match {
15621545
&paths::RESULT_ERR,
15631546
"is_ok()",
15641547
"is_err()",
1565-
|| true,
1566-
|| true,
15671548
)
15681549
} else {
15691550
None
@@ -1582,8 +1563,6 @@ mod redundant_pattern_match {
15821563
&paths::OPTION_NONE,
15831564
"is_some()",
15841565
"is_none()",
1585-
|| can_suggest(cx, hir_id, sym!(option_type), "is_some"),
1586-
|| can_suggest(cx, hir_id, sym!(option_type), "is_none"),
15871566
)
15881567
} else {
15891568
None
@@ -1616,7 +1595,6 @@ mod redundant_pattern_match {
16161595
}
16171596
}
16181597

1619-
#[allow(clippy::too_many_arguments)]
16201598
fn find_good_method_for_match<'a>(
16211599
arms: &[Arm<'_>],
16221600
path_left: &QPath<'_>,
@@ -1625,8 +1603,6 @@ mod redundant_pattern_match {
16251603
expected_right: &[&str],
16261604
should_be_left: &'a str,
16271605
should_be_right: &'a str,
1628-
can_suggest_left: impl Fn() -> bool,
1629-
can_suggest_right: impl Fn() -> bool,
16301606
) -> Option<&'a str> {
16311607
let body_node_pair = if match_qpath(path_left, expected_left) && match_qpath(path_right, expected_right) {
16321608
(&(*arms[0].body).kind, &(*arms[1].body).kind)
@@ -1638,35 +1614,13 @@ mod redundant_pattern_match {
16381614

16391615
match body_node_pair {
16401616
(ExprKind::Lit(ref lit_left), ExprKind::Lit(ref lit_right)) => match (&lit_left.node, &lit_right.node) {
1641-
(LitKind::Bool(true), LitKind::Bool(false)) if can_suggest_left() => Some(should_be_left),
1642-
(LitKind::Bool(false), LitKind::Bool(true)) if can_suggest_right() => Some(should_be_right),
1617+
(LitKind::Bool(true), LitKind::Bool(false)) => Some(should_be_left),
1618+
(LitKind::Bool(false), LitKind::Bool(true)) => Some(should_be_right),
16431619
_ => None,
16441620
},
16451621
_ => None,
16461622
}
16471623
}
1648-
1649-
fn can_suggest(cx: &LateContext<'_>, hir_id: HirId, diag_item: Symbol, name: &str) -> bool {
1650-
if !in_constant(cx, hir_id) {
1651-
return true;
1652-
}
1653-
1654-
// Avoid suggesting calls to non-`const fn`s in const contexts, see #5697.
1655-
cx.tcx
1656-
.get_diagnostic_item(diag_item)
1657-
.and_then(|def_id| {
1658-
cx.tcx.inherent_impls(def_id).iter().find_map(|imp| {
1659-
cx.tcx
1660-
.associated_items(*imp)
1661-
.in_definition_order()
1662-
.find_map(|item| match item.kind {
1663-
ty::AssocKind::Fn if item.ident.name.as_str() == name => Some(item.def_id),
1664-
_ => None,
1665-
})
1666-
})
1667-
})
1668-
.map_or(false, |def_id| is_const_fn(cx.tcx, def_id))
1669-
}
16701624
}
16711625

16721626
#[test]

Diff for: src/tools/clippy/tests/ui/redundant_pattern_matching.fixed

+14-25
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ fn main() {
7676
takes_bool(x);
7777

7878
issue5504();
79-
issue5697();
8079
issue6067();
8180

8281
let _ = if gen_opt().is_some() {
@@ -129,41 +128,31 @@ fn issue5504() {
129128
while m!().is_some() {}
130129
}
131130

132-
// None of these should be linted because none of the suggested methods
133-
// are `const fn` without toggling a feature.
134-
const fn issue5697() {
135-
if let Some(_) = Some(42) {}
136-
137-
if let None = None::<()> {}
138-
139-
while let Some(_) = Some(42) {}
140-
141-
while let None = None::<()> {}
142-
143-
match Some(42) {
144-
Some(_) => true,
145-
None => false,
146-
};
147-
148-
match None::<()> {
149-
Some(_) => false,
150-
None => true,
151-
};
152-
}
153-
154131
// Methods that are unstable const should not be suggested within a const context, see issue #5697.
155-
// However, in Rust 1.48.0 the methods `is_ok` and `is_err` of `Result` were stabilized as const,
156-
// so the following should be linted.
132+
// However, in Rust 1.48.0 the methods `is_ok` and `is_err` of `Result`, and `is_some` and `is_none`
133+
// of `Option` were stabilized as const, so the following should be linted.
157134
const fn issue6067() {
158135
if Ok::<i32, i32>(42).is_ok() {}
159136

160137
if Err::<i32, i32>(42).is_err() {}
161138

139+
if Some(42).is_some() {}
140+
141+
if None::<()>.is_none() {}
142+
162143
while Ok::<i32, i32>(10).is_ok() {}
163144

164145
while Ok::<i32, i32>(10).is_err() {}
165146

147+
while Some(42).is_some() {}
148+
149+
while None::<()>.is_none() {}
150+
166151
Ok::<i32, i32>(42).is_ok();
167152

168153
Err::<i32, i32>(42).is_err();
154+
155+
Some(42).is_some();
156+
157+
None::<()>.is_none();
169158
}

Diff for: src/tools/clippy/tests/ui/redundant_pattern_matching.rs

+20-25
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ fn main() {
9797
takes_bool(x);
9898

9999
issue5504();
100-
issue5697();
101100
issue6067();
102101

103102
let _ = if let Some(_) = gen_opt() {
@@ -150,40 +149,26 @@ fn issue5504() {
150149
while let Some(_) = m!() {}
151150
}
152151

153-
// None of these should be linted because none of the suggested methods
154-
// are `const fn` without toggling a feature.
155-
const fn issue5697() {
156-
if let Some(_) = Some(42) {}
157-
158-
if let None = None::<()> {}
159-
160-
while let Some(_) = Some(42) {}
161-
162-
while let None = None::<()> {}
163-
164-
match Some(42) {
165-
Some(_) => true,
166-
None => false,
167-
};
168-
169-
match None::<()> {
170-
Some(_) => false,
171-
None => true,
172-
};
173-
}
174-
175152
// Methods that are unstable const should not be suggested within a const context, see issue #5697.
176-
// However, in Rust 1.48.0 the methods `is_ok` and `is_err` of `Result` were stabilized as const,
177-
// so the following should be linted.
153+
// However, in Rust 1.48.0 the methods `is_ok` and `is_err` of `Result`, and `is_some` and `is_none`
154+
// of `Option` were stabilized as const, so the following should be linted.
178155
const fn issue6067() {
179156
if let Ok(_) = Ok::<i32, i32>(42) {}
180157

181158
if let Err(_) = Err::<i32, i32>(42) {}
182159

160+
if let Some(_) = Some(42) {}
161+
162+
if let None = None::<()> {}
163+
183164
while let Ok(_) = Ok::<i32, i32>(10) {}
184165

185166
while let Err(_) = Ok::<i32, i32>(10) {}
186167

168+
while let Some(_) = Some(42) {}
169+
170+
while let None = None::<()> {}
171+
187172
match Ok::<i32, i32>(42) {
188173
Ok(_) => true,
189174
Err(_) => false,
@@ -193,4 +178,14 @@ const fn issue6067() {
193178
Ok(_) => false,
194179
Err(_) => true,
195180
};
181+
182+
match Some(42) {
183+
Some(_) => true,
184+
None => false,
185+
};
186+
187+
match None::<()> {
188+
Some(_) => false,
189+
None => true,
190+
};
196191
}

0 commit comments

Comments
 (0)