Skip to content

Commit bfcae7b

Browse files
committed
fill in chained_binops, and fill in a stopgap version of ident_difference_expr, but then notice that the lint does not seem to ever be run in the tests
1 parent 6b5ae73 commit bfcae7b

File tree

2 files changed

+54
-23
lines changed

2 files changed

+54
-23
lines changed

clippy_lints/src/suspicious_operation_groupings.rs

Lines changed: 53 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use crate::utils::{snippet_with_applicability, span_lint_and_sugg};
21
use crate::utils::ast_utils::{eq_id, IdentIter};
2+
use crate::utils::{snippet_with_applicability, span_lint_and_sugg};
33
use core::ops::{Add, AddAssign};
44
use if_chain::if_chain;
55
use rustc_ast::ast::*;
@@ -8,6 +8,7 @@ use rustc_data_structures::fx::FxHashSet;
88
use rustc_errors::Applicability;
99
use rustc_lint::{EarlyContext, EarlyLintPass};
1010
use rustc_session::{declare_lint_pass, declare_tool_lint};
11+
use rustc_span::source_map::Spanned;
1112
use rustc_span::symbol::Ident;
1213
use rustc_span::Span;
1314

@@ -196,17 +197,39 @@ fn ident_swap_sugg(
196197
Some(sugg)
197198
}
198199

199-
struct BinaryOp {
200+
struct BinaryOp<'exprs> {
200201
op: BinOpKind,
201202
span: Span,
202-
left: P<Expr>,
203-
right: P<Expr>,
203+
left: &'exprs Expr,
204+
right: &'exprs Expr,
204205
}
205206

206207
// TODO make this collect expr pairs in (for example) if expressions, and rename it.
207208
// Also, include enough info to make a coherent suggestion in those cases.
208-
fn chained_binops(kind: &ExprKind) -> Option<Vec<BinaryOp>> {
209-
todo!()
209+
fn chained_binops(kind: &'expr ExprKind) -> Option<Vec<BinaryOp<'expr>>> {
210+
match kind {
211+
ExprKind::Binary(_, left_outer, right_outer) => match (&left_outer.kind, &right_outer.kind) {
212+
(
213+
ExprKind::Binary(Spanned { node: left_op, .. }, left_left, left_right),
214+
ExprKind::Binary(Spanned { node: right_op, .. }, right_left, right_right),
215+
) if left_op == right_op => Some(vec![
216+
BinaryOp {
217+
op: *left_op,
218+
left: left_left,
219+
right: left_right,
220+
span: left_outer.span,
221+
},
222+
BinaryOp {
223+
op: *right_op,
224+
left: right_left,
225+
right: right_right,
226+
span: right_outer.span,
227+
},
228+
]),
229+
_ => None,
230+
},
231+
_ => None,
232+
}
210233
}
211234

212235
#[derive(Clone, Copy, PartialEq, Eq, Default, Debug)]
@@ -219,7 +242,7 @@ impl Add for IdentLocation {
219242

220243
fn add(self, other: Self) -> Self::Output {
221244
Self {
222-
index: self.index + other.index
245+
index: self.index + other.index,
223246
}
224247
}
225248
}
@@ -244,15 +267,13 @@ impl Add for IdentDifference {
244267

245268
fn add(self, other: Self) -> Self::Output {
246269
match (self, other) {
247-
(Self::NoDifference, output)|(output, Self::NoDifference) => output,
270+
(Self::NoDifference, output) | (output, Self::NoDifference) => output,
248271
(Self::Multiple, _)
249272
| (_, Self::Multiple)
250273
| (Self::Single(_, _), Self::Double(_, _))
251274
| (Self::Double(_, _), Self::Single(_, _))
252-
| (Self::Double(_, _), Self::Double(_, _)) =>
253-
Self::Multiple,
254-
(Self::NonIdentDifference, _)|(_, Self::NonIdentDifference) =>
255-
Self::NonIdentDifference,
275+
| (Self::Double(_, _), Self::Double(_, _)) => Self::Multiple,
276+
(Self::NonIdentDifference, _) | (_, Self::NonIdentDifference) => Self::NonIdentDifference,
256277
(Self::Single(il1, _), Self::Single(il2, _)) => Self::Double(il1, il2),
257278
}
258279
}
@@ -305,19 +326,29 @@ fn ident_difference_expr_with_base_location(
305326
// without needing to change the rest of the code.
306327
let mut difference = IdentDifference::NoDifference;
307328

308-
for (left_attr, right_attr) in left.attrs.iter().zip(right.attrs.iter()) {
309-
let (new_difference, new_base) =
310-
ident_difference_via_ident_iter_with_base_location(left_attr, right_attr, base);
329+
if false
330+
/* fill out and use this branch later */
331+
{
332+
for (left_attr, right_attr) in left.attrs.iter().zip(right.attrs.iter()) {
333+
let (new_difference, new_base) =
334+
ident_difference_via_ident_iter_with_base_location(left_attr, right_attr, base);
335+
base = new_base;
336+
difference += new_difference;
337+
if difference.is_complete() {
338+
return (difference, base);
339+
}
340+
}
341+
342+
match (&left.kind, &right.kind) {
343+
_ => todo!(),
344+
}
345+
} else {
346+
let (new_difference, new_base) = ident_difference_via_ident_iter_with_base_location(left, right, base);
311347
base = new_base;
312348
difference += new_difference;
313-
if difference.is_complete() {
314-
return (difference, base);
315-
}
316349
}
317350

318-
match (&left.kind, &right.kind) {
319-
_ => todo!(),
320-
}
351+
(difference, base)
321352
}
322353

323354
fn ident_difference_via_ident_iter_with_base_location<Iterable: Into<IdentIter>>(
@@ -365,7 +396,7 @@ fn suggestion_with_swapped_ident(
365396
new_ident: Ident,
366397
applicability: &mut Applicability,
367398
) -> Option<String> {
368-
get_ident(expr, location).and_then(|current_ident| {
399+
get_ident(expr, location).map(|current_ident| {
369400
format!(
370401
"{}{}{}",
371402
snippet_with_applicability(cx, expr.span.with_hi(current_ident.span.lo()), "..", applicability),
@@ -374,4 +405,3 @@ fn suggestion_with_swapped_ident(
374405
)
375406
})
376407
}
377-

tests/ui/suspicious_operation_groupings.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![warn(clippy::suspicious_operation_groupings)]
2+
#![allow(clippy::eq_op)]
23

34
struct Vec3 { x: f64, y: f64, z: f64 }
45

0 commit comments

Comments
 (0)