Skip to content

Commit 5cba09c

Browse files
committed
Auto merge of #11526 - Dev380:redundant-as-str, r=Manishearth
Add redundant_as_str lint This lint checks for `as_str` on a `String` immediately followed by `as_bytes` or `is_empty` as those methods are available on `String` too. This could possibly also be extended to `&[u8]` in the future. changelog: New lint [`redundant_as_str`] #11526
2 parents 4022591 + d9d25e9 commit 5cba09c

File tree

7 files changed

+130
-0
lines changed

7 files changed

+130
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5281,6 +5281,7 @@ Released 2018-09-13
52815281
[`readonly_write_lock`]: https://rust-lang.github.io/rust-clippy/master/index.html#readonly_write_lock
52825282
[`recursive_format_impl`]: https://rust-lang.github.io/rust-clippy/master/index.html#recursive_format_impl
52835283
[`redundant_allocation`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_allocation
5284+
[`redundant_as_str`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_as_str
52845285
[`redundant_async_block`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_async_block
52855286
[`redundant_at_rest_pattern`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_at_rest_pattern
52865287
[`redundant_clone`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_clone

clippy_lints/src/declared_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
403403
crate::methods::RANGE_ZIP_WITH_LEN_INFO,
404404
crate::methods::READONLY_WRITE_LOCK_INFO,
405405
crate::methods::READ_LINE_WITHOUT_TRIM_INFO,
406+
crate::methods::REDUNDANT_AS_STR_INFO,
406407
crate::methods::REPEAT_ONCE_INFO,
407408
crate::methods::RESULT_MAP_OR_INTO_OPTION_INFO,
408409
crate::methods::SEARCH_IS_SOME_INFO,

clippy_lints/src/methods/mod.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ mod path_ends_with_ext;
7878
mod range_zip_with_len;
7979
mod read_line_without_trim;
8080
mod readonly_write_lock;
81+
mod redundant_as_str;
8182
mod repeat_once;
8283
mod search_is_some;
8384
mod 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+
36083635
pub 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),
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use super::REDUNDANT_AS_STR;
2+
use clippy_utils::diagnostics::span_lint_and_sugg;
3+
use clippy_utils::source::snippet_with_applicability;
4+
use rustc_errors::Applicability;
5+
use rustc_hir::Expr;
6+
use rustc_lint::LateContext;
7+
use rustc_middle::query::Key;
8+
use rustc_span::Span;
9+
10+
pub(super) fn check(
11+
cx: &LateContext<'_>,
12+
_expr: &Expr<'_>,
13+
recv: &Expr<'_>,
14+
as_str_span: Span,
15+
other_method_span: Span,
16+
) {
17+
if cx
18+
.tcx
19+
.lang_items()
20+
.string()
21+
.is_some_and(|id| Some(id) == cx.typeck_results().expr_ty(recv).ty_adt_id())
22+
{
23+
let mut applicability = Applicability::MachineApplicable;
24+
span_lint_and_sugg(
25+
cx,
26+
REDUNDANT_AS_STR,
27+
as_str_span.to(other_method_span),
28+
"this `as_str` is redundant and can be removed as the method immediately following exists on `String` too",
29+
"try",
30+
snippet_with_applicability(cx, other_method_span, "..", &mut applicability).into_owned(),
31+
applicability,
32+
);
33+
}
34+
}

tests/ui/redundant_as_str.fixed

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#![warn(clippy::redundant_as_str)]
2+
3+
fn main() {
4+
let string = "Hello, world!".to_owned();
5+
6+
// These methods are redundant and the `as_str` can be removed
7+
let _redundant = string.as_bytes();
8+
let _redundant = string.is_empty();
9+
10+
// These methods don't use `as_str` when they are redundant
11+
let _no_as_str = string.as_bytes();
12+
let _no_as_str = string.is_empty();
13+
14+
// These methods are not redundant, and are equivelant to
15+
// doing dereferencing the string and applying the method
16+
let _not_redundant = string.as_str().escape_unicode();
17+
let _not_redundant = string.as_str().trim();
18+
let _not_redundant = string.as_str().split_whitespace();
19+
20+
// These methods don't use `as_str` and are applied on a `str` directly
21+
let borrowed_str = "Hello, world!";
22+
let _is_str = borrowed_str.as_bytes();
23+
let _is_str = borrowed_str.is_empty();
24+
}

tests/ui/redundant_as_str.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#![warn(clippy::redundant_as_str)]
2+
3+
fn main() {
4+
let string = "Hello, world!".to_owned();
5+
6+
// These methods are redundant and the `as_str` can be removed
7+
let _redundant = string.as_str().as_bytes();
8+
let _redundant = string.as_str().is_empty();
9+
10+
// These methods don't use `as_str` when they are redundant
11+
let _no_as_str = string.as_bytes();
12+
let _no_as_str = string.is_empty();
13+
14+
// These methods are not redundant, and are equivelant to
15+
// doing dereferencing the string and applying the method
16+
let _not_redundant = string.as_str().escape_unicode();
17+
let _not_redundant = string.as_str().trim();
18+
let _not_redundant = string.as_str().split_whitespace();
19+
20+
// These methods don't use `as_str` and are applied on a `str` directly
21+
let borrowed_str = "Hello, world!";
22+
let _is_str = borrowed_str.as_bytes();
23+
let _is_str = borrowed_str.is_empty();
24+
}

tests/ui/redundant_as_str.stderr

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error: this `as_str` is redundant and can be removed as the method immediately following exists on `String` too
2+
--> $DIR/redundant_as_str.rs:7:29
3+
|
4+
LL | let _redundant = string.as_str().as_bytes();
5+
| ^^^^^^^^^^^^^^^^^ help: try: `as_bytes`
6+
|
7+
= note: `-D clippy::redundant-as-str` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::redundant_as_str)]`
9+
10+
error: this `as_str` is redundant and can be removed as the method immediately following exists on `String` too
11+
--> $DIR/redundant_as_str.rs:8:29
12+
|
13+
LL | let _redundant = string.as_str().is_empty();
14+
| ^^^^^^^^^^^^^^^^^ help: try: `is_empty`
15+
16+
error: aborting due to 2 previous errors
17+

0 commit comments

Comments
 (0)