Skip to content

Commit 92ba075

Browse files
committed
Auto merge of #6134 - patrickelectric:as_utf8, r=llogiq
Check when `from_utf8` is called from sliced byte array from string --- *Please keep the line below* changelog: Fix #5487: Add linter to check when `from_utf8` is called from sliced byte array from string.
2 parents 694cec1 + bc27d14 commit 92ba075

File tree

8 files changed

+99
-3
lines changed

8 files changed

+99
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1956,6 +1956,7 @@ Released 2018-09-13
19561956
[`string_add`]: https://rust-lang.github.io/rust-clippy/master/index.html#string_add
19571957
[`string_add_assign`]: https://rust-lang.github.io/rust-clippy/master/index.html#string_add_assign
19581958
[`string_extend_chars`]: https://rust-lang.github.io/rust-clippy/master/index.html#string_extend_chars
1959+
[`string_from_utf8_as_bytes`]: https://rust-lang.github.io/rust-clippy/master/index.html#string_from_utf8_as_bytes
19591960
[`string_lit_as_bytes`]: https://rust-lang.github.io/rust-clippy/master/index.html#string_lit_as_bytes
19601961
[`string_to_string`]: https://rust-lang.github.io/rust-clippy/master/index.html#string_to_string
19611962
[`struct_excessive_bools`]: https://rust-lang.github.io/rust-clippy/master/index.html#struct_excessive_bools

clippy_lints/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
832832
&stable_sort_primitive::STABLE_SORT_PRIMITIVE,
833833
&strings::STRING_ADD,
834834
&strings::STRING_ADD_ASSIGN,
835+
&strings::STRING_FROM_UTF8_AS_BYTES,
835836
&strings::STRING_LIT_AS_BYTES,
836837
&suspicious_trait_impl::SUSPICIOUS_ARITHMETIC_IMPL,
837838
&suspicious_trait_impl::SUSPICIOUS_OP_ASSIGN_IMPL,
@@ -1527,6 +1528,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
15271528
LintId::of(&single_component_path_imports::SINGLE_COMPONENT_PATH_IMPORTS),
15281529
LintId::of(&slow_vector_initialization::SLOW_VECTOR_INITIALIZATION),
15291530
LintId::of(&stable_sort_primitive::STABLE_SORT_PRIMITIVE),
1531+
LintId::of(&strings::STRING_FROM_UTF8_AS_BYTES),
15301532
LintId::of(&suspicious_trait_impl::SUSPICIOUS_ARITHMETIC_IMPL),
15311533
LintId::of(&suspicious_trait_impl::SUSPICIOUS_OP_ASSIGN_IMPL),
15321534
LintId::of(&swap::ALMOST_SWAPPED),
@@ -1752,6 +1754,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
17521754
LintId::of(&reference::DEREF_ADDROF),
17531755
LintId::of(&reference::REF_IN_DEREF),
17541756
LintId::of(&repeat_once::REPEAT_ONCE),
1757+
LintId::of(&strings::STRING_FROM_UTF8_AS_BYTES),
17551758
LintId::of(&swap::MANUAL_SWAP),
17561759
LintId::of(&temporary_assignment::TEMPORARY_ASSIGNMENT),
17571760
LintId::of(&transmute::CROSSPOINTER_TRANSMUTE),

clippy_lints/src/strings.rs

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rustc_errors::Applicability;
2-
use rustc_hir::{BinOpKind, Expr, ExprKind};
2+
use rustc_hir::{BinOpKind, BorrowKind, Expr, ExprKind, LangItem, QPath};
33
use rustc_lint::{LateContext, LateLintPass, LintContext};
44
use rustc_middle::lint::in_external_macro;
55
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -9,7 +9,10 @@ use rustc_span::sym;
99
use if_chain::if_chain;
1010

1111
use crate::utils::SpanlessEq;
12-
use crate::utils::{get_parent_expr, is_allowed, is_type_diagnostic_item, span_lint, span_lint_and_sugg};
12+
use crate::utils::{
13+
get_parent_expr, is_allowed, is_type_diagnostic_item, match_function_call, method_calls, paths, span_lint,
14+
span_lint_and_sugg,
15+
};
1316

1417
declare_clippy_lint! {
1518
/// **What it does:** Checks for string appends of the form `x = x + y` (without
@@ -174,16 +177,75 @@ fn is_add(cx: &LateContext<'_>, src: &Expr<'_>, target: &Expr<'_>) -> bool {
174177
}
175178
}
176179

180+
declare_clippy_lint! {
181+
/// **What it does:** Check if the string is transformed to byte array and casted back to string.
182+
///
183+
/// **Why is this bad?** It's unnecessary, the string can be used directly.
184+
///
185+
/// **Known problems:** None
186+
///
187+
/// **Example:**
188+
/// ```rust
189+
/// let _ = std::str::from_utf8(&"Hello World!".as_bytes()[6..11]).unwrap();
190+
/// ```
191+
/// could be written as
192+
/// ```rust
193+
/// let _ = &"Hello World!"[6..11];
194+
/// ```
195+
pub STRING_FROM_UTF8_AS_BYTES,
196+
complexity,
197+
"casting string slices to byte slices and back"
198+
}
199+
177200
// Max length a b"foo" string can take
178201
const MAX_LENGTH_BYTE_STRING_LIT: usize = 32;
179202

