Skip to content

[beta] Clippy backport for stabilization of range_is_empty feature #76051

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
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
17 changes: 1 addition & 16 deletions src/tools/clippy/clippy_lints/src/len_zero.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils::{get_item_name, higher, snippet_with_applicability, span_lint, span_lint_and_sugg, walk_ptrs_ty};
use crate::utils::{get_item_name, snippet_with_applicability, span_lint, span_lint_and_sugg, walk_ptrs_ty};
use rustc_ast::ast::LitKind;
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::Applicability;
Expand Down Expand Up @@ -260,17 +260,6 @@ fn check_len(

/// Checks if this type has an `is_empty` method.
fn has_is_empty(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
/// Special case ranges until `range_is_empty` is stabilized. See issue 3807.
fn should_skip_range(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
higher::range(expr).map_or(false, |_| {
!cx.tcx
.features()
.declared_lib_features
.iter()
.any(|(name, _)| name.as_str() == "range_is_empty")
})
}

/// Gets an `AssocItem` and return true if it matches `is_empty(self)`.
fn is_is_empty(cx: &LateContext<'_>, item: &ty::AssocItem) -> bool {
if let ty::AssocKind::Fn = item.kind {
Expand All @@ -296,10 +285,6 @@ fn has_is_empty(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
})
}

if should_skip_range(cx, expr) {
return false;
}

let ty = &walk_ptrs_ty(cx.typeck_results().expr_ty(expr));
match ty.kind {
ty::Dynamic(ref tt, ..) => tt.principal().map_or(false, |principal| {
Expand Down
8 changes: 0 additions & 8 deletions src/tools/clippy/tests/ui/len_zero.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,3 @@ fn main() {
fn test_slice(b: &[u8]) {
if !b.is_empty() {}
}

mod issue_3807 {
// Avoid suggesting changes to ranges if the user did not enable `range_is_empty`.
// See https://github.com/rust-lang/rust/issues/48111#issuecomment-445132965
fn no_suggestion() {
let _ = (0..42).len() == 0;
}
}
8 changes: 0 additions & 8 deletions src/tools/clippy/tests/ui/len_zero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,3 @@ fn main() {
fn test_slice(b: &[u8]) {
if b.len() != 0 {}
}

mod issue_3807 {
// Avoid suggesting changes to ranges if the user did not enable `range_is_empty`.
// See https://github.com/rust-lang/rust/issues/48111#issuecomment-445132965
fn no_suggestion() {
let _ = (0..42).len() == 0;
}
}
10 changes: 6 additions & 4 deletions src/tools/clippy/tests/ui/len_zero_ranges.fixed
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
// run-rustfix

#![feature(range_is_empty)]
#![warn(clippy::len_zero)]
#![allow(unused)]
#![allow(stable_features)] // TODO: https://github.com/rust-lang/rust-clippy/issues/5956

// Now that `Range(Inclusive)::is_empty` is stable (1.47), we can always suggest this
mod issue_3807 {
// With the feature enabled, `is_empty` should be suggested
fn suggestion_is_fine() {
fn suggestion_is_fine_range() {
let _ = (0..42).is_empty();
}

fn suggestion_is_fine_range_inclusive() {
let _ = (0_u8..=42).is_empty();
}
}

fn main() {}
10 changes: 6 additions & 4 deletions src/tools/clippy/tests/ui/len_zero_ranges.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
// run-rustfix

#![feature(range_is_empty)]
#![warn(clippy::len_zero)]
#![allow(unused)]
#![allow(stable_features)] // TODO: https://github.com/rust-lang/rust-clippy/issues/5956

// Now that `Range(Inclusive)::is_empty` is stable (1.47), we can always suggest this
mod issue_3807 {
// With the feature enabled, `is_empty` should be suggested
fn suggestion_is_fine() {
fn suggestion_is_fine_range() {
let _ = (0..42).len() == 0;
}

fn suggestion_is_fine_range_inclusive() {
let _ = (0_u8..=42).len() == 0;
}
}

fn main() {}
10 changes: 8 additions & 2 deletions src/tools/clippy/tests/ui/len_zero_ranges.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
error: length comparison to zero
--> $DIR/len_zero_ranges.rs:11:17
--> $DIR/len_zero_ranges.rs:9:17
|
LL | let _ = (0..42).len() == 0;
| ^^^^^^^^^^^^^^^^^^ help: using `is_empty` is clearer and more explicit: `(0..42).is_empty()`
|
= note: `-D clippy::len-zero` implied by `-D warnings`

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

error: aborting due to 2 previous errors