-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Rustup to rustc 1.39.0-nightly (acf7b50c7 2019-09-25) #4574
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -193,7 +193,7 @@ fn lint_match_arms<'tcx>(cx: &LateContext<'_, 'tcx>, expr: &Expr) { | |
(min_index..=max_index).all(|index| arms[index].guard.is_none()) && | ||
SpanlessEq::new(cx).eq_expr(&lhs.body, &rhs.body) && | ||
// all patterns should have the same bindings | ||
same_bindings(cx, &bindings(cx, &lhs.pats[0]), &bindings(cx, &rhs.pats[0])) | ||
same_bindings(cx, &bindings(cx, &lhs.pat), &bindings(cx, &rhs.pat)) | ||
}; | ||
|
||
let indexed_arms: Vec<(usize, &Arm)> = arms.iter().enumerate().collect(); | ||
|
@@ -213,27 +213,22 @@ fn lint_match_arms<'tcx>(cx: &LateContext<'_, 'tcx>, expr: &Expr) { | |
// span for the whole pattern, the suggestion is only shown when there is only | ||
// one pattern. The user should know about `|` if they are already using it… | ||
|
||
if i.pats.len() == 1 && j.pats.len() == 1 { | ||
let lhs = snippet(cx, i.pats[0].span, "<pat1>"); | ||
let rhs = snippet(cx, j.pats[0].span, "<pat2>"); | ||
|
||
if let PatKind::Wild = j.pats[0].node { | ||
// if the last arm is _, then i could be integrated into _ | ||
// note that i.pats[0] cannot be _, because that would mean that we're | ||
// hiding all the subsequent arms, and rust won't compile | ||
db.span_note( | ||
i.body.span, | ||
&format!( | ||
"`{}` has the same arm body as the `_` wildcard, consider removing it`", | ||
lhs | ||
), | ||
); | ||
} else { | ||
db.span_help( | ||
i.pats[0].span, | ||
&format!("consider refactoring into `{} | {}`", lhs, rhs), | ||
); | ||
} | ||
let lhs = snippet(cx, i.pat.span, "<pat1>"); | ||
let rhs = snippet(cx, j.pat.span, "<pat2>"); | ||
|
||
if let PatKind::Wild = j.pat.node { | ||
// if the last arm is _, then i could be integrated into _ | ||
// note that i.pat cannot be _, because that would mean that we're | ||
// hiding all the subsequent arms, and rust won't compile | ||
db.span_note( | ||
i.body.span, | ||
&format!( | ||
"`{}` has the same arm body as the `_` wildcard, consider removing it`", | ||
lhs | ||
), | ||
); | ||
} else { | ||
db.span_help(i.pat.span, &format!("consider refactoring into `{} | {}`", lhs, rhs)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aside: In the spirit of this lint, in the future you may want a lint that suggests |
||
} | ||
}, | ||
); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,6 @@ use rustc::{declare_lint_pass, declare_tool_lint}; | |
use rustc_errors::Applicability; | ||
use std::cmp::Ordering; | ||
use std::collections::Bound; | ||
use std::ops::Deref; | ||
use syntax::ast::LitKind; | ||
use syntax::source_map::Span; | ||
|
||
|
@@ -255,9 +254,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Matches { | |
|
||
#[rustfmt::skip] | ||
fn check_single_match(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm], expr: &Expr) { | ||
if arms.len() == 2 && | ||
arms[0].pats.len() == 1 && arms[0].guard.is_none() && | ||
arms[1].pats.len() == 1 && arms[1].guard.is_none() { | ||
if arms.len() == 2 && arms[0].guard.is_none() && arms[1].guard.is_none() { | ||
if let PatKind::Or(..) = arms[0].pat.node { | ||
// don't lint for or patterns for now, this makes | ||
// the lint noisy in unnecessary situations | ||
return; | ||
} | ||
let els = remove_blocks(&arms[1].body); | ||
let els = if is_unit_expr(els) { | ||
None | ||
|
@@ -283,7 +285,7 @@ fn check_single_match_single_pattern( | |
expr: &Expr, | ||
els: Option<&Expr>, | ||
) { | ||
if is_wild(&arms[1].pats[0]) { | ||
if is_wild(&arms[1].pat) { | ||
report_single_match_single_pattern(cx, ex, arms, expr, els); | ||
} | ||
} | ||
|
@@ -308,7 +310,7 @@ fn report_single_match_single_pattern( | |
"try this", | ||
format!( | ||
"if let {} = {} {}{}", | ||
snippet(cx, arms[0].pats[0].span, ".."), | ||
snippet(cx, arms[0].pat.span, ".."), | ||
snippet(cx, ex.span, ".."), | ||
expr_block(cx, &arms[0].body, None, ".."), | ||
els_str, | ||
|
@@ -336,7 +338,7 @@ fn check_single_match_opt_like( | |
(&paths::RESULT, "Ok"), | ||
]; | ||
|
||
let path = match arms[1].pats[0].node { | ||
let path = match arms[1].pat.node { | ||
PatKind::TupleStruct(ref path, ref inner, _) => { | ||
// Contains any non wildcard patterns (e.g., `Err(err)`)? | ||
if !inner.iter().all(is_wild) { | ||
|
@@ -365,9 +367,9 @@ fn check_match_bool(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm], expr: &Ex | |
expr.span, | ||
"you seem to be trying to match on a boolean expression", | ||
move |db| { | ||
if arms.len() == 2 && arms[0].pats.len() == 1 { | ||
if arms.len() == 2 { | ||
// no guards | ||
let exprs = if let PatKind::Lit(ref arm_bool) = arms[0].pats[0].node { | ||
let exprs = if let PatKind::Lit(ref arm_bool) = arms[0].pat.node { | ||
if let ExprKind::Lit(ref lit) = arm_bool.node { | ||
match lit.node { | ||
LitKind::Bool(true) => Some((&*arms[0].body, &*arms[1].body)), | ||
|
@@ -446,7 +448,7 @@ fn check_wild_err_arm(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm]) { | |
let ex_ty = walk_ptrs_ty(cx.tables.expr_ty(ex)); | ||
if match_type(cx, ex_ty, &paths::RESULT) { | ||
for arm in arms { | ||
if let PatKind::TupleStruct(ref path, ref inner, _) = arm.pats[0].node { | ||
if let PatKind::TupleStruct(ref path, ref inner, _) = arm.pat.node { | ||
let path_str = print::to_string(print::NO_ANN, |s| s.print_qpath(path, false)); | ||
if_chain! { | ||
if path_str == "Err"; | ||
|
@@ -457,9 +459,9 @@ fn check_wild_err_arm(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm]) { | |
// `Err(_)` arm with `panic!` found | ||
span_note_and_lint(cx, | ||
MATCH_WILD_ERR_ARM, | ||
arm.pats[0].span, | ||
arm.pat.span, | ||
"Err(_) will match all errors, maybe not a good idea", | ||
arm.pats[0].span, | ||
arm.pat.span, | ||
"to remove this warning, match each error separately \ | ||
or use unreachable macro"); | ||
} | ||
|
@@ -482,13 +484,11 @@ fn check_wild_enum_match(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm]) { | |
let mut wildcard_span = None; | ||
let mut wildcard_ident = None; | ||
for arm in arms { | ||
for pat in &arm.pats { | ||
if let PatKind::Wild = pat.node { | ||
wildcard_span = Some(pat.span); | ||
} else if let PatKind::Binding(_, _, ident, None) = pat.node { | ||
wildcard_span = Some(pat.span); | ||
wildcard_ident = Some(ident); | ||
} | ||
if let PatKind::Wild = arm.pat.node { | ||
wildcard_span = Some(arm.pat.span); | ||
} else if let PatKind::Binding(_, _, ident, None) = arm.pat.node { | ||
wildcard_span = Some(arm.pat.span); | ||
wildcard_ident = Some(ident); | ||
} | ||
} | ||
|
||
|
@@ -510,15 +510,13 @@ fn check_wild_enum_match(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm]) { | |
// covered by the set of guards that cover it, but that's really hard to do. | ||
continue; | ||
} | ||
for pat in &arm.pats { | ||
if let PatKind::Path(ref path) = pat.deref().node { | ||
if let QPath::Resolved(_, p) = path { | ||
missing_variants.retain(|e| e.ctor_def_id != Some(p.res.def_id())); | ||
} | ||
} else if let PatKind::TupleStruct(ref path, ..) = pat.deref().node { | ||
if let QPath::Resolved(_, p) = path { | ||
missing_variants.retain(|e| e.ctor_def_id != Some(p.res.def_id())); | ||
} | ||
if let PatKind::Path(ref path) = arm.pat.node { | ||
if let QPath::Resolved(_, p) = path { | ||
missing_variants.retain(|e| e.ctor_def_id != Some(p.res.def_id())); | ||
} | ||
} else if let PatKind::TupleStruct(ref path, ..) = arm.pat.node { | ||
if let QPath::Resolved(_, p) = path { | ||
missing_variants.retain(|e| e.ctor_def_id != Some(p.res.def_id())); | ||
} | ||
} | ||
} | ||
|
@@ -588,9 +586,9 @@ fn check_match_ref_pats(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm], expr: | |
) | ||
}; | ||
|
||
suggs.extend(arms.iter().flat_map(|a| &a.pats).filter_map(|p| { | ||
if let PatKind::Ref(ref refp, _) = p.node { | ||
Some((p.span, snippet(cx, refp.span, "..").to_string())) | ||
suggs.extend(arms.iter().filter_map(|a| { | ||
if let PatKind::Ref(ref refp, _) = a.pat.node { | ||
Some((a.pat.span, snippet(cx, refp.span, "..").to_string())) | ||
} else { | ||
None | ||
} | ||
|
@@ -605,12 +603,7 @@ fn check_match_ref_pats(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm], expr: | |
} | ||
|
||
fn check_match_as_ref(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm], expr: &Expr) { | ||
if arms.len() == 2 | ||
&& arms[0].pats.len() == 1 | ||
&& arms[0].guard.is_none() | ||
&& arms[1].pats.len() == 1 | ||
&& arms[1].guard.is_none() | ||
{ | ||
if arms.len() == 2 && arms[0].guard.is_none() && arms[1].guard.is_none() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel that I've seen this pattern a few times now btw... maybe refactor in a follow up PR. |
||
let arm_ref: Option<BindingAnnotation> = if is_none_arm(&arms[0]) { | ||
is_ref_some_arm(&arms[1]) | ||
} else if is_none_arm(&arms[1]) { | ||
|
@@ -666,14 +659,9 @@ fn all_ranges<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, arms: &'tcx [Arm]) -> Vec<Sp | |
arms.iter() | ||
.flat_map(|arm| { | ||
if let Arm { | ||
ref pats, guard: None, .. | ||
ref pat, guard: None, .. | ||
} = *arm | ||
{ | ||
pats.iter() | ||
} else { | ||
[].iter() | ||
} | ||
.filter_map(|pat| { | ||
if let PatKind::Range(ref lhs, ref rhs, ref range_end) = pat.node { | ||
let lhs = constant(cx, cx.tables, lhs)?.0; | ||
let rhs = constant(cx, cx.tables, rhs)?.0; | ||
|
@@ -694,9 +682,8 @@ fn all_ranges<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, arms: &'tcx [Arm]) -> Vec<Sp | |
node: (value.clone(), Bound::Included(value)), | ||
}); | ||
} | ||
|
||
None | ||
}) | ||
} | ||
None | ||
}) | ||
.collect() | ||
} | ||
|
@@ -743,7 +730,7 @@ fn is_unit_expr(expr: &Expr) -> bool { | |
|
||
// Checks if arm has the form `None => None` | ||
fn is_none_arm(arm: &Arm) -> bool { | ||
match arm.pats[0].node { | ||
match arm.pat.node { | ||
PatKind::Path(ref path) if match_qpath(path, &paths::OPTION_NONE) => true, | ||
_ => false, | ||
} | ||
|
@@ -752,7 +739,7 @@ fn is_none_arm(arm: &Arm) -> bool { | |
// Checks if arm has the form `Some(ref v) => Some(v)` (checks for `ref` and `ref mut`) | ||
fn is_ref_some_arm(arm: &Arm) -> Option<BindingAnnotation> { | ||
if_chain! { | ||
if let PatKind::TupleStruct(ref path, ref pats, _) = arm.pats[0].node; | ||
if let PatKind::TupleStruct(ref path, ref pats, _) = arm.pat.node; | ||
if pats.len() == 1 && match_qpath(path, &paths::OPTION_SOME); | ||
if let PatKind::Binding(rb, .., ident, _) = pats[0].node; | ||
if rb == BindingAnnotation::Ref || rb == BindingAnnotation::RefMut; | ||
|
@@ -772,9 +759,8 @@ fn is_ref_some_arm(arm: &Arm) -> Option<BindingAnnotation> { | |
fn has_only_ref_pats(arms: &[Arm]) -> bool { | ||
let mapped = arms | ||
.iter() | ||
.flat_map(|a| &a.pats) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd just inline into |
||
.map(|p| { | ||
match p.node { | ||
.map(|a| { | ||
match a.pat.node { | ||
PatKind::Ref(..) => Some(true), // &-patterns | ||
PatKind::Wild => Some(false), // an "anything" wildcard is also fine | ||
_ => None, // any other pattern is not fine | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I'm reading the intent of this code you might want to recurse and count each or-pattern as
cc += 1
but I think the change you made makes more sense.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah i debated this but i think we might need a more careful CC accounting for pats here