mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-27 23:20:39 +00:00
Auto merge of #10361 - nindalf:nth_bytes_copy, r=llogiq
Stop bytes_nth from suggesting code that does not compile Fixes #10151 As discussed in the issue, this PR changes the lint in 2 ways 1. Replace `bytes().nth(n).unwrap()` with `as_bytes()[n]` 2. Replace other `bytes().nth(n)` with `as_bytes().get(n).copied()` --- changelog: Stop bytes_nth from suggesting code that does not compile in some cases
This commit is contained in:
commit
eac0bd9da3
4 changed files with 38 additions and 22 deletions
|
@ -5,6 +5,8 @@ use rustc_errors::Applicability;
|
||||||
use rustc_hir::{Expr, LangItem};
|
use rustc_hir::{Expr, LangItem};
|
||||||
use rustc_lint::LateContext;
|
use rustc_lint::LateContext;
|
||||||
|
|
||||||
|
use crate::methods::method_call;
|
||||||
|
|
||||||
use super::BYTES_NTH;
|
use super::BYTES_NTH;
|
||||||
|
|
||||||
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, recv: &'tcx Expr<'tcx>, n_arg: &'tcx Expr<'tcx>) {
|
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, recv: &'tcx Expr<'tcx>, n_arg: &'tcx Expr<'tcx>) {
|
||||||
|
@ -16,18 +18,32 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, recv: &'tcx E
|
||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut applicability = Applicability::MachineApplicable;
|
let mut applicability = Applicability::MachineApplicable;
|
||||||
|
let receiver = snippet_with_applicability(cx, recv.span, "..", &mut applicability);
|
||||||
|
let n = snippet_with_applicability(cx, n_arg.span, "..", &mut applicability);
|
||||||
|
|
||||||
|
if let Some(parent) = clippy_utils::get_parent_expr(cx, expr)
|
||||||
|
&& let Some((name, _, _, _, _)) = method_call(parent)
|
||||||
|
&& name == "unwrap" {
|
||||||
|
span_lint_and_sugg(
|
||||||
|
cx,
|
||||||
|
BYTES_NTH,
|
||||||
|
parent.span,
|
||||||
|
&format!("called `.bytes().nth().unwrap()` on a `{caller_type}`"),
|
||||||
|
"try",
|
||||||
|
format!("{receiver}.as_bytes()[{n}]",),
|
||||||
|
applicability
|
||||||
|
);
|
||||||
|
} else {
|
||||||
span_lint_and_sugg(
|
span_lint_and_sugg(
|
||||||
cx,
|
cx,
|
||||||
BYTES_NTH,
|
BYTES_NTH,
|
||||||
expr.span,
|
expr.span,
|
||||||
&format!("called `.bytes().nth()` on a `{caller_type}`"),
|
&format!("called `.bytes().nth()` on a `{caller_type}`"),
|
||||||
"try",
|
"try",
|
||||||
format!(
|
format!("{receiver}.as_bytes().get({n}).copied()"),
|
||||||
"{}.as_bytes().get({})",
|
applicability
|
||||||
snippet_with_applicability(cx, recv.span, "..", &mut applicability),
|
|
||||||
snippet_with_applicability(cx, n_arg.span, "..", &mut applicability)
|
|
||||||
),
|
|
||||||
applicability,
|
|
||||||
);
|
);
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let s = String::from("String");
|
let s = String::from("String");
|
||||||
let _ = s.as_bytes().get(3);
|
let _ = s.as_bytes().get(3).copied();
|
||||||
let _ = &s.as_bytes().get(3);
|
let _ = &s.as_bytes()[3];
|
||||||
let _ = s[..].as_bytes().get(3);
|
let _ = s[..].as_bytes().get(3).copied();
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,6 @@
|
||||||
fn main() {
|
fn main() {
|
||||||
let s = String::from("String");
|
let s = String::from("String");
|
||||||
let _ = s.bytes().nth(3);
|
let _ = s.bytes().nth(3);
|
||||||
let _ = &s.bytes().nth(3);
|
let _ = &s.bytes().nth(3).unwrap();
|
||||||
let _ = s[..].bytes().nth(3);
|
let _ = s[..].bytes().nth(3);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,21 +2,21 @@ error: called `.bytes().nth()` on a `String`
|
||||||
--> $DIR/bytes_nth.rs:8:13
|
--> $DIR/bytes_nth.rs:8:13
|
||||||
|
|
|
|
||||||
LL | let _ = s.bytes().nth(3);
|
LL | let _ = s.bytes().nth(3);
|
||||||
| ^^^^^^^^^^^^^^^^ help: try: `s.as_bytes().get(3)`
|
| ^^^^^^^^^^^^^^^^ help: try: `s.as_bytes().get(3).copied()`
|
||||||
|
|
|
|
||||||
= note: `-D clippy::bytes-nth` implied by `-D warnings`
|
= note: `-D clippy::bytes-nth` implied by `-D warnings`
|
||||||
|
|
||||||
error: called `.bytes().nth()` on a `String`
|
error: called `.bytes().nth().unwrap()` on a `String`
|
||||||
--> $DIR/bytes_nth.rs:9:14
|
--> $DIR/bytes_nth.rs:9:14
|
||||||
|
|
|
|
||||||
LL | let _ = &s.bytes().nth(3);
|
LL | let _ = &s.bytes().nth(3).unwrap();
|
||||||
| ^^^^^^^^^^^^^^^^ help: try: `s.as_bytes().get(3)`
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `s.as_bytes()[3]`
|
||||||
|
|
||||||
error: called `.bytes().nth()` on a `str`
|
error: called `.bytes().nth()` on a `str`
|
||||||
--> $DIR/bytes_nth.rs:10:13
|
--> $DIR/bytes_nth.rs:10:13
|
||||||
|
|
|
|
||||||
LL | let _ = s[..].bytes().nth(3);
|
LL | let _ = s[..].bytes().nth(3);
|
||||||
| ^^^^^^^^^^^^^^^^^^^^ help: try: `s[..].as_bytes().get(3)`
|
| ^^^^^^^^^^^^^^^^^^^^ help: try: `s[..].as_bytes().get(3).copied()`
|
||||||
|
|
||||||
error: aborting due to 3 previous errors
|
error: aborting due to 3 previous errors
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue