Skip to content

Commit 24eddc4

Browse files
committed
Share outer paren trimming logic
1 parent e099780 commit 24eddc4

File tree

1 file changed

+46
-84
lines changed

1 file changed

+46
-84
lines changed

src/librustc_lint/unused.rs

+46-84
Original file line numberDiff line numberDiff line change
@@ -273,43 +273,8 @@ impl UnusedParens {
273273
let necessary = struct_lit_needs_parens &&
274274
parser::contains_exterior_struct_lit(&inner);
275275
if !necessary {
276-
let span_msg = format!("unnecessary parentheses around {}", msg);
277-
let mut err = cx.struct_span_lint(UNUSED_PARENS,
278-
value.span,
279-
&span_msg);
280-
// Remove exactly one pair of parentheses (rather than naïvely
281-
// stripping all paren characters)
282-
let mut ate_left_paren = false;
283-
let mut ate_right_paren = false;
284-
let parens_removed = pprust::expr_to_string(value)
285-
.trim_matches(|c| {
286-
match c {
287-
'(' => {
288-
if ate_left_paren {
289-
false
290-
} else {
291-
ate_left_paren = true;
292-
true
293-
}
294-
},
295-
')' => {
296-
if ate_right_paren {
297-
false
298-
} else {
299-
ate_right_paren = true;
300-
true
301-
}
302-
},
303-
_ => false,
304-
}
305-
}).to_owned();
306-
err.span_suggestion_short_with_applicability(
307-
value.span,
308-
"remove these parentheses",
309-
parens_removed,
310-
Applicability::MachineApplicable
311-
);
312-
err.emit();
276+
let pattern = pprust::expr_to_string(value);
277+
Self::remove_outer_parens(cx, value.span, &pattern, msg)
313278
}
314279
}
315280
}
@@ -320,49 +285,48 @@ impl UnusedParens {
320285
msg: &str,
321286
struct_lit_needs_parens: bool) {
322287
if let ast::PatKind::Paren(_) = value.node {
323-
// Does there need to be a check similar to `parser::contains_exterior_struct_lit`
324-
// here?
325288
if !struct_lit_needs_parens {
326-
let span_msg = format!("unnecessary parentheses around {}", msg);
327-
let mut err = cx.struct_span_lint(UNUSED_PARENS,
328-
value.span,
329-
&span_msg);
330-
// Remove exactly one pair of parentheses (rather than naïvely
331-
// stripping all paren characters)
332-
let mut ate_left_paren = false;
333-
let mut ate_right_paren = false;
334-
let parens_removed = pprust::pat_to_string(value)
335-
.trim_matches(|c| {
336-
match c {
337-
'(' => {
338-
if ate_left_paren {
339-
false
340-
} else {
341-
ate_left_paren = true;
342-
true
343-
}
344-
},
345-
')' => {
346-
if ate_right_paren {
347-
false
348-
} else {
349-
ate_right_paren = true;
350-
true
351-
}
352-
},
353-
_ => false,
354-
}
355-
}).to_owned();
356-
err.span_suggestion_short_with_applicability(
357-
value.span,
358-
"remove these parentheses",
359-
parens_removed,
360-
Applicability::MachineApplicable
361-
);
362-
err.emit();
289+
let pattern = pprust::pat_to_string(value);
290+
Self::remove_outer_parens(cx, value.span, &pattern, msg)
363291
}
364292
}
365293
}
294+
295+
fn remove_outer_parens(cx: &EarlyContext, span: Span, pattern: &str, msg: &str) {
296+
let span_msg = format!("unnecessary parentheses around {}", msg);
297+
let mut err = cx.struct_span_lint(UNUSED_PARENS, span, &span_msg);
298+
let mut ate_left_paren = false;
299+
let mut ate_right_paren = false;
300+
let parens_removed = pattern
301+
.trim_matches(|c| {
302+
match c {
303+
'(' => {
304+
if ate_left_paren {
305+
false
306+
} else {
307+
ate_left_paren = true;
308+
true
309+
}
310+
},
311+
')' => {
312+
if ate_right_paren {
313+
false
314+
} else {
315+
ate_right_paren = true;
316+
true
317+
}
318+
},
319+
_ => false,
320+
}
321+
}).to_owned();
322+
err.span_suggestion_short_with_applicability(
323+
span,
324+
"remove these parentheses",
325+
parens_removed,
326+
Applicability::MachineApplicable
327+
);
328+
err.emit();
329+
}
366330
}
367331

368332
impl LintPass for UnusedParens {
@@ -414,16 +378,14 @@ impl EarlyLintPass for UnusedParens {
414378

415379
fn check_pat(&mut self, cx: &EarlyContext, p: &ast::Pat) {
416380
use ast::PatKind::*;
417-
let (value, msg) = match p.node {
418-
Paren(ref pat) => {
419-
match pat.node {
420-
Wild => (p, "wildcard pattern"),
421-
_ => return,
422-
}
423-
}
381+
let (value, msg, struct_lit_needs_parens) = match p.node {
382+
Ident(.., Some(ref pat)) => (pat, "optional subpattern", false),
383+
Ref(ref pat, _) => (pat, "reference pattern", false),
384+
Slice(_, Some(ref pat), _) => (pat, "optional position pattern", false),
385+
Paren(_) => (p, "pattern", false),
424386
_ => return,
425387
};
426-
self.check_unused_parens_pat(cx, &value, msg, false);
388+
self.check_unused_parens_pat(cx, &value, msg, struct_lit_needs_parens);
427389
}
428390

429391
fn check_stmt(&mut self, cx: &EarlyContext, s: &ast::Stmt) {

0 commit comments

Comments
 (0)