about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--clippy_lints/src/formatting.rs38
-rw-r--r--tests/ui/formatting.rs11
-rw-r--r--tests/ui/formatting.stderr10
3 files changed, 42 insertions, 17 deletions
diff --git a/clippy_lints/src/formatting.rs b/clippy_lints/src/formatting.rs
index 3768c5924aa..fed87f26be0 100644
--- a/clippy_lints/src/formatting.rs
+++ b/clippy_lints/src/formatting.rs
@@ -3,6 +3,7 @@ use if_chain::if_chain;
 use rustc::lint::in_external_macro;
 use rustc_lint::{EarlyContext, EarlyLintPass};
 use rustc_session::{declare_lint_pass, declare_tool_lint};
+use rustc_span::source_map::Span;
 use syntax::ast::*;
 
 declare_clippy_lint! {
@@ -242,26 +243,31 @@ fn has_unary_equivalent(bin_op: BinOpKind) -> bool {
     bin_op == BinOpKind::And || bin_op == BinOpKind::Mul || bin_op == BinOpKind::Sub
 }
 
+fn indentation(cx: &EarlyContext<'_>, span: Span) -> usize {
+    cx.sess.source_map().lookup_char_pos(span.lo()).col.0
+}
+
 /// Implementation of the `POSSIBLE_MISSING_COMMA` lint for array
 fn check_array(cx: &EarlyContext<'_>, expr: &Expr) {
     if let ExprKind::Array(ref array) = expr.kind {
         for element in array {
-            if let ExprKind::Binary(ref op, ref lhs, _) = element.kind {
-                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());
-                        if space_snippet.contains('\n') {
-                            span_note_and_lint(
-                                cx,
-                                POSSIBLE_MISSING_COMMA,
-                                lint_span,
-                                "possibly missing a comma here",
-                                lint_span,
-                                "to remove this lint, add a comma or write the expr in a single line",
-                            );
-                        }
-                    }
+            if_chain! {
+                if let ExprKind::Binary(ref op, ref lhs, _) = element.kind;
+                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());
+                if space_snippet.contains('\n');
+                if indentation(cx, op.span) <= indentation(cx, lhs.span);
+                then {
+                    span_note_and_lint(
+                        cx,
+                        POSSIBLE_MISSING_COMMA,
+                        lint_span,
+                        "possibly missing a comma here",
+                        lint_span,
+                        "to remove this lint, add a comma or write the expr in a single line",
+                    );
                 }
             }
         }
diff --git a/tests/ui/formatting.rs b/tests/ui/formatting.rs
index 149f1da9396..078811b8d88 100644
--- a/tests/ui/formatting.rs
+++ b/tests/ui/formatting.rs
@@ -143,4 +143,15 @@ fn main() {
         true
         | false,
     ];
+
+    // don't lint if the indentation suggests not to
+    let _ = &[
+        1 + 2, 3 
+                - 4, 5
+    ];
+    // lint if it doesnt
+    let _ = &[
+        -1
+        -4,
+    ];
 }
diff --git a/tests/ui/formatting.stderr b/tests/ui/formatting.stderr
index 29a5c55bc34..e2095cc125b 100644
--- a/tests/ui/formatting.stderr
+++ b/tests/ui/formatting.stderr
@@ -115,5 +115,13 @@ LL |         -1, -2, -3 // <= no comma here
    |
    = note: to remove this lint, add a comma or write the expr in a single line
 
-error: aborting due to 13 previous errors
+error: possibly missing a comma here
+  --> $DIR/formatting.rs:154:11
+   |
+LL |         -1
+   |           ^
+   |
+   = note: to remove this lint, add a comma or write the expr in a single line
+
+error: aborting due to 14 previous errors