Skip to content

Commit b79dada

Browse files
committed
pprust: fix parenthesization of exprs
1 parent f83d20e commit b79dada

File tree

4 files changed

+417
-101
lines changed

4 files changed

+417
-101
lines changed

src/librustc_lint/unused.rs

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use syntax::attr;
2222
use syntax::feature_gate::{BUILTIN_ATTRIBUTES, AttributeType};
2323
use syntax::symbol::keywords;
2424
use syntax::ptr::P;
25+
use syntax::util::parser;
2526
use syntax_pos::Span;
2627

2728
use rustc_back::slice;
@@ -313,47 +314,14 @@ impl UnusedParens {
313314
msg: &str,
314315
struct_lit_needs_parens: bool) {
315316
if let ast::ExprKind::Paren(ref inner) = value.node {
316-
let necessary = struct_lit_needs_parens && contains_exterior_struct_lit(&inner);
317+
let necessary = struct_lit_needs_parens &&
318+
parser::contains_exterior_struct_lit(&inner);
317319
if !necessary {
318320
cx.span_lint(UNUSED_PARENS,
319321
value.span,
320322
&format!("unnecessary parentheses around {}", msg))
321323
}
322324
}
323-
324-
/// Expressions that syntactically contain an "exterior" struct
325-
/// literal i.e. not surrounded by any parens or other
326-
/// delimiters, e.g. `X { y: 1 }`, `X { y: 1 }.method()`, `foo
327-
/// == X { y: 1 }` and `X { y: 1 } == foo` all do, but `(X {
328-
/// y: 1 }) == foo` does not.
329-
fn contains_exterior_struct_lit(value: &ast::Expr) -> bool {
330-
match value.node {
331-
ast::ExprKind::Struct(..) => true,
332-
333-
ast::ExprKind::Assign(ref lhs, ref rhs) |
334-
ast::ExprKind::AssignOp(_, ref lhs, ref rhs) |
335-
ast::ExprKind::Binary(_, ref lhs, ref rhs) => {
336-
// X { y: 1 } + X { y: 2 }
337-
contains_exterior_struct_lit(&lhs) || contains_exterior_struct_lit(&rhs)
338-
}
339-
ast::ExprKind::Unary(_, ref x) |
340-
ast::ExprKind::Cast(ref x, _) |
341-
ast::ExprKind::Type(ref x, _) |
342-
ast::ExprKind::Field(ref x, _) |
343-
ast::ExprKind::TupField(ref x, _) |
344-
ast::ExprKind::Index(ref x, _) => {
345-
// &X { y: 1 }, X { y: 1 }.y
346-
contains_exterior_struct_lit(&x)
347-
}
348-
349-
ast::ExprKind::MethodCall(.., ref exprs) => {
350-
// X { y: 1 }.bar(...)
351-
contains_exterior_struct_lit(&exprs[0])
352-
}
353-
354-
_ => false,
355-
}
356-
}
357325
}
358326
}
359327

0 commit comments

Comments
 (0)