Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion clippy_lints/src/suspicious_operation_groupings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,12 +580,18 @@ fn ident_difference_expr_with_base_location(
| (Unary(_, _), Unary(_, _))
| (Binary(_, _, _), Binary(_, _, _))
| (Tup(_), Tup(_))
| (MethodCall(_), MethodCall(_))
| (Call(_, _), Call(_, _))
| (ConstBlock(_), ConstBlock(_))
| (Array(_), Array(_)) => {
// keep going
},
(MethodCall(left, ..), MethodCall(right, ..)) => {
// A method name is part of the expression's structure, not a
// candidate for the identifier swap this lint is looking for.
if !eq_id(left.seg.ident, right.seg.ident) {
return (IdentDifference::NonIdent, base);
}
},
Comment on lines +588 to +594

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets simplify..

Also I don't think the comment is very insightfull and mostly just stating the obvious...

Suggested change
(MethodCall(left, ..), MethodCall(right, ..)) => {
// A method name is part of the expression's structure, not a
// candidate for the identifier swap this lint is looking for.
if !eq_id(left.seg.ident, right.seg.ident) {
return (IdentDifference::NonIdent, base);
}
},
(MethodCall(left, ..), MethodCall(right, ..)) if eq_id(left.seg.ident, right.seg.ident) => {
// keep going
},

_ => {
return (IdentDifference::NonIdent, base);
},
Expand Down
26 changes: 26 additions & 0 deletions tests/ui/suspicious_operation_groupings.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ impl S {
fn a(&self) -> i32 {
0
}

fn is_special(&self) -> bool {
true
}

fn is_file(&self) -> bool {
true
}

fn has_authority(&self) -> bool {
true
}
}

fn do_not_give_bad_suggestions_for_this_unusual_expr(s1: &S, s2: &SaOnly) -> bool {
Expand All @@ -46,6 +58,20 @@ fn do_not_give_bad_suggestions_for_this_unusual_expr(s1: &S, s2: &SaOnly) -> boo
s1.a < s2.a && s1.a() < s1.b
}

fn do_not_suggest_a_different_method(s1: &S, s2: &S) -> bool {
// The different method names are intentional and must not be treated as a
// typo in the receiver.
(s1.is_special() && !s2.is_special())
|| (!s1.is_special() && s2.is_special())
|| (s1.is_file() && s2.has_authority())
}

#[expect(clippy::suspicious_operation_groupings)]
fn still_detects_a_receiver_typo_with_the_same_method(s1: &S, s2: &S) -> i32 {
// The method names match, so a receiver mismatch remains suspicious.
s1.a() * s2.a() + s1.a() * s1.a()
}

fn do_not_give_bad_suggestions_for_this_macro_expr(s1: &S, s2: &SaOnly) -> bool {
macro_rules! s1 {
() => {
Expand Down
26 changes: 26 additions & 0 deletions tests/ui/suspicious_operation_groupings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ impl S {
fn a(&self) -> i32 {
0
}

fn is_special(&self) -> bool {
true
}

fn is_file(&self) -> bool {
true
}

fn has_authority(&self) -> bool {
true
}
}

fn do_not_give_bad_suggestions_for_this_unusual_expr(s1: &S, s2: &SaOnly) -> bool {
Expand All @@ -46,6 +58,20 @@ fn do_not_give_bad_suggestions_for_this_unusual_expr(s1: &S, s2: &SaOnly) -> boo
s1.a < s2.a && s1.a() < s1.b
}

fn do_not_suggest_a_different_method(s1: &S, s2: &S) -> bool {
// The different method names are intentional and must not be treated as a
// typo in the receiver.
(s1.is_special() && !s2.is_special())
|| (!s1.is_special() && s2.is_special())
|| (s1.is_file() && s2.has_authority())
}

#[expect(clippy::suspicious_operation_groupings)]
fn still_detects_a_receiver_typo_with_the_same_method(s1: &S, s2: &S) -> i32 {
// The method names match, so a receiver mismatch remains suspicious.
s1.a() * s2.a() + s1.a() * s1.a()
}

fn do_not_give_bad_suggestions_for_this_macro_expr(s1: &S, s2: &SaOnly) -> bool {
macro_rules! s1 {
() => {
Expand Down
44 changes: 22 additions & 22 deletions tests/ui/suspicious_operation_groupings.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,133 +14,133 @@ LL | s1.a < s2.a && s1.a < s2.b
| ^^^^^^^^^^^ help: did you mean: `s1.b < s2.b`

error: this sequence of operators looks suspiciously like a bug
--> tests/ui/suspicious_operation_groupings.rs:78:33
--> tests/ui/suspicious_operation_groupings.rs:104:33
|
LL | s1.a * s2.a + s1.b * s2.b + s1.c * s2.b + s1.d * s2.d
| ^^^^^^^^^^^ help: did you mean: `s1.c * s2.c`

error: this sequence of operators looks suspiciously like a bug
--> tests/ui/suspicious_operation_groupings.rs:84:19
--> tests/ui/suspicious_operation_groupings.rs:110:19
|
LL | s1.a * s2.a + s1.b * s2.c + s1.c * s2.c
| ^^^^^^^^^^^ help: did you mean: `s1.b * s2.b`

error: this sequence of operators looks suspiciously like a bug
--> tests/ui/suspicious_operation_groupings.rs:90:19
--> tests/ui/suspicious_operation_groupings.rs:116:19
|
LL | s1.a * s2.a + s2.b * s2.b + s1.c * s2.c
| ^^^^^^^^^^^ help: did you mean: `s1.b * s2.b`

error: this sequence of operators looks suspiciously like a bug
--> tests/ui/suspicious_operation_groupings.rs:96:19
--> tests/ui/suspicious_operation_groupings.rs:122:19
|
LL | s1.a * s2.a + s1.b * s1.b + s1.c * s2.c
| ^^^^^^^^^^^ help: did you mean: `s1.b * s2.b`

error: this sequence of operators looks suspiciously like a bug
--> tests/ui/suspicious_operation_groupings.rs:102:5
--> tests/ui/suspicious_operation_groupings.rs:128:5
|
LL | s1.a * s1.a + s1.b * s2.b + s1.c * s2.c
| ^^^^^^^^^^^ help: did you mean: `s1.a * s2.a`

error: this sequence of operators looks suspiciously like a bug
--> tests/ui/suspicious_operation_groupings.rs:108:33
--> tests/ui/suspicious_operation_groupings.rs:134:33
|
LL | s1.a * s2.a + s1.b * s2.b + s1.c * s1.c
| ^^^^^^^^^^^ help: did you mean: `s1.c * s2.c`

error: this sequence of operators looks suspiciously like a bug
--> tests/ui/suspicious_operation_groupings.rs:122:20
--> tests/ui/suspicious_operation_groupings.rs:148:20
|
LL | (s1.a * s2.a + s1.b * s1.b)
| ^^^^^^^^^^^ help: did you mean: `s1.b * s2.b`

error: this sequence of operators looks suspiciously like a bug
--> tests/ui/suspicious_operation_groupings.rs:128:34
--> tests/ui/suspicious_operation_groupings.rs:154:34
|
LL | (s1.a * s2.a + s1.b * s2.b + s1.c * s2.b + s1.d * s2.d)
| ^^^^^^^^^^^ help: did you mean: `s1.c * s2.c`

error: this sequence of operators looks suspiciously like a bug
--> tests/ui/suspicious_operation_groupings.rs:134:38
--> tests/ui/suspicious_operation_groupings.rs:160:38
|
LL | (s1.a * s2.a) + (s1.b * s2.b) + (s1.c * s2.b) + (s1.d * s2.d)
| ^^^^^^^^^^^ help: did you mean: `s1.c * s2.c`

error: this sequence of operators looks suspiciously like a bug
--> tests/ui/suspicious_operation_groupings.rs:140:39
--> tests/ui/suspicious_operation_groupings.rs:166:39
|
LL | ((s1.a * s2.a) + (s1.b * s2.b) + (s1.c * s2.b) + (s1.d * s2.d))
| ^^^^^^^^^^^ help: did you mean: `s1.c * s2.c`

error: this sequence of operators looks suspiciously like a bug
--> tests/ui/suspicious_operation_groupings.rs:146:42
--> tests/ui/suspicious_operation_groupings.rs:172:42
|
LL | (((s1.a * s2.a) + (s1.b * s2.b)) + ((s1.c * s2.b) + (s1.d * s2.d)))
| ^^^^^^^^^^^ help: did you mean: `s1.c * s2.c`

error: this sequence of operators looks suspiciously like a bug
--> tests/ui/suspicious_operation_groupings.rs:152:40
--> tests/ui/suspicious_operation_groupings.rs:178:40
|
LL | (((s1.a * s2.a) + (s1.b * s2.b) + (s1.c * s2.b)) + (s1.d * s2.d))
| ^^^^^^^^^^^ help: did you mean: `s1.c * s2.c`

error: this sequence of operators looks suspiciously like a bug
--> tests/ui/suspicious_operation_groupings.rs:158:40
--> tests/ui/suspicious_operation_groupings.rs:184:40
|
LL | ((s1.a * s2.a) + ((s1.b * s2.b) + (s1.c * s2.b) + (s1.d * s2.d)))
| ^^^^^^^^^^^ help: did you mean: `s1.c * s2.c`

error: this sequence of operators looks suspiciously like a bug
--> tests/ui/suspicious_operation_groupings.rs:164:20
--> tests/ui/suspicious_operation_groupings.rs:190:20
|
LL | (s1.a * s2.a + s2.b * s2.b) / 2
| ^^^^^^^^^^^ help: did you mean: `s1.b * s2.b`

error: this sequence of operators looks suspiciously like a bug
--> tests/ui/suspicious_operation_groupings.rs:170:35
--> tests/ui/suspicious_operation_groupings.rs:196:35
|
LL | i32::swap_bytes(s1.a * s2.a + s2.b * s2.b)
| ^^^^^^^^^^^ help: did you mean: `s1.b * s2.b`

error: this sequence of operators looks suspiciously like a bug
--> tests/ui/suspicious_operation_groupings.rs:176:29
--> tests/ui/suspicious_operation_groupings.rs:202:29
|
LL | s1.a > 0 && s1.b > 0 && s1.d == s2.c && s1.d == s2.d
| ^^^^^^^^^^^^ help: did you mean: `s1.c == s2.c`

error: this sequence of operators looks suspiciously like a bug
--> tests/ui/suspicious_operation_groupings.rs:182:17
--> tests/ui/suspicious_operation_groupings.rs:208:17
|
LL | s1.a > 0 && s1.d == s2.c && s1.b > 0 && s1.d == s2.d
| ^^^^^^^^^^^^ help: did you mean: `s1.c == s2.c`

error: this sequence of operators looks suspiciously like a bug
--> tests/ui/suspicious_operation_groupings.rs:192:77
--> tests/ui/suspicious_operation_groupings.rs:218:77
|
LL | (n1.inner.0).0 == (n2.inner.0).0 && (n1.inner.1).0 == (n2.inner.1).0 && (n1.inner.2).0 == (n2.inner.1).0
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: did you mean: `(n1.inner.2).0 == (n2.inner.2).0`

error: this sequence of operators looks suspiciously like a bug
--> tests/ui/suspicious_operation_groupings.rs:207:25
--> tests/ui/suspicious_operation_groupings.rs:233:25
|
LL | s1.a <= s2.a && s1.a <= s2.b
| ^^^^^^^^^^^^ help: did you mean: `s1.b <= s2.b`

error: this sequence of operators looks suspiciously like a bug
--> tests/ui/suspicious_operation_groupings.rs:214:23
--> tests/ui/suspicious_operation_groupings.rs:240:23
|
LL | if s1.a < s2.a && s1.a < s2.b {
| ^^^^^^^^^^^ help: did you mean: `s1.b < s2.b`

error: this sequence of operators looks suspiciously like a bug
--> tests/ui/suspicious_operation_groupings.rs:222:48
--> tests/ui/suspicious_operation_groupings.rs:248:48
|
LL | -(-(-s1.a * -s2.a) + (-(-s1.b * -s2.b) + -(-s1.c * -s2.b) + -(-s1.d * -s2.d)))
| ^^^^^^^^^^^^^ help: did you mean: `-s1.c * -s2.c`

error: this sequence of operators looks suspiciously like a bug
--> tests/ui/suspicious_operation_groupings.rs:228:27
--> tests/ui/suspicious_operation_groupings.rs:254:27
|
LL | -(if -s1.a < -s2.a && -s1.a < -s2.b { s1.c } else { s2.a })
| ^^^^^^^^^^^^^ help: did you mean: `-s1.b < -s2.b`
Expand Down