Stop bytes_nth from suggesting code that does not compile

This commit is contained in:
Krishna Sundarram 2023-02-16 16:41:04 +00:00
parent 52c8b536c9
commit 7e53e27dfd
4 changed files with 38 additions and 22 deletions

View File

@ -5,6 +5,8 @@ use rustc_errors::Applicability;
use rustc_hir::{Expr, LangItem};
use rustc_lint::LateContext;
use crate::methods::method_call;
use super::BYTES_NTH;
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 {
return;
};
let mut applicability = Applicability::MachineApplicable;
span_lint_and_sugg(
cx,
BYTES_NTH,
expr.span,
&format!("called `.bytes().nth()` on a `{caller_type}`"),
"try",
format!(
"{}.as_bytes().get({})",
snippet_with_applicability(cx, recv.span, "..", &mut applicability),
snippet_with_applicability(cx, n_arg.span, "..", &mut applicability)
),
applicability,
);
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(
cx,
BYTES_NTH,
expr.span,
&format!("called `.bytes().nth()` on a `{caller_type}`"),
"try",
format!("{receiver}.as_bytes().get({n}).copied()"),
applicability
);
};
}

View File

@ -5,7 +5,7 @@
fn main() {
let s = String::from("String");
let _ = s.as_bytes().get(3);
let _ = &s.as_bytes().get(3);
let _ = s[..].as_bytes().get(3);
let _ = s.as_bytes().get(3).copied();
let _ = &s.as_bytes()[3];
let _ = s[..].as_bytes().get(3).copied();
}

View File

@ -6,6 +6,6 @@
fn main() {
let s = String::from("String");
let _ = s.bytes().nth(3);
let _ = &s.bytes().nth(3);
let _ = &s.bytes().nth(3).unwrap();
let _ = s[..].bytes().nth(3);
}

View File

@ -2,21 +2,21 @@ error: called `.bytes().nth()` on a `String`
--> $DIR/bytes_nth.rs:8:13
|
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`
error: called `.bytes().nth()` on a `String`
error: called `.bytes().nth().unwrap()` on a `String`
--> $DIR/bytes_nth.rs:9:14
|
LL | let _ = &s.bytes().nth(3);
| ^^^^^^^^^^^^^^^^ help: try: `s.as_bytes().get(3)`
LL | let _ = &s.bytes().nth(3).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `s.as_bytes()[3]`
error: called `.bytes().nth()` on a `str`
--> $DIR/bytes_nth.rs:10:13
|
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