Skip to content

Commit 257f097

Browse files
author
Evan Typanski
committed
Fix issue with mismatched parens in suggestion
1 parent 78f7e37 commit 257f097

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

clippy_lints/src/ranges.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,15 @@ fn check_possible_range_contains(
296296
}
297297

298298
// If the LHS is the same operator, we have to recurse to get the "real" RHS, since they have
299-
// the same operator precedence.
300-
if let ExprKind::Binary(ref lhs_op, _left, new_lhs) = left.kind {
301-
if op == lhs_op.node {
302-
let new_span = Span::new(new_lhs.span.lo(), right.span.hi(), expr.span.ctxt(), expr.span.parent());
299+
// the same operator precedence
300+
if_chain! {
301+
if let ExprKind::Binary(ref lhs_op, _left, new_lhs) = left.kind;
302+
if op == lhs_op.node;
303+
let new_span = Span::new(new_lhs.span.lo(), right.span.hi(), expr.span.ctxt(), expr.span.parent());
304+
if let Some(snip) = &snippet_opt(cx, new_span);
305+
// Do not continue if we have mismatched number of parens, otherwise the suggestion is wrong
306+
if snip.matches('(').count() == snip.matches(')').count();
307+
then {
303308
check_possible_range_contains(cx, op, new_lhs, right, expr, new_span);
304309
}
305310
}

tests/ui/range_contains.fixed

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ fn main() {
5454
let z = 42;
5555
(0..=10).contains(&x) && (0..=10).contains(&z);
5656
!(0..10).contains(&x) || !(0..10).contains(&z);
57+
// Make sure operators in parens don't give a breaking suggestion
58+
((x % 2 == 0) || (x < 0)) || (x >= 10);
5759
}
5860

5961
// Fix #6373

tests/ui/range_contains.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ fn main() {
5454
let z = 42;
5555
(x >= 0) && (x <= 10) && (z >= 0) && (z <= 10);
5656
(x < 0) || (x >= 10) || (z < 0) || (z >= 10);
57+
// Make sure operators in parens don't give a breaking suggestion
58+
((x % 2 == 0) || (x < 0)) || (x >= 10);
5759
}
5860

5961
// Fix #6373

0 commit comments

Comments
 (0)