Skip to content

Commit 78fbb04

Browse files
committed
Auto merge of rust-lang#6071 - ebroto:rustup, r=ebroto
Rustup r? `@ghost` changelog: none
2 parents d88b9b7 + 4117ae1 commit 78fbb04

10 files changed

+415
-473
lines changed

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) && can_suggest(cx, hir_id, sym!(result_type), "is_ok") {
1473-
return Some("is_ok()");
1474-
}
1475-
if match_qpath(path, &paths::RESULT_ERR) && can_suggest(cx, hir_id, sym!(result_type), "is_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-
|| can_suggest(cx, hir_id, sym!(result_type), "is_ok"),
1566-
|| can_suggest(cx, hir_id, sym!(result_type), "is_err"),
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]

tests/ui/redundant_pattern_matching.fixed

+14-85
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,14 @@ fn main() {
1818

1919
if Err::<i32, i32>(42).is_err() {}
2020

21-
if None::<()>.is_none() {}
22-
23-
if Some(42).is_some() {}
24-
25-
if Some(42).is_some() {
26-
foo();
27-
} else {
28-
bar();
29-
}
30-
31-
while Some(42).is_some() {}
32-
33-
while Some(42).is_none() {}
34-
35-
while None::<()>.is_none() {}
36-
3721
while Ok::<i32, i32>(10).is_ok() {}
3822

3923
while Ok::<i32, i32>(10).is_err() {}
4024

41-
let mut v = vec![1, 2, 3];
42-
while v.pop().is_some() {
43-
foo();
44-
}
45-
4625
if Ok::<i32, i32>(42).is_ok() {}
4726

4827
if Err::<i32, i32>(42).is_err() {}
4928

50-
if None::<i32>.is_none() {}
51-
52-
if Some(42).is_some() {}
53-
5429
if let Ok(x) = Ok::<i32, i32>(42) {
5530
println!("{}", x);
5631
}
@@ -63,48 +38,24 @@ fn main() {
6338

6439
Err::<i32, i32>(42).is_ok();
6540

66-
Some(42).is_some();
67-
68-
None::<()>.is_none();
69-
70-
let _ = None::<()>.is_none();
71-
7241
let _ = if Ok::<usize, ()>(4).is_ok() { true } else { false };
7342

74-
let opt = Some(false);
75-
let x = if opt.is_some() { true } else { false };
76-
takes_bool(x);
77-
7843
issue5504();
79-
issue5697();
44+
issue6067();
8045

81-
let _ = if gen_opt().is_some() {
46+
let _ = if gen_res().is_ok() {
8247
1
83-
} else if gen_opt().is_none() {
84-
2
85-
} else if gen_res().is_ok() {
86-
3
8748
} else if gen_res().is_err() {
88-
4
49+
2
8950
} else {
90-
5
51+
3
9152
};
9253
}
9354

94-
fn gen_opt() -> Option<()> {
95-
None
96-
}
97-
9855
fn gen_res() -> Result<(), ()> {
9956
Ok(())
10057
}
10158

102-
fn takes_bool(_: bool) {}
103-
104-
fn foo() {}
105-
106-
fn bar() {}
107-
10859
macro_rules! m {
10960
() => {
11061
Some(42u32)
@@ -128,41 +79,19 @@ fn issue5504() {
12879
while m!().is_some() {}
12980
}
13081

131-
// None of these should be linted because none of the suggested methods
132-
// are `const fn` without toggling a feature.
133-
const fn issue5697() {
134-
if let Ok(_) = Ok::<i32, i32>(42) {}
135-
136-
if let Err(_) = Err::<i32, i32>(42) {}
137-
138-
if let Some(_) = Some(42) {}
139-
140-
if let None = None::<()> {}
141-
142-
while let Ok(_) = Ok::<i32, i32>(10) {}
143-
144-
while let Err(_) = Ok::<i32, i32>(10) {}
82+
// Methods that are unstable const should not be suggested within a const context, see issue #5697.
83+
// However, in Rust 1.48.0 the methods `is_ok` and `is_err` of `Result` were stabilized as const,
84+
// so the following should be linted.
85+
const fn issue6067() {
86+
if Ok::<i32, i32>(42).is_ok() {}
14587

146-
while let Some(_) = Some(42) {}
88+
if Err::<i32, i32>(42).is_err() {}
14789

148-
while let None = None::<()> {}
90+
while Ok::<i32, i32>(10).is_ok() {}
14991

150-
match Ok::<i32, i32>(42) {
151-
Ok(_) => true,
152-
Err(_) => false,
153-
};
92+
while Ok::<i32, i32>(10).is_err() {}
15493

155-
match Err::<i32, i32>(42) {
156-
Ok(_) => false,
157-
Err(_) => true,
158-
};
159-
match Some(42) {
160-
Some(_) => true,
161-
None => false,
162-
};
94+
Ok::<i32, i32>(42).is_ok();
16395

164-
match None::<()> {
165-
Some(_) => false,
166-
None => true,
167-
};
96+
Err::<i32, i32>(42).is_err();
16897
}

0 commit comments

Comments
 (0)