mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-13 00:17:13 +00:00
Fixes #8128
changelog: Fix error suggestion of skip(..).next() for immutable variable.
This commit is contained in:
parent
aa3648af50
commit
4ffd66074a
6 changed files with 138 additions and 10 deletions
|
@ -1,8 +1,10 @@
|
||||||
use clippy_utils::diagnostics::span_lint_and_sugg;
|
use clippy_utils::diagnostics::span_lint_and_then;
|
||||||
use clippy_utils::is_trait_method;
|
use clippy_utils::is_trait_method;
|
||||||
|
use clippy_utils::path_to_local;
|
||||||
use clippy_utils::source::snippet;
|
use clippy_utils::source::snippet;
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
use rustc_hir as hir;
|
use rustc_hir as hir;
|
||||||
|
use rustc_hir::{BindingAnnotation, Node, PatKind};
|
||||||
use rustc_lint::LateContext;
|
use rustc_lint::LateContext;
|
||||||
use rustc_span::sym;
|
use rustc_span::sym;
|
||||||
|
|
||||||
|
@ -11,14 +13,34 @@ use super::ITER_SKIP_NEXT;
|
||||||
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>, arg: &hir::Expr<'_>) {
|
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>, arg: &hir::Expr<'_>) {
|
||||||
// lint if caller of skip is an Iterator
|
// lint if caller of skip is an Iterator
|
||||||
if is_trait_method(cx, expr, sym::Iterator) {
|
if is_trait_method(cx, expr, sym::Iterator) {
|
||||||
span_lint_and_sugg(
|
let mut application = Applicability::MachineApplicable;
|
||||||
|
span_lint_and_then(
|
||||||
cx,
|
cx,
|
||||||
ITER_SKIP_NEXT,
|
ITER_SKIP_NEXT,
|
||||||
expr.span.trim_start(recv.span).unwrap(),
|
expr.span.trim_start(recv.span).unwrap(),
|
||||||
"called `skip(..).next()` on an iterator",
|
"called `skip(..).next()` on an iterator",
|
||||||
"use `nth` instead",
|
|diag| {
|
||||||
format!(".nth({})", snippet(cx, arg.span, "..")),
|
if_chain! {
|
||||||
Applicability::MachineApplicable,
|
if let Some(id) = path_to_local(recv);
|
||||||
|
if let Node::Binding(pat) = cx.tcx.hir().get(id);
|
||||||
|
if let PatKind::Binding(ann, _, _, _) = pat.kind;
|
||||||
|
if ann != BindingAnnotation::Mutable;
|
||||||
|
then {
|
||||||
|
application = Applicability::Unspecified;
|
||||||
|
diag.span_help(
|
||||||
|
pat.span,
|
||||||
|
&format!("for this change `{}` has to be mutable", snippet(cx, pat.span, "..")),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
diag.span_suggestion(
|
||||||
|
expr.span.trim_start(recv.span).unwrap(),
|
||||||
|
"use `nth` instead",
|
||||||
|
format!(".nth({})", snippet(cx, arg.span, "..")),
|
||||||
|
application,
|
||||||
|
);
|
||||||
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
#![warn(clippy::iter_skip_next)]
|
#![warn(clippy::iter_skip_next)]
|
||||||
#![allow(clippy::blacklisted_name)]
|
#![allow(clippy::blacklisted_name)]
|
||||||
#![allow(clippy::iter_nth)]
|
#![allow(clippy::iter_nth)]
|
||||||
|
#![allow(unused_mut, dead_code)]
|
||||||
|
|
||||||
extern crate option_helpers;
|
extern crate option_helpers;
|
||||||
|
|
||||||
|
@ -19,4 +20,18 @@ fn main() {
|
||||||
let foo = IteratorFalsePositives { foo: 0 };
|
let foo = IteratorFalsePositives { foo: 0 };
|
||||||
let _ = foo.skip(42).next();
|
let _ = foo.skip(42).next();
|
||||||
let _ = foo.filter().skip(42).next();
|
let _ = foo.filter().skip(42).next();
|
||||||
|
|
||||||
|
// fix #8128
|
||||||
|
let test_string = "1|1 2";
|
||||||
|
let mut sp = test_string.split('|').map(|s| s.trim());
|
||||||
|
let _: Vec<&str> = sp.nth(1).unwrap().split(' ').collect();
|
||||||
|
if let Some(mut s) = Some(test_string.split('|').map(|s| s.trim())) {
|
||||||
|
let _: Vec<&str> = s.nth(1).unwrap().split(' ').collect();
|
||||||
|
};
|
||||||
|
fn check<T>(mut s: T)
|
||||||
|
where
|
||||||
|
T: Iterator<Item = String>,
|
||||||
|
{
|
||||||
|
let _: Vec<&str> = s.nth(1).unwrap().split(' ').collect();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
#![warn(clippy::iter_skip_next)]
|
#![warn(clippy::iter_skip_next)]
|
||||||
#![allow(clippy::blacklisted_name)]
|
#![allow(clippy::blacklisted_name)]
|
||||||
#![allow(clippy::iter_nth)]
|
#![allow(clippy::iter_nth)]
|
||||||
|
#![allow(unused_mut, dead_code)]
|
||||||
|
|
||||||
extern crate option_helpers;
|
extern crate option_helpers;
|
||||||
|
|
||||||
|
@ -19,4 +20,18 @@ fn main() {
|
||||||
let foo = IteratorFalsePositives { foo: 0 };
|
let foo = IteratorFalsePositives { foo: 0 };
|
||||||
let _ = foo.skip(42).next();
|
let _ = foo.skip(42).next();
|
||||||
let _ = foo.filter().skip(42).next();
|
let _ = foo.filter().skip(42).next();
|
||||||
|
|
||||||
|
// fix #8128
|
||||||
|
let test_string = "1|1 2";
|
||||||
|
let mut sp = test_string.split('|').map(|s| s.trim());
|
||||||
|
let _: Vec<&str> = sp.skip(1).next().unwrap().split(' ').collect();
|
||||||
|
if let Some(mut s) = Some(test_string.split('|').map(|s| s.trim())) {
|
||||||
|
let _: Vec<&str> = s.skip(1).next().unwrap().split(' ').collect();
|
||||||
|
};
|
||||||
|
fn check<T>(mut s: T)
|
||||||
|
where
|
||||||
|
T: Iterator<Item = String>,
|
||||||
|
{
|
||||||
|
let _: Vec<&str> = s.skip(1).next().unwrap().split(' ').collect();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
error: called `skip(..).next()` on an iterator
|
error: called `skip(..).next()` on an iterator
|
||||||
--> $DIR/iter_skip_next.rs:15:28
|
--> $DIR/iter_skip_next.rs:16:28
|
||||||
|
|
|
|
||||||
LL | let _ = some_vec.iter().skip(42).next();
|
LL | let _ = some_vec.iter().skip(42).next();
|
||||||
| ^^^^^^^^^^^^^^^^ help: use `nth` instead: `.nth(42)`
|
| ^^^^^^^^^^^^^^^^ help: use `nth` instead: `.nth(42)`
|
||||||
|
@ -7,22 +7,40 @@ LL | let _ = some_vec.iter().skip(42).next();
|
||||||
= note: `-D clippy::iter-skip-next` implied by `-D warnings`
|
= note: `-D clippy::iter-skip-next` implied by `-D warnings`
|
||||||
|
|
||||||
error: called `skip(..).next()` on an iterator
|
error: called `skip(..).next()` on an iterator
|
||||||
--> $DIR/iter_skip_next.rs:16:36
|
--> $DIR/iter_skip_next.rs:17:36
|
||||||
|
|
|
|
||||||
LL | let _ = some_vec.iter().cycle().skip(42).next();
|
LL | let _ = some_vec.iter().cycle().skip(42).next();
|
||||||
| ^^^^^^^^^^^^^^^^ help: use `nth` instead: `.nth(42)`
|
| ^^^^^^^^^^^^^^^^ help: use `nth` instead: `.nth(42)`
|
||||||
|
|
||||||
error: called `skip(..).next()` on an iterator
|
error: called `skip(..).next()` on an iterator
|
||||||
--> $DIR/iter_skip_next.rs:17:20
|
--> $DIR/iter_skip_next.rs:18:20
|
||||||
|
|
|
|
||||||
LL | let _ = (1..10).skip(10).next();
|
LL | let _ = (1..10).skip(10).next();
|
||||||
| ^^^^^^^^^^^^^^^^ help: use `nth` instead: `.nth(10)`
|
| ^^^^^^^^^^^^^^^^ help: use `nth` instead: `.nth(10)`
|
||||||
|
|
||||||
error: called `skip(..).next()` on an iterator
|
error: called `skip(..).next()` on an iterator
|
||||||
--> $DIR/iter_skip_next.rs:18:33
|
--> $DIR/iter_skip_next.rs:19:33
|
||||||
|
|
|
|
||||||
LL | let _ = &some_vec[..].iter().skip(3).next();
|
LL | let _ = &some_vec[..].iter().skip(3).next();
|
||||||
| ^^^^^^^^^^^^^^^ help: use `nth` instead: `.nth(3)`
|
| ^^^^^^^^^^^^^^^ help: use `nth` instead: `.nth(3)`
|
||||||
|
|
||||||
error: aborting due to 4 previous errors
|
error: called `skip(..).next()` on an iterator
|
||||||
|
--> $DIR/iter_skip_next.rs:27:26
|
||||||
|
|
|
||||||
|
LL | let _: Vec<&str> = sp.skip(1).next().unwrap().split(' ').collect();
|
||||||
|
| ^^^^^^^^^^^^^^^ help: use `nth` instead: `.nth(1)`
|
||||||
|
|
||||||
|
error: called `skip(..).next()` on an iterator
|
||||||
|
--> $DIR/iter_skip_next.rs:29:29
|
||||||
|
|
|
||||||
|
LL | let _: Vec<&str> = s.skip(1).next().unwrap().split(' ').collect();
|
||||||
|
| ^^^^^^^^^^^^^^^ help: use `nth` instead: `.nth(1)`
|
||||||
|
|
||||||
|
error: called `skip(..).next()` on an iterator
|
||||||
|
--> $DIR/iter_skip_next.rs:35:29
|
||||||
|
|
|
||||||
|
LL | let _: Vec<&str> = s.skip(1).next().unwrap().split(' ').collect();
|
||||||
|
| ^^^^^^^^^^^^^^^ help: use `nth` instead: `.nth(1)`
|
||||||
|
|
||||||
|
error: aborting due to 7 previous errors
|
||||||
|
|
||||||
|
|
19
tests/ui/iter_skip_next_unfixable.rs
Normal file
19
tests/ui/iter_skip_next_unfixable.rs
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
#![warn(clippy::iter_skip_next)]
|
||||||
|
#![allow(dead_code)]
|
||||||
|
|
||||||
|
/// Checks implementation of `ITER_SKIP_NEXT` lint
|
||||||
|
fn main() {
|
||||||
|
// fix #8128
|
||||||
|
let test_string = "1|1 2";
|
||||||
|
let sp = test_string.split('|').map(|s| s.trim());
|
||||||
|
let _: Vec<&str> = sp.skip(1).next().unwrap().split(' ').collect();
|
||||||
|
if let Some(s) = Some(test_string.split('|').map(|s| s.trim())) {
|
||||||
|
let _: Vec<&str> = s.skip(1).next().unwrap().split(' ').collect();
|
||||||
|
};
|
||||||
|
fn check<T>(s: T)
|
||||||
|
where
|
||||||
|
T: Iterator<Item = String>,
|
||||||
|
{
|
||||||
|
let _: Vec<&str> = s.skip(1).next().unwrap().split(' ').collect();
|
||||||
|
}
|
||||||
|
}
|
39
tests/ui/iter_skip_next_unfixable.stderr
Normal file
39
tests/ui/iter_skip_next_unfixable.stderr
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
error: called `skip(..).next()` on an iterator
|
||||||
|
--> $DIR/iter_skip_next_unfixable.rs:9:26
|
||||||
|
|
|
||||||
|
LL | let _: Vec<&str> = sp.skip(1).next().unwrap().split(' ').collect();
|
||||||
|
| ^^^^^^^^^^^^^^^ help: use `nth` instead: `.nth(1)`
|
||||||
|
|
|
||||||
|
= note: `-D clippy::iter-skip-next` implied by `-D warnings`
|
||||||
|
help: for this change `sp` has to be mutable
|
||||||
|
--> $DIR/iter_skip_next_unfixable.rs:8:9
|
||||||
|
|
|
||||||
|
LL | let sp = test_string.split('|').map(|s| s.trim());
|
||||||
|
| ^^
|
||||||
|
|
||||||
|
error: called `skip(..).next()` on an iterator
|
||||||
|
--> $DIR/iter_skip_next_unfixable.rs:11:29
|
||||||
|
|
|
||||||
|
LL | let _: Vec<&str> = s.skip(1).next().unwrap().split(' ').collect();
|
||||||
|
| ^^^^^^^^^^^^^^^ help: use `nth` instead: `.nth(1)`
|
||||||
|
|
|
||||||
|
help: for this change `s` has to be mutable
|
||||||
|
--> $DIR/iter_skip_next_unfixable.rs:10:17
|
||||||
|
|
|
||||||
|
LL | if let Some(s) = Some(test_string.split('|').map(|s| s.trim())) {
|
||||||
|
| ^
|
||||||
|
|
||||||
|
error: called `skip(..).next()` on an iterator
|
||||||
|
--> $DIR/iter_skip_next_unfixable.rs:17:29
|
||||||
|
|
|
||||||
|
LL | let _: Vec<&str> = s.skip(1).next().unwrap().split(' ').collect();
|
||||||
|
| ^^^^^^^^^^^^^^^ help: use `nth` instead: `.nth(1)`
|
||||||
|
|
|
||||||
|
help: for this change `s` has to be mutable
|
||||||
|
--> $DIR/iter_skip_next_unfixable.rs:13:17
|
||||||
|
|
|
||||||
|
LL | fn check<T>(s: T)
|
||||||
|
| ^
|
||||||
|
|
||||||
|
error: aborting due to 3 previous errors
|
||||||
|
|
Loading…
Reference in a new issue