Auto merge of #5083 - Areredify:issue-4399, r=flip1995

dont fire `possible_missing_comma` if intendation is present

Closes #4399
changelog: dont fire `possible_missing_comma` if intendation is present

I suspect there is already a utils function for indentation (but searching indent didn't yield a function for that), and my solution is certainly not universal, but it's probably the best we can do.
This commit is contained in:
bors 2020-01-25 17:47:54 +00:00
commit fd6f609f64
3 changed files with 42 additions and 17 deletions

View file

@ -3,6 +3,7 @@ use if_chain::if_chain;
use rustc::lint::in_external_macro; use rustc::lint::in_external_macro;
use rustc_lint::{EarlyContext, EarlyLintPass}; use rustc_lint::{EarlyContext, EarlyLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::source_map::Span;
use syntax::ast::*; use syntax::ast::*;
declare_clippy_lint! { 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 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 /// Implementation of the `POSSIBLE_MISSING_COMMA` lint for array
fn check_array(cx: &EarlyContext<'_>, expr: &Expr) { fn check_array(cx: &EarlyContext<'_>, expr: &Expr) {
if let ExprKind::Array(ref array) = expr.kind { if let ExprKind::Array(ref array) = expr.kind {
for element in array { for element in array {
if let ExprKind::Binary(ref op, ref lhs, _) = element.kind { if_chain! {
if has_unary_equivalent(op.node) && !differing_macro_contexts(lhs.span, op.span) { if let ExprKind::Binary(ref op, ref lhs, _) = element.kind;
let space_span = lhs.span.between(op.span); if has_unary_equivalent(op.node) && !differing_macro_contexts(lhs.span, op.span);
if let Some(space_snippet) = snippet_opt(cx, space_span) { let space_span = lhs.span.between(op.span);
let lint_span = lhs.span.with_lo(lhs.span.hi()); if let Some(space_snippet) = snippet_opt(cx, space_span);
if space_snippet.contains('\n') { let lint_span = lhs.span.with_lo(lhs.span.hi());
span_note_and_lint( if space_snippet.contains('\n');
cx, if indentation(cx, op.span) <= indentation(cx, lhs.span);
POSSIBLE_MISSING_COMMA, then {
lint_span, span_note_and_lint(
"possibly missing a comma here", cx,
lint_span, POSSIBLE_MISSING_COMMA,
"to remove this lint, add a comma or write the expr in a single line", lint_span,
); "possibly missing a comma here",
} lint_span,
} "to remove this lint, add a comma or write the expr in a single line",
);
} }
} }
} }

View file

@ -143,4 +143,15 @@ fn main() {
true true
| false, | false,
]; ];
// don't lint if the indentation suggests not to
let _ = &[
1 + 2, 3
- 4, 5
];
// lint if it doesnt
let _ = &[
-1
-4,
];
} }

View file

@ -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 = 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