mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-27 07:00:55 +00:00
Fix ICE in loops
module
This commit is contained in:
parent
2d4c3379d3
commit
888657e09a
6 changed files with 30 additions and 15 deletions
|
@ -2374,7 +2374,7 @@ fn check_needless_collect_direct_usage<'tcx>(expr: &'tcx Expr<'_>, cx: &LateCont
|
||||||
match_type(cx, ty, &paths::BTREEMAP) ||
|
match_type(cx, ty, &paths::BTREEMAP) ||
|
||||||
is_type_diagnostic_item(cx, ty, sym!(hashmap_type)) {
|
is_type_diagnostic_item(cx, ty, sym!(hashmap_type)) {
|
||||||
if method.ident.name == sym!(len) {
|
if method.ident.name == sym!(len) {
|
||||||
let span = shorten_span(expr, sym!(collect));
|
let span = shorten_needless_collect_span(expr);
|
||||||
span_lint_and_sugg(
|
span_lint_and_sugg(
|
||||||
cx,
|
cx,
|
||||||
NEEDLESS_COLLECT,
|
NEEDLESS_COLLECT,
|
||||||
|
@ -2386,20 +2386,20 @@ fn check_needless_collect_direct_usage<'tcx>(expr: &'tcx Expr<'_>, cx: &LateCont
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if method.ident.name == sym!(is_empty) {
|
if method.ident.name == sym!(is_empty) {
|
||||||
let span = shorten_span(expr, sym!(iter));
|
let span = shorten_needless_collect_span(expr);
|
||||||
span_lint_and_sugg(
|
span_lint_and_sugg(
|
||||||
cx,
|
cx,
|
||||||
NEEDLESS_COLLECT,
|
NEEDLESS_COLLECT,
|
||||||
span,
|
span,
|
||||||
NEEDLESS_COLLECT_MSG,
|
NEEDLESS_COLLECT_MSG,
|
||||||
"replace with",
|
"replace with",
|
||||||
"get(0).is_none()".to_string(),
|
"next().is_none()".to_string(),
|
||||||
Applicability::MachineApplicable,
|
Applicability::MachineApplicable,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if method.ident.name == sym!(contains) {
|
if method.ident.name == sym!(contains) {
|
||||||
let contains_arg = snippet(cx, args[1].span, "??");
|
let contains_arg = snippet(cx, args[1].span, "??");
|
||||||
let span = shorten_span(expr, sym!(collect));
|
let span = shorten_needless_collect_span(expr);
|
||||||
span_lint_and_then(
|
span_lint_and_then(
|
||||||
cx,
|
cx,
|
||||||
NEEDLESS_COLLECT,
|
NEEDLESS_COLLECT,
|
||||||
|
@ -2579,13 +2579,13 @@ fn detect_iter_and_into_iters<'tcx>(block: &'tcx Block<'tcx>, identifier: Ident)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn shorten_span(expr: &Expr<'_>, target_fn_name: Symbol) -> Span {
|
fn shorten_needless_collect_span(expr: &Expr<'_>) -> Span {
|
||||||
let mut current_expr = expr;
|
if_chain! {
|
||||||
while let ExprKind::MethodCall(ref path, ref span, ref args, _) = current_expr.kind {
|
if let ExprKind::MethodCall(.., args, _) = &expr.kind;
|
||||||
if path.ident.name == target_fn_name {
|
if let ExprKind::MethodCall(_, span, ..) = &args[0].kind;
|
||||||
|
then {
|
||||||
return expr.span.with_lo(span.lo());
|
return expr.span.with_lo(span.lo());
|
||||||
}
|
}
|
||||||
current_expr = &args[0];
|
|
||||||
}
|
}
|
||||||
unreachable!()
|
unreachable!();
|
||||||
}
|
}
|
||||||
|
|
5
tests/ui/crashes/ice-5872.rs
Normal file
5
tests/ui/crashes/ice-5872.rs
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
#![warn(clippy::needless_collect)]
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let _ = vec![1, 2, 3].into_iter().collect::<Vec<_>>().is_empty();
|
||||||
|
}
|
10
tests/ui/crashes/ice-5872.stderr
Normal file
10
tests/ui/crashes/ice-5872.stderr
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
error: avoid using `collect()` when not needed
|
||||||
|
--> $DIR/ice-5872.rs:4:39
|
||||||
|
|
|
||||||
|
LL | let _ = vec![1, 2, 3].into_iter().collect::<Vec<_>>().is_empty();
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `next().is_none()`
|
||||||
|
|
|
||||||
|
= note: `-D clippy::needless-collect` implied by `-D warnings`
|
||||||
|
|
||||||
|
error: aborting due to previous error
|
||||||
|
|
|
@ -5,11 +5,11 @@
|
||||||
use std::collections::{BTreeSet, HashMap, HashSet};
|
use std::collections::{BTreeSet, HashMap, HashSet};
|
||||||
|
|
||||||
#[warn(clippy::needless_collect)]
|
#[warn(clippy::needless_collect)]
|
||||||
#[allow(unused_variables, clippy::iter_cloned_collect)]
|
#[allow(unused_variables, clippy::iter_cloned_collect, clippy::iter_next_slice)]
|
||||||
fn main() {
|
fn main() {
|
||||||
let sample = [1; 5];
|
let sample = [1; 5];
|
||||||
let len = sample.iter().count();
|
let len = sample.iter().count();
|
||||||
if sample.get(0).is_none() {
|
if sample.iter().next().is_none() {
|
||||||
// Empty
|
// Empty
|
||||||
}
|
}
|
||||||
sample.iter().cloned().any(|x| x == 1);
|
sample.iter().cloned().any(|x| x == 1);
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
use std::collections::{BTreeSet, HashMap, HashSet};
|
use std::collections::{BTreeSet, HashMap, HashSet};
|
||||||
|
|
||||||
#[warn(clippy::needless_collect)]
|
#[warn(clippy::needless_collect)]
|
||||||
#[allow(unused_variables, clippy::iter_cloned_collect)]
|
#[allow(unused_variables, clippy::iter_cloned_collect, clippy::iter_next_slice)]
|
||||||
fn main() {
|
fn main() {
|
||||||
let sample = [1; 5];
|
let sample = [1; 5];
|
||||||
let len = sample.iter().collect::<Vec<_>>().len();
|
let len = sample.iter().collect::<Vec<_>>().len();
|
||||||
|
|
|
@ -7,10 +7,10 @@ LL | let len = sample.iter().collect::<Vec<_>>().len();
|
||||||
= note: `-D clippy::needless-collect` implied by `-D warnings`
|
= note: `-D clippy::needless-collect` implied by `-D warnings`
|
||||||
|
|
||||||
error: avoid using `collect()` when not needed
|
error: avoid using `collect()` when not needed
|
||||||
--> $DIR/needless_collect.rs:12:15
|
--> $DIR/needless_collect.rs:12:22
|
||||||
|
|
|
|
||||||
LL | if sample.iter().collect::<Vec<_>>().is_empty() {
|
LL | if sample.iter().collect::<Vec<_>>().is_empty() {
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `get(0).is_none()`
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `next().is_none()`
|
||||||
|
|
||||||
error: avoid using `collect()` when not needed
|
error: avoid using `collect()` when not needed
|
||||||
--> $DIR/needless_collect.rs:15:28
|
--> $DIR/needless_collect.rs:15:28
|
||||||
|
|
Loading…
Reference in a new issue