Skip to content

Commit 64a05f5

Browse files
committed
len_zero: skip ranges if feature range_is_empty is not enabled
1 parent 7ea7cd1 commit 64a05f5

File tree

6 files changed

+70
-1
lines changed

6 files changed

+70
-1
lines changed

clippy_lints/src/len_zero.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::utils::{get_item_name, snippet_with_applicability, span_lint, span_lint_and_sugg, walk_ptrs_ty};
1+
use crate::utils::{get_item_name, higher, 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;
@@ -259,6 +259,17 @@ fn check_len(
259259

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

298+
if should_skip_range(cx, expr) {
299+
return false;
300+
}
301+
287302
let ty = &walk_ptrs_ty(cx.tables.expr_ty(expr));
288303
match ty.kind {
289304
ty::Dynamic(ref tt, ..) => {

tests/ui/len_zero.fixed

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,11 @@ 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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,11 @@ 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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// run-rustfix
2+
3+
#![feature(range_is_empty)]
4+
#![warn(clippy::len_zero)]
5+
#![allow(unused)]
6+
7+
mod issue_3807 {
8+
// With the feature enabled, `is_empty` should be suggested
9+
fn suggestion_is_fine() {
10+
let _ = (0..42).is_empty();
11+
}
12+
}
13+
14+
fn main() {}

tests/ui/len_zero_ranges.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// run-rustfix
2+
3+
#![feature(range_is_empty)]
4+
#![warn(clippy::len_zero)]
5+
#![allow(unused)]
6+
7+
mod issue_3807 {
8+
// With the feature enabled, `is_empty` should be suggested
9+
fn suggestion_is_fine() {
10+
let _ = (0..42).len() == 0;
11+
}
12+
}
13+
14+
fn main() {}

tests/ui/len_zero_ranges.stderr

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: length comparison to zero
2+
--> $DIR/len_zero_ranges.rs:10:17
3+
|
4+
LL | let _ = (0..42).len() == 0;
5+
| ^^^^^^^^^^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `(0..42).is_empty()`
6+
|
7+
= note: `-D clippy::len-zero` implied by `-D warnings`
8+
9+
error: aborting due to previous error
10+

0 commit comments

Comments
 (0)