-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New lint sliced_string_as_bytes
#14002
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Manishearth
merged 12 commits into
rust-lang:master
from
wowinter13:new-lint-slice-as-bytes
Jan 27, 2025
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d8752db
New lint
wowinter13 c214f51
Update CHANGELOG.md
wowinter13 2ad20da
Rollback CHANGELOG.md change
wowinter13 307d854
Address linter & changelog issues
wowinter13 7124d82
Fix function signature
wowinter13 43066fe
Fix related test
wowinter13 9e83183
uibless for tests
wowinter13 26a7b32
Rename slice_as_bytes -> sliced_string_as_bytes
wowinter13 4fef1b4
Fix tests
wowinter13 5855e0a
Small refactoring: irrefutable let pattern
wowinter13 4e94d22
nit: change placeholders
wowinter13 beeb6f7
slice-as-bytes: pedantic -> perf
wowinter13 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
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::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 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, | ||
); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#![allow(unused)] | ||
#![warn(clippy::sliced_string_as_bytes)] | ||
|
||
use std::ops::{Index, Range}; | ||
|
||
struct Foo; | ||
|
||
struct Bar; | ||
|
||
impl Bar { | ||
fn as_bytes(&self) -> &[u8] { | ||
&[0, 1, 2, 3] | ||
} | ||
} | ||
|
||
impl Index<Range<usize>> for Foo { | ||
type Output = Bar; | ||
|
||
fn index(&self, _: Range<usize>) -> &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(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#![allow(unused)] | ||
#![warn(clippy::sliced_string_as_bytes)] | ||
|
||
use std::ops::{Index, Range}; | ||
|
||
struct Foo; | ||
|
||
struct Bar; | ||
|
||
impl Bar { | ||
fn as_bytes(&self) -> &[u8] { | ||
&[0, 1, 2, 3] | ||
} | ||
} | ||
|
||
impl Index<Range<usize>> for Foo { | ||
type Output = Bar; | ||
|
||
fn index(&self, _: Range<usize>) -> &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(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
error: calling `as_bytes` after slicing a string | ||
--> 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::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 | ||
--> 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/sliced_string_as_bytes.rs:30:17 | ||
| | ||
LL | let bytes = "consectetur adipiscing"[..=5].as_bytes(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&"consectetur adipiscing".as_bytes()[..=5]` | ||
|
||
error: aborting due to 3 previous errors | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.