Skip to content

Commit 12b8c64

Browse files
authored
Merge pull request rust-lang#5982 from flip1995/backport
[beta] Backport: Re-enable len_zero for ranges now that `is_empty` is stable on them
2 parents 09bd400 + c1fc50b commit 12b8c64

File tree

6 files changed

+21
-40
lines changed

6 files changed

+21
-40
lines changed

clippy_lints/src/len_zero.rs

+1-16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::utils::{get_item_name, higher, snippet_with_applicability, span_lint, span_lint_and_sugg, walk_ptrs_ty};
1+
use crate::utils::{get_item_name, snippet_with_applicability, span_lint, span_lint_and_sugg, walk_ptrs_ty};
22
use rustc_ast::ast::LitKind;
33
use rustc_data_structures::fx::FxHashSet;
44
use rustc_errors::Applicability;
@@ -260,17 +260,6 @@ fn check_len(
260260

261261
/// Checks if this type has an `is_empty` method.
262262
fn has_is_empty(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
263-
/// Special case ranges until `range_is_empty` is stabilized. See issue 3807.
264-
fn should_skip_range(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
265-
higher::range(cx, expr).map_or(false, |_| {
266-
!cx.tcx
267-
.features()
268-
.declared_lib_features
269-
.iter()
270-
.any(|(name, _)| name.as_str() == "range_is_empty")
271-
})
272-
}
273-
274263
/// Gets an `AssocItem` and return true if it matches `is_empty(self)`.
275264
fn is_is_empty(cx: &LateContext<'_>, item: &ty::AssocItem) -> bool {
276265
if let ty::AssocKind::Fn = item.kind {
@@ -296,10 +285,6 @@ fn has_is_empty(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
296285
})
297286
}
298287

299-
if should_skip_range(cx, expr) {
300-
return false;
301-
}
302-
303288
let ty = &walk_ptrs_ty(cx.typeck_results().expr_ty(expr));
304289
match ty.kind {
305290
ty::Dynamic(ref tt, ..) => tt.principal().map_or(false, |principal| {

tests/ui/len_zero.fixed

-8
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,3 @@ fn main() {
141141
fn test_slice(b: &[u8]) {
142142
if !b.is_empty() {}
143143
}
144-
145-
mod issue_3807 {
146-
// Avoid suggesting changes to ranges if the user did not enable `range_is_empty`.
147-
// See https://github.com/rust-lang/rust/issues/48111#issuecomment-445132965
148-
fn no_suggestion() {
149-
let _ = (0..42).len() == 0;
150-
}
151-
}

tests/ui/len_zero.rs

-8
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,3 @@ fn main() {
141141
fn test_slice(b: &[u8]) {
142142
if b.len() != 0 {}
143143
}
144-
145-
mod issue_3807 {
146-
// Avoid suggesting changes to ranges if the user did not enable `range_is_empty`.
147-
// See https://github.com/rust-lang/rust/issues/48111#issuecomment-445132965
148-
fn no_suggestion() {
149-
let _ = (0..42).len() == 0;
150-
}
151-
}

tests/ui/len_zero_ranges.fixed

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
// run-rustfix
22

3-
#![feature(range_is_empty)]
43
#![warn(clippy::len_zero)]
54
#![allow(unused)]
65

6+
// Now that `Range(Inclusive)::is_empty` is stable (1.47), we can always suggest this
77
mod issue_3807 {
8-
// With the feature enabled, `is_empty` should be suggested
9-
fn suggestion_is_fine() {
8+
fn suggestion_is_fine_range() {
109
let _ = (0..42).is_empty();
1110
}
11+
12+
fn suggestion_is_fine_range_inclusive() {
13+
let _ = (0_u8..=42).is_empty();
14+
}
1215
}
1316

1417
fn main() {}

tests/ui/len_zero_ranges.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
// run-rustfix
22

3-
#![feature(range_is_empty)]
43
#![warn(clippy::len_zero)]
54
#![allow(unused)]
65

6+
// Now that `Range(Inclusive)::is_empty` is stable (1.47), we can always suggest this
77
mod issue_3807 {
8-
// With the feature enabled, `is_empty` should be suggested
9-
fn suggestion_is_fine() {
8+
fn suggestion_is_fine_range() {
109
let _ = (0..42).len() == 0;
1110
}
11+
12+
fn suggestion_is_fine_range_inclusive() {
13+
let _ = (0_u8..=42).len() == 0;
14+
}
1215
}
1316

1417
fn main() {}

tests/ui/len_zero_ranges.stderr

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
error: length comparison to zero
2-
--> $DIR/len_zero_ranges.rs:10:17
2+
--> $DIR/len_zero_ranges.rs:9:17
33
|
44
LL | let _ = (0..42).len() == 0;
55
| ^^^^^^^^^^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `(0..42).is_empty()`
66
|
77
= note: `-D clippy::len-zero` implied by `-D warnings`
88

9-
error: aborting due to previous error
9+
error: length comparison to zero
10+
--> $DIR/len_zero_ranges.rs:13:17
11+
|
12+
LL | let _ = (0_u8..=42).len() == 0;
13+
| ^^^^^^^^^^^^^^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `(0_u8..=42).is_empty()`
14+
15+
error: aborting due to 2 previous errors
1016

0 commit comments

Comments
 (0)