From d8752dbf4080c5ce4b87ae753a0ef510ff67617b Mon Sep 17 00:00:00 2001 From: wowinter13 Date: Wed, 15 Jan 2025 15:58:18 +0100 Subject: [PATCH 01/12] New lint --- clippy_lints/src/declared_lints.rs | 1 + clippy_lints/src/methods/mod.rs | 31 +++++++++++++++++++ clippy_lints/src/methods/slice_as_bytes.rs | 30 ++++++++++++++++++ tests/ui/bytes_nth.fixed | 1 + tests/ui/bytes_nth.rs | 1 + tests/ui/bytes_nth.stderr | 6 ++-- tests/ui/slice_as_bytes.fixed | 35 +++++++++++++++++++++ tests/ui/slice_as_bytes.rs | 36 ++++++++++++++++++++++ tests/ui/slice_as_bytes.stderr | 21 +++++++++++++ 9 files changed, 159 insertions(+), 3 deletions(-) create mode 100644 clippy_lints/src/methods/slice_as_bytes.rs create mode 100644 tests/ui/slice_as_bytes.fixed create mode 100644 tests/ui/slice_as_bytes.rs create mode 100644 tests/ui/slice_as_bytes.stderr diff --git a/clippy_lints/src/declared_lints.rs b/clippy_lints/src/declared_lints.rs index 86410c16d95f..0917dcff3085 100644 --- a/clippy_lints/src/declared_lints.rs +++ b/clippy_lints/src/declared_lints.rs @@ -503,6 +503,7 @@ pub static LINTS: &[&crate::LintInfo] = &[ crate::methods::WAKER_CLONE_WAKE_INFO, crate::methods::WRONG_SELF_CONVENTION_INFO, crate::methods::ZST_OFFSET_INFO, + crate::methods::SLICE_AS_BYTES_INFO, crate::min_ident_chars::MIN_IDENT_CHARS_INFO, crate::minmax::MIN_MAX_INFO, crate::misc::SHORT_CIRCUIT_STATEMENT_INFO, diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index 9bfa59479905..4a00da39a4b3 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -102,6 +102,7 @@ mod single_char_add_str; mod single_char_insert_string; mod single_char_push_string; mod skip_while_next; +mod slice_as_bytes; mod stable_sort_primitive; mod str_split; mod str_splitn; @@ -4363,6 +4364,34 @@ declare_clippy_lint! { "detect `repeat().take()` that can be replaced with `repeat_n()`" } +declare_clippy_lint! { + /// ### What it does + /// Checks for string slices immediantly followed by `as_bytes`. + /// + /// ### Why is this bad? + /// It involves doing an unnecessary UTF-8 alignment check which is less efficient, and can cause a panic. + /// + /// ### Known problems + /// In some cases, the UTF-8 validation and potential panic from string slicing may be required for + /// the code's correctness. If you need to ensure the slice boundaries fall on valid UTF-8 character + /// boundaries, the original form (`s[1..5].as_bytes()`) should be preferred. + /// + /// ### Example + /// ```rust + /// let s = "Lorem ipsum"; + /// s[1..5].as_bytes(); + /// ``` + /// Use instead: + /// ```rust + /// let s = "Lorem ipsum"; + /// &s.as_bytes()[1..5]; + /// ``` + #[clippy::version = "1.72.0"] + pub SLICE_AS_BYTES, + pedantic, + "slicing a string and immediately calling as_bytes is less efficient and can lead to panics" +} + pub struct Methods { avoid_breaking_exported_api: bool, msrv: Msrv, @@ -4531,6 +4560,7 @@ impl_lint_pass!(Methods => [ DOUBLE_ENDED_ITERATOR_LAST, USELESS_NONZERO_NEW_UNCHECKED, MANUAL_REPEAT_N, + SLICE_AS_BYTES, ]); /// Extracts a method call name, args, and `Span` of the method name. @@ -4798,6 +4828,7 @@ impl Methods { if let Some(("as_str", recv, [], as_str_span, _)) = method_call(recv) { redundant_as_str::check(cx, expr, recv, as_str_span, span); } + slice_as_bytes::check(cx, expr, recv); }, ("as_mut", []) => useless_asref::check(cx, expr, "as_mut", recv), ("as_ptr", []) => manual_c_str_literals::check_as_ptr(cx, expr, recv, &self.msrv), diff --git a/clippy_lints/src/methods/slice_as_bytes.rs b/clippy_lints/src/methods/slice_as_bytes.rs new file mode 100644 index 000000000000..6c2aeab33080 --- /dev/null +++ b/clippy_lints/src/methods/slice_as_bytes.rs @@ -0,0 +1,30 @@ +use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::source::snippet_with_applicability; +use clippy_utils::ty::is_type_lang_item; +use rustc_errors::Applicability; +use rustc_hir::{Expr, ExprKind, LangItem, is_range_literal}; +use rustc_lint::LateContext; + +use super::SLICE_AS_BYTES; + +pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>) { + if let ExprKind::Index(indexed, index) = recv.kind + && is_range_literal(index) + { + let ty = cx.typeck_results().expr_ty(indexed).peel_refs(); + if ty.is_str() || is_type_lang_item(cx, ty, LangItem::String) { + let mut applicability = Applicability::MaybeIncorrect; + let stringish = snippet_with_applicability(cx, indexed.span, "..", &mut applicability); + let range = snippet_with_applicability(cx, index.span, "..", &mut applicability); + span_lint_and_sugg( + cx, + SLICE_AS_BYTES, + expr.span, + "calling `as_bytes` after slicing a string", + "try", + format!("&{stringish}.as_bytes()[{range}]"), + applicability, + ); + } + } +} diff --git a/tests/ui/bytes_nth.fixed b/tests/ui/bytes_nth.fixed index 11deb2390fd4..d58eb5227fbf 100644 --- a/tests/ui/bytes_nth.fixed +++ b/tests/ui/bytes_nth.fixed @@ -1,4 +1,5 @@ #![allow(clippy::unnecessary_operation)] +#![allow(clippy::slice_as_bytes)] #![warn(clippy::bytes_nth)] fn main() { diff --git a/tests/ui/bytes_nth.rs b/tests/ui/bytes_nth.rs index 62d9c7a5ea79..bbfe388e8bb1 100644 --- a/tests/ui/bytes_nth.rs +++ b/tests/ui/bytes_nth.rs @@ -1,4 +1,5 @@ #![allow(clippy::unnecessary_operation)] +#![allow(clippy::slice_as_bytes)] #![warn(clippy::bytes_nth)] fn main() { diff --git a/tests/ui/bytes_nth.stderr b/tests/ui/bytes_nth.stderr index c6f21576c3db..c5f341cb37f6 100644 --- a/tests/ui/bytes_nth.stderr +++ b/tests/ui/bytes_nth.stderr @@ -1,5 +1,5 @@ error: called `.bytes().nth()` on a `String` - --> tests/ui/bytes_nth.rs:6:13 + --> tests/ui/bytes_nth.rs:7:13 | LL | let _ = s.bytes().nth(3); | ^^^^^^^^^^^^^^^^ help: try: `s.as_bytes().get(3).copied()` @@ -8,13 +8,13 @@ LL | let _ = s.bytes().nth(3); = help: to override `-D warnings` add `#[allow(clippy::bytes_nth)]` error: called `.bytes().nth().unwrap()` on a `String` - --> tests/ui/bytes_nth.rs:7:14 + --> tests/ui/bytes_nth.rs:8:14 | LL | let _ = &s.bytes().nth(3).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `s.as_bytes()[3]` error: called `.bytes().nth()` on a `str` - --> tests/ui/bytes_nth.rs:8:13 + --> tests/ui/bytes_nth.rs:9:13 | LL | let _ = s[..].bytes().nth(3); | ^^^^^^^^^^^^^^^^^^^^ help: try: `s[..].as_bytes().get(3).copied()` diff --git a/tests/ui/slice_as_bytes.fixed b/tests/ui/slice_as_bytes.fixed new file mode 100644 index 000000000000..fcbcb80a9634 --- /dev/null +++ b/tests/ui/slice_as_bytes.fixed @@ -0,0 +1,35 @@ +//@run-rustfix +#![allow(unused)] +#![warn(clippy::slice_as_bytes)] + +use std::ops::{Index, Range}; + +struct Foo; + +struct Bar; + +impl Bar { + fn as_bytes(&self) -> &[u8] { + &[0, 1, 2, 3] + } +} + +impl Index> for Foo { + type Output = Bar; + + fn index(&self, _: Range) -> &Self::Output { + &Bar + } +} + +fn main() { + let s = "Lorem ipsum"; + let string: String = "dolor sit amet".to_owned(); + + let bytes = &s.as_bytes()[1..5]; + let bytes = &string.as_bytes()[1..]; + let bytes = &"consectetur adipiscing".as_bytes()[..=5]; + + let f = Foo; + let bytes = f[0..4].as_bytes(); +} \ No newline at end of file diff --git a/tests/ui/slice_as_bytes.rs b/tests/ui/slice_as_bytes.rs new file mode 100644 index 000000000000..4ff6c5ac7844 --- /dev/null +++ b/tests/ui/slice_as_bytes.rs @@ -0,0 +1,36 @@ +//@run-rustfix + +#![allow(unused)] +#![warn(clippy::slice_as_bytes)] + +use std::ops::{Index, Range}; + +struct Foo; + +struct Bar; + +impl Bar { + fn as_bytes(&self) -> &[u8] { + &[0, 1, 2, 3] + } +} + +impl Index> for Foo { + type Output = Bar; + + fn index(&self, _: Range) -> &Self::Output { + &Bar + } +} + +fn main() { + let s = "Lorem ipsum"; + let string: String = "dolor sit amet".to_owned(); + + let bytes = s[1..5].as_bytes(); + let bytes = string[1..].as_bytes(); + let bytes = "consectetur adipiscing"[..=5].as_bytes(); + + let f = Foo; + let bytes = f[0..4].as_bytes(); +} \ No newline at end of file diff --git a/tests/ui/slice_as_bytes.stderr b/tests/ui/slice_as_bytes.stderr new file mode 100644 index 000000000000..2a3251b4d9cb --- /dev/null +++ b/tests/ui/slice_as_bytes.stderr @@ -0,0 +1,21 @@ +error: slicing a str before calling `as_bytes` results in needless UTF-8 alignment checks, and has the possiblity of panicking +--> $DIR/slice_as_bytes.rs:29:17 +| +LL | let bytes = s[1..5].as_bytes(); +| ^^^^^^^^^^^^^^^^^^ help: try: `&s.as_bytes()[1..5]` +| += note: `-D clippy::slice-as-bytes` implied by `-D warnings` + +error: slicing a String before calling `as_bytes` results in needless UTF-8 alignment checks, and has the possiblity of panicking +--> $DIR/slice_as_bytes.rs:30:17 +| +LL | let bytes = string[1..].as_bytes(); +| ^^^^^^^^^^^^^^^^^^^^^^ help: try: `&string.as_bytes()[1..]` + +error: slicing a str before calling `as_bytes` results in needless UTF-8 alignment checks, and has the possiblity of panicking +--> $DIR/slice_as_bytes.rs:31:17 +| +LL | let bytes = "consectetur adipiscing"[..=5].as_bytes(); +| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&"consectetur adipiscing".as_bytes()[..=5]` + +error: aborting due to 3 previous errors From c214f5144e1f7638dc007b0a389bb769528e56a8 Mon Sep 17 00:00:00 2001 From: wowinter13 Date: Wed, 15 Jan 2025 16:07:08 +0100 Subject: [PATCH 02/12] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ce5b8fb392ff..3a55c9f67ff2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6066,6 +6066,7 @@ Released 2018-09-13 [`size_of_in_element_count`]: https://rust-lang.github.io/rust-clippy/master/index.html#size_of_in_element_count [`size_of_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#size_of_ref [`skip_while_next`]: https://rust-lang.github.io/rust-clippy/master/index.html#skip_while_next +[`slice_as_bytes`]: https://rust-lang.github.io/rust-clippy/master/index.html#slice_as_bytes [`slow_vector_initialization`]: https://rust-lang.github.io/rust-clippy/master/index.html#slow_vector_initialization [`stable_sort_primitive`]: https://rust-lang.github.io/rust-clippy/master/index.html#stable_sort_primitive [`std_instead_of_alloc`]: https://rust-lang.github.io/rust-clippy/master/index.html#std_instead_of_alloc From 2ad20daa2a2e9c0be30806fc93b54bf1dcca2559 Mon Sep 17 00:00:00 2001 From: wowinter13 Date: Wed, 15 Jan 2025 16:13:59 +0100 Subject: [PATCH 03/12] Rollback CHANGELOG.md change --- CHANGELOG.md | 1 - tests/ui/slice_as_bytes.fixed | 2 +- tests/ui/slice_as_bytes.rs | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a55c9f67ff2..ce5b8fb392ff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6066,7 +6066,6 @@ Released 2018-09-13 [`size_of_in_element_count`]: https://rust-lang.github.io/rust-clippy/master/index.html#size_of_in_element_count [`size_of_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#size_of_ref [`skip_while_next`]: https://rust-lang.github.io/rust-clippy/master/index.html#skip_while_next -[`slice_as_bytes`]: https://rust-lang.github.io/rust-clippy/master/index.html#slice_as_bytes [`slow_vector_initialization`]: https://rust-lang.github.io/rust-clippy/master/index.html#slow_vector_initialization [`stable_sort_primitive`]: https://rust-lang.github.io/rust-clippy/master/index.html#stable_sort_primitive [`std_instead_of_alloc`]: https://rust-lang.github.io/rust-clippy/master/index.html#std_instead_of_alloc diff --git a/tests/ui/slice_as_bytes.fixed b/tests/ui/slice_as_bytes.fixed index fcbcb80a9634..22e6ba580e20 100644 --- a/tests/ui/slice_as_bytes.fixed +++ b/tests/ui/slice_as_bytes.fixed @@ -32,4 +32,4 @@ fn main() { let f = Foo; let bytes = f[0..4].as_bytes(); -} \ No newline at end of file +} diff --git a/tests/ui/slice_as_bytes.rs b/tests/ui/slice_as_bytes.rs index 4ff6c5ac7844..ceb2ee9f66fb 100644 --- a/tests/ui/slice_as_bytes.rs +++ b/tests/ui/slice_as_bytes.rs @@ -33,4 +33,4 @@ fn main() { let f = Foo; let bytes = f[0..4].as_bytes(); -} \ No newline at end of file +} From 307d85409b141eb60724147e5cc6d41ddad569d4 Mon Sep 17 00:00:00 2001 From: wowinter13 Date: Wed, 15 Jan 2025 16:37:55 +0100 Subject: [PATCH 04/12] Address linter & changelog issues --- CHANGELOG.md | 1 + clippy_lints/src/declared_lints.rs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ce5b8fb392ff..3a55c9f67ff2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6066,6 +6066,7 @@ Released 2018-09-13 [`size_of_in_element_count`]: https://rust-lang.github.io/rust-clippy/master/index.html#size_of_in_element_count [`size_of_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#size_of_ref [`skip_while_next`]: https://rust-lang.github.io/rust-clippy/master/index.html#skip_while_next +[`slice_as_bytes`]: https://rust-lang.github.io/rust-clippy/master/index.html#slice_as_bytes [`slow_vector_initialization`]: https://rust-lang.github.io/rust-clippy/master/index.html#slow_vector_initialization [`stable_sort_primitive`]: https://rust-lang.github.io/rust-clippy/master/index.html#stable_sort_primitive [`std_instead_of_alloc`]: https://rust-lang.github.io/rust-clippy/master/index.html#std_instead_of_alloc diff --git a/clippy_lints/src/declared_lints.rs b/clippy_lints/src/declared_lints.rs index 0917dcff3085..e822aedbd2c6 100644 --- a/clippy_lints/src/declared_lints.rs +++ b/clippy_lints/src/declared_lints.rs @@ -468,6 +468,7 @@ pub static LINTS: &[&crate::LintInfo] = &[ crate::methods::SHOULD_IMPLEMENT_TRAIT_INFO, crate::methods::SINGLE_CHAR_ADD_STR_INFO, crate::methods::SKIP_WHILE_NEXT_INFO, + crate::methods::SLICE_AS_BYTES_INFO, crate::methods::STABLE_SORT_PRIMITIVE_INFO, crate::methods::STRING_EXTEND_CHARS_INFO, crate::methods::STRING_LIT_CHARS_ANY_INFO, @@ -503,7 +504,6 @@ pub static LINTS: &[&crate::LintInfo] = &[ crate::methods::WAKER_CLONE_WAKE_INFO, crate::methods::WRONG_SELF_CONVENTION_INFO, crate::methods::ZST_OFFSET_INFO, - crate::methods::SLICE_AS_BYTES_INFO, crate::min_ident_chars::MIN_IDENT_CHARS_INFO, crate::minmax::MIN_MAX_INFO, crate::misc::SHORT_CIRCUIT_STATEMENT_INFO, From 7124d822b433342360a299f3260e13b3c6b7bcf8 Mon Sep 17 00:00:00 2001 From: wowinter13 Date: Wed, 15 Jan 2025 16:43:42 +0100 Subject: [PATCH 05/12] Fix function signature --- clippy_lints/src/methods/slice_as_bytes.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clippy_lints/src/methods/slice_as_bytes.rs b/clippy_lints/src/methods/slice_as_bytes.rs index 6c2aeab33080..4040008c98ac 100644 --- a/clippy_lints/src/methods/slice_as_bytes.rs +++ b/clippy_lints/src/methods/slice_as_bytes.rs @@ -8,7 +8,7 @@ use rustc_lint::LateContext; use super::SLICE_AS_BYTES; pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>) { - if let ExprKind::Index(indexed, index) = recv.kind + if let ExprKind::Index(indexed, index, _) = recv.kind && is_range_literal(index) { let ty = cx.typeck_results().expr_ty(indexed).peel_refs(); From 43066fea88205824a98d207b0e38a05ce36245f7 Mon Sep 17 00:00:00 2001 From: wowinter13 Date: Wed, 15 Jan 2025 17:01:56 +0100 Subject: [PATCH 06/12] Fix related test --- tests/ui/slice_as_bytes.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/ui/slice_as_bytes.rs b/tests/ui/slice_as_bytes.rs index ceb2ee9f66fb..095b7773810b 100644 --- a/tests/ui/slice_as_bytes.rs +++ b/tests/ui/slice_as_bytes.rs @@ -1,5 +1,3 @@ -//@run-rustfix - #![allow(unused)] #![warn(clippy::slice_as_bytes)] From 9e83183f7ee7b7132f2bd6d857aee7bc2c7e46b3 Mon Sep 17 00:00:00 2001 From: wowinter13 Date: Wed, 15 Jan 2025 17:19:21 +0100 Subject: [PATCH 07/12] uibless for tests --- tests/ui/slice_as_bytes.fixed | 1 - tests/ui/slice_as_bytes.stderr | 30 ++++++++++++++++-------------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/tests/ui/slice_as_bytes.fixed b/tests/ui/slice_as_bytes.fixed index 22e6ba580e20..da627717a529 100644 --- a/tests/ui/slice_as_bytes.fixed +++ b/tests/ui/slice_as_bytes.fixed @@ -1,4 +1,3 @@ -//@run-rustfix #![allow(unused)] #![warn(clippy::slice_as_bytes)] diff --git a/tests/ui/slice_as_bytes.stderr b/tests/ui/slice_as_bytes.stderr index 2a3251b4d9cb..9a787b0b9173 100644 --- a/tests/ui/slice_as_bytes.stderr +++ b/tests/ui/slice_as_bytes.stderr @@ -1,21 +1,23 @@ -error: slicing a str before calling `as_bytes` results in needless UTF-8 alignment checks, and has the possiblity of panicking ---> $DIR/slice_as_bytes.rs:29:17 -| +error: calling `as_bytes` after slicing a string + --> tests/ui/slice_as_bytes.rs:28:17 + | LL | let bytes = s[1..5].as_bytes(); -| ^^^^^^^^^^^^^^^^^^ help: try: `&s.as_bytes()[1..5]` -| -= note: `-D clippy::slice-as-bytes` implied by `-D warnings` + | ^^^^^^^^^^^^^^^^^^ help: try: `&s.as_bytes()[1..5]` + | + = note: `-D clippy::slice-as-bytes` implied by `-D warnings` + = help: to override `-D warnings` add `#[allow(clippy::slice_as_bytes)]` -error: slicing a String before calling `as_bytes` results in needless UTF-8 alignment checks, and has the possiblity of panicking ---> $DIR/slice_as_bytes.rs:30:17 -| +error: calling `as_bytes` after slicing a string + --> tests/ui/slice_as_bytes.rs:29:17 + | LL | let bytes = string[1..].as_bytes(); -| ^^^^^^^^^^^^^^^^^^^^^^ help: try: `&string.as_bytes()[1..]` + | ^^^^^^^^^^^^^^^^^^^^^^ help: try: `&string.as_bytes()[1..]` -error: slicing a str before calling `as_bytes` results in needless UTF-8 alignment checks, and has the possiblity of panicking ---> $DIR/slice_as_bytes.rs:31:17 -| +error: calling `as_bytes` after slicing a string + --> tests/ui/slice_as_bytes.rs:30:17 + | LL | let bytes = "consectetur adipiscing"[..=5].as_bytes(); -| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&"consectetur adipiscing".as_bytes()[..=5]` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&"consectetur adipiscing".as_bytes()[..=5]` error: aborting due to 3 previous errors + From 26a7b322a37c1eeefb324bd3d082eeb36654c6a2 Mon Sep 17 00:00:00 2001 From: wowinter13 Date: Wed, 15 Jan 2025 22:15:49 +0100 Subject: [PATCH 08/12] Rename slice_as_bytes -> sliced_string_as_bytes --- CHANGELOG.md | 2 +- clippy_lints/src/declared_lints.rs | 2 +- clippy_lints/src/methods/mod.rs | 10 +++++----- .../{slice_as_bytes.rs => sliced_string_as_bytes.rs} | 4 ++-- tests/ui/bytes_nth.fixed | 2 +- tests/ui/bytes_nth.rs | 2 +- ...ice_as_bytes.fixed => sliced_string_as_bytes.fixed} | 2 +- .../{slice_as_bytes.rs => sliced_string_as_bytes.rs} | 2 +- ...e_as_bytes.stderr => sliced_string_as_bytes.stderr} | 8 ++++---- 9 files changed, 17 insertions(+), 17 deletions(-) rename clippy_lints/src/methods/{slice_as_bytes.rs => sliced_string_as_bytes.rs} (93%) rename tests/ui/{slice_as_bytes.fixed => sliced_string_as_bytes.fixed} (93%) rename tests/ui/{slice_as_bytes.rs => sliced_string_as_bytes.rs} (93%) rename tests/ui/{slice_as_bytes.stderr => sliced_string_as_bytes.stderr} (75%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a55c9f67ff2..2757597fc511 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6066,7 +6066,7 @@ Released 2018-09-13 [`size_of_in_element_count`]: https://rust-lang.github.io/rust-clippy/master/index.html#size_of_in_element_count [`size_of_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#size_of_ref [`skip_while_next`]: https://rust-lang.github.io/rust-clippy/master/index.html#skip_while_next -[`slice_as_bytes`]: https://rust-lang.github.io/rust-clippy/master/index.html#slice_as_bytes +[`sliced_string_as_bytes`]: https://rust-lang.github.io/rust-clippy/master/index.html#sliced_string_as_bytes [`slow_vector_initialization`]: https://rust-lang.github.io/rust-clippy/master/index.html#slow_vector_initialization [`stable_sort_primitive`]: https://rust-lang.github.io/rust-clippy/master/index.html#stable_sort_primitive [`std_instead_of_alloc`]: https://rust-lang.github.io/rust-clippy/master/index.html#std_instead_of_alloc diff --git a/clippy_lints/src/declared_lints.rs b/clippy_lints/src/declared_lints.rs index e822aedbd2c6..ec223381aec6 100644 --- a/clippy_lints/src/declared_lints.rs +++ b/clippy_lints/src/declared_lints.rs @@ -468,7 +468,7 @@ pub static LINTS: &[&crate::LintInfo] = &[ crate::methods::SHOULD_IMPLEMENT_TRAIT_INFO, crate::methods::SINGLE_CHAR_ADD_STR_INFO, crate::methods::SKIP_WHILE_NEXT_INFO, - crate::methods::SLICE_AS_BYTES_INFO, + crate::methods::SLICED_STRING_AS_BYTES_INFO, crate::methods::STABLE_SORT_PRIMITIVE_INFO, crate::methods::STRING_EXTEND_CHARS_INFO, crate::methods::STRING_LIT_CHARS_ANY_INFO, diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index 4a00da39a4b3..3953fe03f314 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -102,7 +102,7 @@ mod single_char_add_str; mod single_char_insert_string; mod single_char_push_string; mod skip_while_next; -mod slice_as_bytes; +mod sliced_string_as_bytes; mod stable_sort_primitive; mod str_split; mod str_splitn; @@ -4386,8 +4386,8 @@ declare_clippy_lint! { /// let s = "Lorem ipsum"; /// &s.as_bytes()[1..5]; /// ``` - #[clippy::version = "1.72.0"] - pub SLICE_AS_BYTES, + #[clippy::version = "1.86.0"] + pub SLICED_STRING_AS_BYTES, pedantic, "slicing a string and immediately calling as_bytes is less efficient and can lead to panics" } @@ -4560,7 +4560,7 @@ impl_lint_pass!(Methods => [ DOUBLE_ENDED_ITERATOR_LAST, USELESS_NONZERO_NEW_UNCHECKED, MANUAL_REPEAT_N, - SLICE_AS_BYTES, + SLICED_STRING_AS_BYTES, ]); /// Extracts a method call name, args, and `Span` of the method name. @@ -4828,7 +4828,7 @@ impl Methods { if let Some(("as_str", recv, [], as_str_span, _)) = method_call(recv) { redundant_as_str::check(cx, expr, recv, as_str_span, span); } - slice_as_bytes::check(cx, expr, recv); + sliced_string_as_bytes::check(cx, expr, recv); }, ("as_mut", []) => useless_asref::check(cx, expr, "as_mut", recv), ("as_ptr", []) => manual_c_str_literals::check_as_ptr(cx, expr, recv, &self.msrv), diff --git a/clippy_lints/src/methods/slice_as_bytes.rs b/clippy_lints/src/methods/sliced_string_as_bytes.rs similarity index 93% rename from clippy_lints/src/methods/slice_as_bytes.rs rename to clippy_lints/src/methods/sliced_string_as_bytes.rs index 4040008c98ac..43aab8a452e0 100644 --- a/clippy_lints/src/methods/slice_as_bytes.rs +++ b/clippy_lints/src/methods/sliced_string_as_bytes.rs @@ -5,7 +5,7 @@ use rustc_errors::Applicability; use rustc_hir::{Expr, ExprKind, LangItem, is_range_literal}; use rustc_lint::LateContext; -use super::SLICE_AS_BYTES; +use super::SLICED_STRING_AS_BYTES; pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>) { if let ExprKind::Index(indexed, index, _) = recv.kind @@ -18,7 +18,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>) { let range = snippet_with_applicability(cx, index.span, "..", &mut applicability); span_lint_and_sugg( cx, - SLICE_AS_BYTES, + SLICED_STRING_AS_BYTES, expr.span, "calling `as_bytes` after slicing a string", "try", diff --git a/tests/ui/bytes_nth.fixed b/tests/ui/bytes_nth.fixed index d58eb5227fbf..da35fcb55e5a 100644 --- a/tests/ui/bytes_nth.fixed +++ b/tests/ui/bytes_nth.fixed @@ -1,5 +1,5 @@ #![allow(clippy::unnecessary_operation)] -#![allow(clippy::slice_as_bytes)] +#![allow(clippy::sliced_string_as_bytes)] #![warn(clippy::bytes_nth)] fn main() { diff --git a/tests/ui/bytes_nth.rs b/tests/ui/bytes_nth.rs index bbfe388e8bb1..5dbe84ecec8b 100644 --- a/tests/ui/bytes_nth.rs +++ b/tests/ui/bytes_nth.rs @@ -1,5 +1,5 @@ #![allow(clippy::unnecessary_operation)] -#![allow(clippy::slice_as_bytes)] +#![allow(clippy::sliced_string_as_bytes)] #![warn(clippy::bytes_nth)] fn main() { diff --git a/tests/ui/slice_as_bytes.fixed b/tests/ui/sliced_string_as_bytes.fixed similarity index 93% rename from tests/ui/slice_as_bytes.fixed rename to tests/ui/sliced_string_as_bytes.fixed index da627717a529..469ad27a99b9 100644 --- a/tests/ui/slice_as_bytes.fixed +++ b/tests/ui/sliced_string_as_bytes.fixed @@ -1,5 +1,5 @@ #![allow(unused)] -#![warn(clippy::slice_as_bytes)] +#![warn(clippy::sliced_string_as_bytes)] use std::ops::{Index, Range}; diff --git a/tests/ui/slice_as_bytes.rs b/tests/ui/sliced_string_as_bytes.rs similarity index 93% rename from tests/ui/slice_as_bytes.rs rename to tests/ui/sliced_string_as_bytes.rs index 095b7773810b..4a4605e5a1ae 100644 --- a/tests/ui/slice_as_bytes.rs +++ b/tests/ui/sliced_string_as_bytes.rs @@ -1,5 +1,5 @@ #![allow(unused)] -#![warn(clippy::slice_as_bytes)] +#![warn(clippy::sliced_string_as_bytes)] use std::ops::{Index, Range}; diff --git a/tests/ui/slice_as_bytes.stderr b/tests/ui/sliced_string_as_bytes.stderr similarity index 75% rename from tests/ui/slice_as_bytes.stderr rename to tests/ui/sliced_string_as_bytes.stderr index 9a787b0b9173..47c928d1c1e8 100644 --- a/tests/ui/slice_as_bytes.stderr +++ b/tests/ui/sliced_string_as_bytes.stderr @@ -1,20 +1,20 @@ error: calling `as_bytes` after slicing a string - --> tests/ui/slice_as_bytes.rs:28:17 + --> tests/ui/sliced_string_as_bytes.rs:28:17 | LL | let bytes = s[1..5].as_bytes(); | ^^^^^^^^^^^^^^^^^^ help: try: `&s.as_bytes()[1..5]` | = note: `-D clippy::slice-as-bytes` implied by `-D warnings` - = help: to override `-D warnings` add `#[allow(clippy::slice_as_bytes)]` + = help: to override `-D warnings` add `#[allow(clippy::sliced_string_as_bytes)]` error: calling `as_bytes` after slicing a string - --> tests/ui/slice_as_bytes.rs:29:17 + --> tests/ui/sliced_string_as_bytes.rs:29:17 | LL | let bytes = string[1..].as_bytes(); | ^^^^^^^^^^^^^^^^^^^^^^ help: try: `&string.as_bytes()[1..]` error: calling `as_bytes` after slicing a string - --> tests/ui/slice_as_bytes.rs:30:17 + --> tests/ui/sliced_string_as_bytes.rs:30:17 | LL | let bytes = "consectetur adipiscing"[..=5].as_bytes(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&"consectetur adipiscing".as_bytes()[..=5]` From 4fef1b46bea9a56d4170b1321787ee20e7406366 Mon Sep 17 00:00:00 2001 From: wowinter13 Date: Wed, 15 Jan 2025 22:46:18 +0100 Subject: [PATCH 09/12] Fix tests --- tests/ui/sliced_string_as_bytes.stderr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ui/sliced_string_as_bytes.stderr b/tests/ui/sliced_string_as_bytes.stderr index 47c928d1c1e8..1342f4c01a48 100644 --- a/tests/ui/sliced_string_as_bytes.stderr +++ b/tests/ui/sliced_string_as_bytes.stderr @@ -4,7 +4,7 @@ error: calling `as_bytes` after slicing a string LL | let bytes = s[1..5].as_bytes(); | ^^^^^^^^^^^^^^^^^^ help: try: `&s.as_bytes()[1..5]` | - = note: `-D clippy::slice-as-bytes` implied by `-D warnings` + = note: `-D clippy::sliced-string-as-bytes` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::sliced_string_as_bytes)]` error: calling `as_bytes` after slicing a string From 5855e0a2f1df8a939342b16c0e7337cd393527d7 Mon Sep 17 00:00:00 2001 From: wowinter13 Date: Thu, 16 Jan 2025 01:42:41 +0100 Subject: [PATCH 10/12] Small refactoring: irrefutable let pattern --- .../src/methods/sliced_string_as_bytes.rs | 29 +++++++++---------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/clippy_lints/src/methods/sliced_string_as_bytes.rs b/clippy_lints/src/methods/sliced_string_as_bytes.rs index 43aab8a452e0..eb080ebe21d3 100644 --- a/clippy_lints/src/methods/sliced_string_as_bytes.rs +++ b/clippy_lints/src/methods/sliced_string_as_bytes.rs @@ -10,21 +10,20 @@ use super::SLICED_STRING_AS_BYTES; pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>) { if let ExprKind::Index(indexed, index, _) = recv.kind && is_range_literal(index) + && let ty = cx.typeck_results().expr_ty(indexed).peel_refs() + && (ty.is_str() || is_type_lang_item(cx, ty, LangItem::String)) { - let ty = cx.typeck_results().expr_ty(indexed).peel_refs(); - if ty.is_str() || is_type_lang_item(cx, ty, LangItem::String) { - let mut applicability = Applicability::MaybeIncorrect; - let stringish = snippet_with_applicability(cx, indexed.span, "..", &mut applicability); - let range = snippet_with_applicability(cx, index.span, "..", &mut applicability); - span_lint_and_sugg( - cx, - SLICED_STRING_AS_BYTES, - expr.span, - "calling `as_bytes` after slicing a string", - "try", - format!("&{stringish}.as_bytes()[{range}]"), - applicability, - ); - } + let mut applicability = Applicability::MaybeIncorrect; + let stringish = snippet_with_applicability(cx, indexed.span, "..", &mut applicability); + let range = snippet_with_applicability(cx, index.span, "..", &mut applicability); + span_lint_and_sugg( + cx, + SLICED_STRING_AS_BYTES, + expr.span, + "calling `as_bytes` after slicing a string", + "try", + format!("&{stringish}.as_bytes()[{range}]"), + applicability, + ); } } From 4e94d222912c2a36966ae845e8fd54e073896266 Mon Sep 17 00:00:00 2001 From: wowinter13 Date: Thu, 16 Jan 2025 16:28:59 +0100 Subject: [PATCH 11/12] nit: change placeholders --- clippy_lints/src/methods/sliced_string_as_bytes.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/methods/sliced_string_as_bytes.rs b/clippy_lints/src/methods/sliced_string_as_bytes.rs index eb080ebe21d3..6d4cfdb34f31 100644 --- a/clippy_lints/src/methods/sliced_string_as_bytes.rs +++ b/clippy_lints/src/methods/sliced_string_as_bytes.rs @@ -14,8 +14,8 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>) { && (ty.is_str() || is_type_lang_item(cx, ty, LangItem::String)) { let mut applicability = Applicability::MaybeIncorrect; - let stringish = snippet_with_applicability(cx, indexed.span, "..", &mut applicability); - let range = snippet_with_applicability(cx, index.span, "..", &mut applicability); + let stringish = snippet_with_applicability(cx, indexed.span, "_", &mut applicability); + let range = snippet_with_applicability(cx, index.span, "_", &mut applicability); span_lint_and_sugg( cx, SLICED_STRING_AS_BYTES, From beeb6f7432a2274895dfbe4736be75bc21db827a Mon Sep 17 00:00:00 2001 From: wowinter13 Date: Sat, 25 Jan 2025 18:38:47 +0100 Subject: [PATCH 12/12] slice-as-bytes: pedantic -> perf --- clippy_lints/src/methods/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index 3953fe03f314..ceb21f627c97 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -4388,7 +4388,7 @@ declare_clippy_lint! { /// ``` #[clippy::version = "1.86.0"] pub SLICED_STRING_AS_BYTES, - pedantic, + perf, "slicing a string and immediately calling as_bytes is less efficient and can lead to panics" }