@@ -111,6 +111,7 @@ mod uninit_assumed_init;
111
111
mod unit_hash;
112
112
mod unnecessary_fallible_conversions;
113
113
mod unnecessary_filter_map;
114
+ mod unnecessary_first_then_check;
114
115
mod unnecessary_fold;
115
116
mod unnecessary_get_then_check;
116
117
mod unnecessary_iter_cloned;
@@ -4137,6 +4138,34 @@ declare_clippy_lint! {
4137
4138
"use of `map` returning the original item"
4138
4139
}
4139
4140
4141
+ declare_clippy_lint ! {
4142
+ /// ### What it does
4143
+ /// Checks the usage of `.first().is_some()` or `.first().is_none()` to check if a slice is
4144
+ /// empty.
4145
+ ///
4146
+ /// ### Why is this bad?
4147
+ /// Using `.is_empty()` is shorter and better communicates the intention.
4148
+ ///
4149
+ /// ### Example
4150
+ /// ```no_run
4151
+ /// let v = vec![1, 2, 3];
4152
+ /// if v.first().is_none() {
4153
+ /// // The vector is empty...
4154
+ /// }
4155
+ /// ```
4156
+ /// Use instead:
4157
+ /// ```no_run
4158
+ /// let v = vec![1, 2, 3];
4159
+ /// if v.is_empty() {
4160
+ /// // The vector is empty...
4161
+ /// }
4162
+ /// ```
4163
+ #[ clippy:: version = "1.83.0" ]
4164
+ pub UNNECESSARY_FIRST_THEN_CHECK ,
4165
+ complexity,
4166
+ "calling `.first().is_some()` or `.first().is_none()` instead of `.is_empty()`"
4167
+ }
4168
+
4140
4169
pub struct Methods {
4141
4170
avoid_breaking_exported_api : bool ,
4142
4171
msrv : Msrv ,
@@ -4294,6 +4323,7 @@ impl_lint_pass!(Methods => [
4294
4323
UNNECESSARY_RESULT_MAP_OR_ELSE ,
4295
4324
MANUAL_C_STR_LITERALS ,
4296
4325
UNNECESSARY_GET_THEN_CHECK ,
4326
+ UNNECESSARY_FIRST_THEN_CHECK ,
4297
4327
NEEDLESS_CHARACTER_ITERATION ,
4298
4328
MANUAL_INSPECT ,
4299
4329
UNNECESSARY_MIN_OR_MAX ,
@@ -5066,6 +5096,9 @@ fn check_is_some_is_none(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>,
5066
5096
Some ( ( "get" , f_recv, [ arg] , _, _) ) => {
5067
5097
unnecessary_get_then_check:: check ( cx, call_span, recv, f_recv, arg, is_some) ;
5068
5098
} ,
5099
+ Some ( ( "first" , f_recv, [ ] , _, _) ) => {
5100
+ unnecessary_first_then_check:: check ( cx, call_span, recv, f_recv, is_some) ;
5101
+ } ,
5069
5102
_ => { } ,
5070
5103
}
5071
5104
}
0 commit comments