-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #13421 - lukaslueg:issue11212, r=Manishearth
Initial impl of `unnecessary_first_then_check` Fixes #11212 Checks for `{slice/vec/Box<[]>}.first().is_some()` and suggests replacing the unnecessary `Option`-construct with a direct `{slice/...}.is_empty()`. Other lints guide constructs like `if let Some(_) = v.get(0)` into this, which end up as `!v.is_empty()`. changelog: [`unnecessary_first_then_check`]: Initial implementation
- Loading branch information
Showing
7 changed files
with
182 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::source::SpanRangeExt; | ||
|
||
use rustc_errors::Applicability; | ||
use rustc_hir::{Expr, ExprKind}; | ||
use rustc_lint::LateContext; | ||
use rustc_span::Span; | ||
|
||
use super::UNNECESSARY_FIRST_THEN_CHECK; | ||
|
||
pub(super) fn check( | ||
cx: &LateContext<'_>, | ||
call_span: Span, | ||
first_call: &Expr<'_>, | ||
first_caller: &Expr<'_>, | ||
is_some: bool, | ||
) { | ||
if !cx | ||
.typeck_results() | ||
.expr_ty_adjusted(first_caller) | ||
.peel_refs() | ||
.is_slice() | ||
{ | ||
return; | ||
} | ||
|
||
let ExprKind::MethodCall(_, _, _, first_call_span) = first_call.kind else { | ||
return; | ||
}; | ||
|
||
let both_calls_span = first_call_span.with_hi(call_span.hi()); | ||
if let Some(both_calls_snippet) = both_calls_span.get_source_text(cx) | ||
&& let Some(first_caller_snippet) = first_caller.span.get_source_text(cx) | ||
{ | ||
let (sugg_span, suggestion) = if is_some { | ||
( | ||
first_caller.span.with_hi(call_span.hi()), | ||
format!("!{first_caller_snippet}.is_empty()"), | ||
) | ||
} else { | ||
(both_calls_span, "is_empty()".to_owned()) | ||
}; | ||
span_lint_and_sugg( | ||
cx, | ||
UNNECESSARY_FIRST_THEN_CHECK, | ||
sugg_span, | ||
format!( | ||
"unnecessary use of `{both_calls_snippet}` to check if slice {}", | ||
if is_some { "is not empty" } else { "is empty" } | ||
), | ||
"replace this with", | ||
suggestion, | ||
Applicability::MaybeIncorrect, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#![warn(clippy::unnecessary_first_then_check)] | ||
#![allow(clippy::useless_vec, clippy::const_is_empty)] | ||
|
||
fn main() { | ||
let s = [1, 2, 3]; | ||
let _: bool = !s.is_empty(); | ||
let _: bool = s.is_empty(); | ||
|
||
let v = vec![1, 2, 3]; | ||
let _: bool = !v.is_empty(); | ||
|
||
let n = [[1, 2, 3], [4, 5, 6]]; | ||
let _: bool = !n[0].is_empty(); | ||
let _: bool = n[0].is_empty(); | ||
|
||
struct Foo { | ||
bar: &'static [i32], | ||
} | ||
let f = [Foo { bar: &[] }]; | ||
let _: bool = !f[0].bar.is_empty(); | ||
let _: bool = f[0].bar.is_empty(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#![warn(clippy::unnecessary_first_then_check)] | ||
#![allow(clippy::useless_vec, clippy::const_is_empty)] | ||
|
||
fn main() { | ||
let s = [1, 2, 3]; | ||
let _: bool = s.first().is_some(); | ||
let _: bool = s.first().is_none(); | ||
|
||
let v = vec![1, 2, 3]; | ||
let _: bool = v.first().is_some(); | ||
|
||
let n = [[1, 2, 3], [4, 5, 6]]; | ||
let _: bool = n[0].first().is_some(); | ||
let _: bool = n[0].first().is_none(); | ||
|
||
struct Foo { | ||
bar: &'static [i32], | ||
} | ||
let f = [Foo { bar: &[] }]; | ||
let _: bool = f[0].bar.first().is_some(); | ||
let _: bool = f[0].bar.first().is_none(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
error: unnecessary use of `first().is_some()` to check if slice is not empty | ||
--> tests/ui/unnecessary_first_then_check.rs:6:19 | ||
| | ||
LL | let _: bool = s.first().is_some(); | ||
| ^^^^^^^^^^^^^^^^^^^ help: replace this with: `!s.is_empty()` | ||
| | ||
= note: `-D clippy::unnecessary-first-then-check` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::unnecessary_first_then_check)]` | ||
|
||
error: unnecessary use of `first().is_none()` to check if slice is empty | ||
--> tests/ui/unnecessary_first_then_check.rs:7:21 | ||
| | ||
LL | let _: bool = s.first().is_none(); | ||
| ^^^^^^^^^^^^^^^^^ help: replace this with: `is_empty()` | ||
|
||
error: unnecessary use of `first().is_some()` to check if slice is not empty | ||
--> tests/ui/unnecessary_first_then_check.rs:10:19 | ||
| | ||
LL | let _: bool = v.first().is_some(); | ||
| ^^^^^^^^^^^^^^^^^^^ help: replace this with: `!v.is_empty()` | ||
|
||
error: unnecessary use of `first().is_some()` to check if slice is not empty | ||
--> tests/ui/unnecessary_first_then_check.rs:13:19 | ||
| | ||
LL | let _: bool = n[0].first().is_some(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^ help: replace this with: `!n[0].is_empty()` | ||
|
||
error: unnecessary use of `first().is_none()` to check if slice is empty | ||
--> tests/ui/unnecessary_first_then_check.rs:14:24 | ||
| | ||
LL | let _: bool = n[0].first().is_none(); | ||
| ^^^^^^^^^^^^^^^^^ help: replace this with: `is_empty()` | ||
|
||
error: unnecessary use of `first().is_some()` to check if slice is not empty | ||
--> tests/ui/unnecessary_first_then_check.rs:20:19 | ||
| | ||
LL | let _: bool = f[0].bar.first().is_some(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace this with: `!f[0].bar.is_empty()` | ||
|
||
error: unnecessary use of `first().is_none()` to check if slice is empty | ||
--> tests/ui/unnecessary_first_then_check.rs:21:28 | ||
| | ||
LL | let _: bool = f[0].bar.first().is_none(); | ||
| ^^^^^^^^^^^^^^^^^ help: replace this with: `is_empty()` | ||
|
||
error: aborting due to 7 previous errors | ||
|