Skip to content

Commit 3bb8877

Browse files
bors[bot]Michael Wright
and
Michael Wright
committed
Merge #3407
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]>
2 parents 4c3408c + c20e17f commit 3bb8877

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

clippy_lints/src/formatting.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,19 @@ 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::And
179+
|| bin_op == ast::BinOpKind::Mul
180+
|| bin_op == ast::BinOpKind::Sub
181+
}
182+
176183
/// Implementation of the `POSSIBLE_MISSING_COMMA` lint for array
177184
fn check_array(cx: &EarlyContext<'_>, expr: &ast::Expr) {
178185
if let ast::ExprKind::Array(ref array) = expr.node {
179186
for element in array {
180187
if let ast::ExprKind::Binary(ref op, ref lhs, _) = element.node {
181-
if !differing_macro_contexts(lhs.span, op.span) {
188+
if has_unary_equivalent(op.node) && !differing_macro_contexts(lhs.span, op.span) {
182189
let space_span = lhs.span.between(op.span);
183190
if let Some(space_snippet) = snippet_opt(cx, space_span) {
184191
let lint_span = lhs.span.with_lo(lhs.span.hi());

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)