Skip to content

Commit

Permalink
Merge #3407
Browse files Browse the repository at this point in the history
3407: Fix `possible_missing_comma` false positives r=oli-obk a=mikerite

`possible_missing_comma` should only trigger when the binary operator has
unary equivalent. Otherwise, it's not possible to insert a comma without
breaking compilation. The operators identified were `+`, `&`, `*` and `-`.

This fixes the specific examples given in issues #3244 and #3396
but doesn't address the conflict this lint has with the style of starting
a line with a binary operator.

Co-authored-by: Michael Wright <[email protected]>
  • Loading branch information
bors[bot] and Michael Wright committed Nov 7, 2018
2 parents 4c3408c + c20e17f commit 3bb8877
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
9 changes: 8 additions & 1 deletion clippy_lints/src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,19 @@ fn check_else_if(cx: &EarlyContext<'_>, expr: &ast::Expr) {
}
}

fn has_unary_equivalent(bin_op: ast::BinOpKind) -> bool {
// &, *, -
bin_op == ast::BinOpKind::And
|| bin_op == ast::BinOpKind::Mul
|| bin_op == ast::BinOpKind::Sub
}

/// Implementation of the `POSSIBLE_MISSING_COMMA` lint for array
fn check_array(cx: &EarlyContext<'_>, expr: &ast::Expr) {
if let ast::ExprKind::Array(ref array) = expr.node {
for element in array {
if let ast::ExprKind::Binary(ref op, ref lhs, _) = element.node {
if !differing_macro_contexts(lhs.span, op.span) {
if has_unary_equivalent(op.node) && !differing_macro_contexts(lhs.span, op.span) {
let space_span = lhs.span.between(op.span);
if let Some(space_snippet) = snippet_opt(cx, space_span) {
let lint_span = lhs.span.with_lo(lhs.span.hi());
Expand Down
12 changes: 12 additions & 0 deletions tests/ui/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,16 @@ fn main() {
1 + 2, 3 +
4, 5 + 6,
];

// don't lint for bin op without unary equiv
// issue 3244
vec![
1
/ 2,
];
// issue 3396
vec![
true
| false,
];
}

0 comments on commit 3bb8877

Please sign in to comment.