-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
160 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 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 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,39 @@ | ||
use clippy_utils::{diagnostics::span_lint_and_sugg, source::snippet_with_applicability, ty::is_type_lang_item}; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{ | ||
Expr, ExprKind, | ||
LangItem::{self, Range, RangeFrom, RangeFull, RangeTo, RangeToInclusive}, | ||
QPath, | ||
}; | ||
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::Struct(QPath::LangItem(Range | RangeFrom | RangeTo | RangeToInclusive | RangeFull, ..), ..) = | ||
index.kind | ||
{ | ||
let ty = cx.typeck_results().expr_ty(indexed).peel_refs(); | ||
let is_str = ty.is_str(); | ||
let is_string = is_type_lang_item(cx, ty, LangItem::String); | ||
if is_str || is_string { | ||
let mut applicability = Applicability::MachineApplicable; | ||
let stringish = snippet_with_applicability(cx, indexed.span, "..", &mut applicability); | ||
let range = snippet_with_applicability(cx, index.span, "..", &mut applicability); | ||
let type_name = if is_str { "str" } else { "String" }; | ||
span_lint_and_sugg( | ||
cx, | ||
SLICE_AS_BYTES, | ||
expr.span, | ||
&(format!( | ||
"slicing a {type_name} before calling `as_bytes` results in needless UTF-8 alignment checks, and has the possiblity of panicking" | ||
)), | ||
"try", | ||
format!("&{stringish}.as_bytes()[{range}]"), | ||
applicability, | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains 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,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<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 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,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<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 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,22 @@ | ||
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 | ||
|