180-
declare_lint_pass!(StringLitAsBytes => [STRING_LIT_AS_BYTES]);
203+
declare_lint_pass!(StringLitAsBytes => [STRING_LIT_AS_BYTES, STRING_FROM_UTF8_AS_BYTES]);
181204

182205
impl<'tcx> LateLintPass<'tcx> for StringLitAsBytes {
183206
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
184207
use crate::utils::{snippet, snippet_with_applicability};
185208
use rustc_ast::LitKind;
186209

210+
if_chain! {
211+
// Find std::str::converts::from_utf8
212+
if let Some(args) = match_function_call(cx, e, &paths::STR_FROM_UTF8);
213+
214+
// Find string::as_bytes
215+
if let ExprKind::AddrOf(BorrowKind::Ref, _, ref args) = args[0].kind;
216+
if let ExprKind::Index(ref left, ref right) = args.kind;
217+
let (method_names, expressions, _) = method_calls(left, 1);
218+
if method_names.len() == 1;
219+
if expressions.len() == 1;
220+
if expressions[0].len() == 1;
221+
if method_names[0] == sym!(as_bytes);
222+
223+
// Check for slicer
224+
if let ExprKind::Struct(ref path, _, _) = right.kind;
225+
if let QPath::LangItem(LangItem::Range, _) = path;
226+
227+
then {
228+
let mut applicability = Applicability::MachineApplicable;
229+
let string_expression = &expressions[0][0];
230+
231+
let snippet_app = snippet_with_applicability(
232+
cx,
233+
string_expression.span, "..",
234+
&mut applicability,
235+
);
236+
237+
span_lint_and_sugg(
238+
cx,
239+
STRING_FROM_UTF8_AS_BYTES,
240+
e.span,
241+
"calling a slice of `as_bytes()` with `from_utf8` should be not necessary",
242+
"try",
243+
format!("Some(&{}[{}])", snippet_app, snippet(cx, right.span, "..")),
244+
applicability
245+
)
246+
}
247+
}
248+
187249
if_chain! {
188250
if let ExprKind::MethodCall(path, _, args, _) = &e.kind;
189251
if path.ident.name == sym!(as_bytes);

clippy_lints/src/utils/paths.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ pub const STRING: [&str; 3] = ["alloc", "string", "String"];
122122
pub const STRING_AS_MUT_STR: [&str; 4] = ["alloc", "string", "String", "as_mut_str"];
123123
pub const STRING_AS_STR: [&str; 4] = ["alloc", "string", "String", "as_str"];
124124
pub const STR_ENDS_WITH: [&str; 4] = ["core", "str", "<impl str>", "ends_with"];
125+
pub const STR_FROM_UTF8: [&str; 4] = ["core", "str", "converts", "from_utf8"];
125126
pub const STR_LEN: [&str; 4] = ["core", "str", "<impl str>", "len"];
126127
pub const STR_STARTS_WITH: [&str; 4] = ["core", "str", "<impl str>", "starts_with"];
127128
pub const SYNTAX_CONTEXT: [&str; 3] = ["rustc_span", "hygiene", "SyntaxContext"];

src/lintlist/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2258,6 +2258,13 @@ vec![
22582258
deprecation: None,
22592259
module: "methods",
22602260
},
2261+
Lint {
2262+
name: "string_from_utf8_as_bytes",
2263+
group: "complexity",
2264+
desc: "casting string slices to byte slices and back",
2265+
deprecation: None,
2266+
module: "strings",
2267+
},
22612268
Lint {
22622269
name: "string_lit_as_bytes",
22632270
group: "nursery",
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// run-rustfix
2+
#![warn(clippy::string_from_utf8_as_bytes)]
3+
4+
fn main() {
5+
let _ = Some(&"Hello World!"[6..11]);
6+
}

tests/ui/string_from_utf8_as_bytes.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// run-rustfix
2+
#![warn(clippy::string_from_utf8_as_bytes)]
3+
4+
fn main() {
5+
let _ = std::str::from_utf8(&"Hello World!".as_bytes()[6..11]);
6+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: calling a slice of `as_bytes()` with `from_utf8` should be not necessary
2+
--> $DIR/string_from_utf8_as_bytes.rs:5:13
3+
|
4+
LL | let _ = std::str::from_utf8(&"Hello World!".as_bytes()[6..11]);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Some(&"Hello World!"[6..11])`
6+
|
7+
= note: `-D clippy::string-from-utf8-as-bytes` implied by `-D warnings`
8+
9+
error: aborting due to previous error
10+

0 commit comments

Comments
 (0)