Skip to content

Commit

Permalink
Add initial impl of check_pat() for UnusedParens
Browse files Browse the repository at this point in the history
This uses a copied version of `check_unused_parens_expr` that is
specific to `ast::Pat`. `check_unused_parens_` could possibly be made
more generic to work with any `ast::*` that has `node` and `span`
fields.

This also only checks for the case of parens around the wildcard
pattern. It covers the case highlighted in the issue, but could check
for a lot more.
  • Loading branch information
kleimkuhler committed Oct 10, 2018
1 parent eae47a4 commit 8552c61
Showing 1 changed file with 68 additions and 4 deletions.
72 changes: 68 additions & 4 deletions src/librustc_lint/unused.rs
Expand Up @@ -264,7 +264,7 @@ declare_lint! {
pub struct UnusedParens;

impl UnusedParens {
fn check_unused_parens_core(&self,
fn check_unused_parens_expr(&self,
cx: &EarlyContext,
value: &ast::Expr,
msg: &str,
Expand Down Expand Up @@ -313,6 +313,56 @@ impl UnusedParens {
}
}
}

fn check_unused_parens_pat(&self,
cx: &EarlyContext,
value: &ast::Pat,
msg: &str,
struct_lit_needs_parens: bool) {
if let ast::PatKind::Paren(_) = value.node {
// Does there need to be a check similar to `parser::contains_exterior_struct_lit`
// here?
if !struct_lit_needs_parens {
let span_msg = format!("unnecessary parentheses around {}", msg);
let mut err = cx.struct_span_lint(UNUSED_PARENS,
value.span,
&span_msg);
// Remove exactly one pair of parentheses (rather than naïvely
// stripping all paren characters)
let mut ate_left_paren = false;
let mut ate_right_paren = false;
let parens_removed = pprust::pat_to_string(value)
.trim_matches(|c| {
match c {
'(' => {
if ate_left_paren {
false
} else {
ate_left_paren = true;
true
}
},
')' => {
if ate_right_paren {
false
} else {
ate_right_paren = true;
true
}
},
_ => false,
}
}).to_owned();
err.span_suggestion_short_with_applicability(
value.span,
"remove these parentheses",
parens_removed,
Applicability::MachineApplicable
);
err.emit();
}
}
}
}

impl LintPass for UnusedParens {
Expand Down Expand Up @@ -354,18 +404,32 @@ impl EarlyLintPass for UnusedParens {
}
let msg = format!("{} argument", call_kind);
for arg in args_to_check {
self.check_unused_parens_core(cx, arg, &msg, false);
self.check_unused_parens_expr(cx, arg, &msg, false);
}
return;
}
};
self.check_unused_parens_core(cx, &value, msg, struct_lit_needs_parens);
self.check_unused_parens_expr(cx, &value, msg, struct_lit_needs_parens);
}

fn check_pat(&mut self, cx: &EarlyContext, p: &ast::Pat) {
use ast::PatKind::*;
let (value, msg) = match p.node {
Paren(ref pat) => {
match pat.node {
Wild => (p, "wildcard pattern"),
_ => return,
}
}
_ => return,
};
self.check_unused_parens_pat(cx, &value, msg, false);
}

fn check_stmt(&mut self, cx: &EarlyContext, s: &ast::Stmt) {
if let ast::StmtKind::Local(ref local) = s.node {
if let Some(ref value) = local.init {
self.check_unused_parens_core(cx, &value, "assigned value", false);
self.check_unused_parens_expr(cx, &value, "assigned value", false);
}
}
}
Expand Down

0 comments on commit 8552c61

Please sign in to comment.