Skip to content

Commit 0c1ffc1

Browse files
author
Michael Wright
committed
Fix possible_missing_comma false positives
`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.
1 parent a07c271 commit 0c1ffc1

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

clippy_lints/src/formatting.rs

+23-13
Original file line numberDiff line numberDiff line change
@@ -173,24 +173,34 @@ fn check_else_if(cx: &EarlyContext<'_>, expr: &ast::Expr) {
173173
}
174174
}
175175

176+
fn has_unary_equivalent(bin_op: ast::BinOpKind) -> bool {
177+
//+, &, *, -
178+
bin_op == ast::BinOpKind::Add
179+
|| bin_op == ast::BinOpKind::And
180+
|| bin_op == ast::BinOpKind::Mul
181+
|| bin_op == ast::BinOpKind::Sub
182+
}
183+
176184
/// Implementation of the `POSSIBLE_MISSING_COMMA` lint for array
177185
fn check_array(cx: &EarlyContext<'_>, expr: &ast::Expr) {
178186
if let ast::ExprKind::Array(ref array) = expr.node {
179187
for element in array {
180188
if let ast::ExprKind::Binary(ref op, ref lhs, _) = element.node {
181-
if !differing_macro_contexts(lhs.span, op.span) {
182-
let space_span = lhs.span.between(op.span);
183-
if let Some(space_snippet) = snippet_opt(cx, space_span) {
184-
let lint_span = lhs.span.with_lo(lhs.span.hi());
185-
if space_snippet.contains('\n') {
186-
span_note_and_lint(
187-
cx,
188-
POSSIBLE_MISSING_COMMA,
189-
lint_span,
190-
"possibly missing a comma here",
191-
lint_span,
192-
"to remove this lint, add a comma or write the expr in a single line",
193-
);
189+
if has_unary_equivalent(op.node) {
190+
if !differing_macro_contexts(lhs.span, op.span) {
191+
let space_span = lhs.span.between(op.span);
192+
if let Some(space_snippet) = snippet_opt(cx, space_span) {
193+
let lint_span = lhs.span.with_lo(lhs.span.hi());
194+
if space_snippet.contains('\n') {
195+
span_note_and_lint(
196+
cx,
197+
POSSIBLE_MISSING_COMMA,
198+
lint_span,
199+
"possibly missing a comma here",
200+
lint_span,
201+
"to remove this lint, add a comma or write the expr in a single line",
202+
);
203+
}
194204
}
195205
}
196206
}

tests/ui/formatting.rs

+12
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,16 @@ fn main() {
112112
1 + 2, 3 +
113113
4, 5 + 6,
114114
];
115+
116+
// don't lint for bin op without unary equiv
117+
// issue 3244
118+
vec![
119+
1
120+
/ 2,
121+
];
122+
// issue 3396
123+
vec![
124+
true
125+
| false,
126+
];
115127
}

0 commit comments

Comments
 (0)