Fix lint warnings

This commit is contained in:
Michael Wright 2019-09-12 08:36:05 +02:00
parent 4a3bc6b592
commit 00ca42fe5b
4 changed files with 63 additions and 58 deletions

View File

@ -101,7 +101,7 @@ struct DivergenceVisitor<'a, 'tcx> {
impl<'a, 'tcx> DivergenceVisitor<'a, 'tcx> { impl<'a, 'tcx> DivergenceVisitor<'a, 'tcx> {
fn maybe_walk_expr(&mut self, e: &'tcx Expr) { fn maybe_walk_expr(&mut self, e: &'tcx Expr) {
match e.node { match e.node {
ExprKind::Closure(.., _) => {}, ExprKind::Closure(..) => {},
ExprKind::Match(ref e, ref arms, _) => { ExprKind::Match(ref e, ref arms, _) => {
self.visit_expr(e); self.visit_expr(e);
for arm in arms { for arm in arms {

View File

@ -362,61 +362,7 @@ fn check_pat(&mut self, cx: &EarlyContext<'_>, pat: &Pat) {
} }
} }
if let PatKind::TupleStruct(_, ref patterns) | PatKind::Tuple(ref patterns) = pat.node { check_unneeded_wildcard_pattern(cx, pat);
fn span_lint(cx: &EarlyContext<'_>, span: Span, only_one: bool) {
span_lint_and_sugg(
cx,
UNNEEDED_WILDCARD_PATTERN,
span,
if only_one {
"this pattern is unneeded as the `..` pattern can match that element"
} else {
"these patterns are unneeded as the `..` pattern can match those elements"
},
if only_one { "remove it" } else { "remove them" },
"".to_string(),
Applicability::MachineApplicable,
);
}
fn is_rest<P: std::ops::Deref<Target = Pat>>(pat: &P) -> bool {
if let PatKind::Rest = pat.node {
true
} else {
false
}
}
fn is_wild<P: std::ops::Deref<Target = Pat>>(pat: &&P) -> bool {
if let PatKind::Wild = pat.node {
true
} else {
false
}
}
if let Some(rest_index) = patterns.iter().position(is_rest) {
if let Some((left_index, left_pat)) = patterns[..rest_index]
.iter()
.rev()
.take_while(is_wild)
.enumerate()
.last()
{
span_lint(cx, left_pat.span.until(patterns[rest_index].span), left_index == 0);
}
if let Some((right_index, right_pat)) =
patterns[rest_index + 1..].iter().take_while(is_wild).enumerate().last()
{
span_lint(
cx,
patterns[rest_index].span.shrink_to_hi().to(right_pat.span),
right_index == 0,
);
}
}
}
} }
fn check_fn(&mut self, cx: &EarlyContext<'_>, _: FnKind<'_>, decl: &FnDecl, _: Span, _: NodeId) { fn check_fn(&mut self, cx: &EarlyContext<'_>, _: FnKind<'_>, decl: &FnDecl, _: Span, _: NodeId) {
@ -611,3 +557,62 @@ fn check_lit(self, cx: &EarlyContext<'_>, lit: &Lit) {
} }
} }
} }
fn check_unneeded_wildcard_pattern(cx: &EarlyContext<'_>, pat: &Pat) {
if let PatKind::TupleStruct(_, ref patterns) | PatKind::Tuple(ref patterns) = pat.node {
fn span_lint(cx: &EarlyContext<'_>, span: Span, only_one: bool) {
span_lint_and_sugg(
cx,
UNNEEDED_WILDCARD_PATTERN,
span,
if only_one {
"this pattern is unneeded as the `..` pattern can match that element"
} else {
"these patterns are unneeded as the `..` pattern can match those elements"
},
if only_one { "remove it" } else { "remove them" },
"".to_string(),
Applicability::MachineApplicable,
);
}
fn is_rest<P: std::ops::Deref<Target = Pat>>(pat: &P) -> bool {
if let PatKind::Rest = pat.node {
true
} else {
false
}
}
#[allow(clippy::trivially_copy_pass_by_ref)]
fn is_wild<P: std::ops::Deref<Target = Pat>>(pat: &&P) -> bool {
if let PatKind::Wild = pat.node {
true
} else {
false
}
}
if let Some(rest_index) = patterns.iter().position(is_rest) {
if let Some((left_index, left_pat)) = patterns[..rest_index]
.iter()
.rev()
.take_while(is_wild)
.enumerate()
.last()
{
span_lint(cx, left_pat.span.until(patterns[rest_index].span), left_index == 0);
}
if let Some((right_index, right_pat)) =
patterns[rest_index + 1..].iter().take_while(is_wild).enumerate().last()
{
span_lint(
cx,
patterns[rest_index].span.shrink_to_hi().to(right_pat.span),
right_index == 0,
);
}
}
}
}

View File

@ -47,7 +47,7 @@ fn has_no_effect(cx: &LateContext<'_, '_>, expr: &Expr) -> bool {
return false; return false;
} }
match expr.node { match expr.node {
ExprKind::Lit(..) | ExprKind::Closure(.., _) => true, ExprKind::Lit(..) | ExprKind::Closure(..) => true,
ExprKind::Path(..) => !has_drop(cx, cx.tables.expr_ty(expr)), ExprKind::Path(..) => !has_drop(cx, cx.tables.expr_ty(expr)),
ExprKind::Index(ref a, ref b) | ExprKind::Binary(_, ref a, ref b) => { ExprKind::Index(ref a, ref b) | ExprKind::Binary(_, ref a, ref b) => {
has_no_effect(cx, a) && has_no_effect(cx, b) has_no_effect(cx, a) && has_no_effect(cx, b)

View File

@ -93,7 +93,7 @@ fn hir_from_snippet(expr: &hir::Expr, snippet: Cow<'a, str>) -> Self {
match expr.node { match expr.node {
hir::ExprKind::AddrOf(..) hir::ExprKind::AddrOf(..)
| hir::ExprKind::Box(..) | hir::ExprKind::Box(..)
| hir::ExprKind::Closure(.., _) | hir::ExprKind::Closure(..)
| hir::ExprKind::Unary(..) | hir::ExprKind::Unary(..)
| hir::ExprKind::Match(..) => Sugg::MaybeParen(snippet), | hir::ExprKind::Match(..) => Sugg::MaybeParen(snippet),
hir::ExprKind::Continue(..) hir::ExprKind::Continue(..)