@@ -111,6 +111,7 @@ mod uninit_assumed_init;
111111mod unit_hash;
112112mod unnecessary_fallible_conversions;
113113mod unnecessary_filter_map;
114+ mod unnecessary_first_then_check;
114115mod unnecessary_fold;
115116mod unnecessary_get_then_check;
116117mod unnecessary_iter_cloned;
@@ -4137,6 +4138,34 @@ declare_clippy_lint! {
41374138 "use of `map` returning the original item"
41384139}
41394140
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+
41404169pub struct Methods {
41414170 avoid_breaking_exported_api : bool ,
41424171 msrv : Msrv ,
@@ -4294,6 +4323,7 @@ impl_lint_pass!(Methods => [
42944323 UNNECESSARY_RESULT_MAP_OR_ELSE ,
42954324 MANUAL_C_STR_LITERALS ,
42964325 UNNECESSARY_GET_THEN_CHECK ,
4326+ UNNECESSARY_FIRST_THEN_CHECK ,
42974327 NEEDLESS_CHARACTER_ITERATION ,
42984328 MANUAL_INSPECT ,
42994329 UNNECESSARY_MIN_OR_MAX ,
@@ -5066,6 +5096,9 @@ fn check_is_some_is_none(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>,
50665096 Some ( ( "get" , f_recv, [ arg] , _, _) ) => {
50675097 unnecessary_get_then_check:: check ( cx, call_span, recv, f_recv, arg, is_some) ;
50685098 } ,
5099+ Some ( ( "first" , f_recv, [ ] , _, _) ) => {
5100+ unnecessary_first_then_check:: check ( cx, call_span, recv, f_recv, is_some) ;
5101+ } ,
50695102 _ => { } ,
50705103 }
50715104}
0 commit comments