@@ -78,6 +78,7 @@ mod path_ends_with_ext;
7878mod range_zip_with_len;
7979mod read_line_without_trim;
8080mod readonly_write_lock;
81+ mod redundant_as_str;
8182mod repeat_once;
8283mod search_is_some;
8384mod seek_from_current;
@@ -3605,6 +3606,32 @@ declare_clippy_lint! {
36053606 "attempting to compare file extensions using `Path::ends_with`"
36063607}
36073608
3609+ declare_clippy_lint ! {
3610+ /// ### What it does
3611+ /// Checks for usage of `as_str()` on a `String`` chained with a method available on the `String` itself.
3612+ ///
3613+ /// ### Why is this bad?
3614+ /// The `as_str()` conversion is pointless and can be removed for simplicity and cleanliness.
3615+ ///
3616+ /// ### Example
3617+ /// ```rust
3618+ /// # #![allow(unused)]
3619+ /// let owned_string = "This is a string".to_owned();
3620+ /// owned_string.as_str().as_bytes()
3621+ /// ```
3622+ ///
3623+ /// Use instead:
3624+ /// ```rust
3625+ /// # #![allow(unused)]
3626+ /// let owned_string = "This is a string".to_owned();
3627+ /// owned_string.as_bytes()
3628+ /// ```
3629+ #[ clippy:: version = "1.74.0" ]
3630+ pub REDUNDANT_AS_STR ,
3631+ complexity,
3632+ "`as_str` used to call a method on `str` that is also available on `String`"
3633+ }
3634+
36083635pub struct Methods {
36093636 avoid_breaking_exported_api : bool ,
36103637 msrv : Msrv ,
@@ -3749,6 +3776,7 @@ impl_lint_pass!(Methods => [
37493776 READONLY_WRITE_LOCK ,
37503777 ITER_OUT_OF_BOUNDS ,
37513778 PATH_ENDS_WITH_EXT ,
3779+ REDUNDANT_AS_STR ,
37523780] ) ;
37533781
37543782/// Extracts a method call name, args, and `Span` of the method name.
@@ -3975,6 +4003,7 @@ impl Methods {
39754003 ( "as_deref" | "as_deref_mut" , [ ] ) => {
39764004 needless_option_as_deref:: check ( cx, expr, recv, name) ;
39774005 } ,
4006+ ( "as_bytes" | "is_empty" , [ ] ) => if let Some ( ( "as_str" , recv, [ ] , as_str_span, _) ) = method_call ( recv) { redundant_as_str:: check ( cx, expr, recv, as_str_span, span) ; } ,
39784007 ( "as_mut" , [ ] ) => useless_asref:: check ( cx, expr, "as_mut" , recv) ,
39794008 ( "as_ref" , [ ] ) => useless_asref:: check ( cx, expr, "as_ref" , recv) ,
39804009 ( "assume_init" , [ ] ) => uninit_assumed_init:: check ( cx, expr, recv) ,
0 commit comments