Skip to content

Commit 6328371

Browse files
committed
Auto merge of rust-lang#10681 - J-ZhengLi:issue10529, r=flip1995
make [`len_zero`] lint not spanning over parenthesis sorry it should be a quick fix but I was caught up by other stuffs last couple weeks 🤦‍♂️ --- fixes: rust-lang#10529 changelog: make [`len_zero`] lint not spanning over parenthesis
2 parents 9824234 + b8d6964 commit 6328371

File tree

4 files changed

+34
-12
lines changed

4 files changed

+34
-12
lines changed

clippy_lints/src/len_zero.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -168,25 +168,27 @@ impl<'tcx> LateLintPass<'tcx> for LenZero {
168168
}
169169

170170
if let ExprKind::Binary(Spanned { node: cmp, .. }, left, right) = expr.kind {
171+
// expr.span might contains parenthesis, see issue #10529
172+
let actual_span = left.span.with_hi(right.span.hi());
171173
match cmp {
172174
BinOpKind::Eq => {
173-
check_cmp(cx, expr.span, left, right, "", 0); // len == 0
174-
check_cmp(cx, expr.span, right, left, "", 0); // 0 == len
175+
check_cmp(cx, actual_span, left, right, "", 0); // len == 0
176+
check_cmp(cx, actual_span, right, left, "", 0); // 0 == len
175177
},
176178
BinOpKind::Ne => {
177-
check_cmp(cx, expr.span, left, right, "!", 0); // len != 0
178-
check_cmp(cx, expr.span, right, left, "!", 0); // 0 != len
179+
check_cmp(cx, actual_span, left, right, "!", 0); // len != 0
180+
check_cmp(cx, actual_span, right, left, "!", 0); // 0 != len
179181
},
180182
BinOpKind::Gt => {
181-
check_cmp(cx, expr.span, left, right, "!", 0); // len > 0
182-
check_cmp(cx, expr.span, right, left, "", 1); // 1 > len
183+
check_cmp(cx, actual_span, left, right, "!", 0); // len > 0
184+
check_cmp(cx, actual_span, right, left, "", 1); // 1 > len
183185
},
184186
BinOpKind::Lt => {
185-
check_cmp(cx, expr.span, left, right, "", 1); // len < 1
186-
check_cmp(cx, expr.span, right, left, "!", 0); // 0 < len
187+
check_cmp(cx, actual_span, left, right, "", 1); // len < 1
188+
check_cmp(cx, actual_span, right, left, "!", 0); // 0 < len
187189
},
188-
BinOpKind::Ge => check_cmp(cx, expr.span, left, right, "!", 1), // len >= 1
189-
BinOpKind::Le => check_cmp(cx, expr.span, right, left, "!", 1), // 1 <= len
190+
BinOpKind::Ge => check_cmp(cx, actual_span, left, right, "!", 1), // len >= 1
191+
BinOpKind::Le => check_cmp(cx, actual_span, right, left, "!", 1), // 1 <= len
190192
_ => (),
191193
}
192194
}

tests/ui/len_zero.fixed

+4
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ fn main() {
176176
// No error; `HasWrongIsEmpty` does not have `.is_empty()`.
177177
println!("Or this!");
178178
}
179+
180+
// issue #10529
181+
(!has_is_empty.is_empty()).then(|| println!("This can happen."));
182+
(has_is_empty.is_empty()).then(|| println!("Or this!"));
179183
}
180184

181185
fn test_slice(b: &[u8]) {

tests/ui/len_zero.rs

+4
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ fn main() {
176176
// No error; `HasWrongIsEmpty` does not have `.is_empty()`.
177177
println!("Or this!");
178178
}
179+
180+
// issue #10529
181+
(has_is_empty.len() > 0).then(|| println!("This can happen."));
182+
(has_is_empty.len() == 0).then(|| println!("Or this!"));
179183
}
180184

181185
fn test_slice(b: &[u8]) {

tests/ui/len_zero.stderr

+14-2
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,22 @@ LL | if with_is_empty.len() == 0 {
123123
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `with_is_empty.is_empty()`
124124

125125
error: length comparison to zero
126-
--> $DIR/len_zero.rs:182:8
126+
--> $DIR/len_zero.rs:181:6
127+
|
128+
LL | (has_is_empty.len() > 0).then(|| println!("This can happen."));
129+
| ^^^^^^^^^^^^^^^^^^^^^^ help: using `!is_empty` is clearer and more explicit: `!has_is_empty.is_empty()`
130+
131+
error: length comparison to zero
132+
--> $DIR/len_zero.rs:182:6
133+
|
134+
LL | (has_is_empty.len() == 0).then(|| println!("Or this!"));
135+
| ^^^^^^^^^^^^^^^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `has_is_empty.is_empty()`
136+
137+
error: length comparison to zero
138+
--> $DIR/len_zero.rs:186:8
127139
|
128140
LL | if b.len() != 0 {}
129141
| ^^^^^^^^^^^^ help: using `!is_empty` is clearer and more explicit: `!b.is_empty()`
130142

131-
error: aborting due to 21 previous errors
143+
error: aborting due to 23 previous errors
132144

0 commit comments

Comments
 (0)