Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ICE in loops module #5877

Merged
merged 1 commit into from
Aug 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions clippy_lints/src/loops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2374,7 +2374,7 @@ fn check_needless_collect_direct_usage<'tcx>(expr: &'tcx Expr<'_>, cx: &LateCont
match_type(cx, ty, &paths::BTREEMAP) ||
is_type_diagnostic_item(cx, ty, sym!(hashmap_type)) {
if method.ident.name == sym!(len) {
let span = shorten_span(expr, sym!(collect));
let span = shorten_needless_collect_span(expr);
span_lint_and_sugg(
cx,
NEEDLESS_COLLECT,
Expand All @@ -2386,20 +2386,20 @@ fn check_needless_collect_direct_usage<'tcx>(expr: &'tcx Expr<'_>, cx: &LateCont
);
}
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(
cx,
NEEDLESS_COLLECT,
span,
NEEDLESS_COLLECT_MSG,
"replace with",
"get(0).is_none()".to_string(),
"next().is_none()".to_string(),
Applicability::MachineApplicable,
);
}
if method.ident.name == sym!(contains) {
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(
cx,
NEEDLESS_COLLECT,
Expand Down Expand Up @@ -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 {
let mut current_expr = expr;
while let ExprKind::MethodCall(ref path, ref span, ref args, _) = current_expr.kind {
if path.ident.name == target_fn_name {
fn shorten_needless_collect_span(expr: &Expr<'_>) -> Span {
if_chain! {
if let ExprKind::MethodCall(.., args, _) = &expr.kind;
if let ExprKind::MethodCall(_, span, ..) = &args[0].kind;
then {
return expr.span.with_lo(span.lo());
}
current_expr = &args[0];
}
unreachable!()
unreachable!();
}
5 changes: 5 additions & 0 deletions tests/ui/crashes/ice-5872.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#![warn(clippy::needless_collect)]

fn main() {
let _ = vec![1, 2, 3].into_iter().collect::<Vec<_>>().is_empty();
}
10 changes: 10 additions & 0 deletions tests/ui/crashes/ice-5872.stderr
Original file line number Diff line number Diff line change
@@ -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

4 changes: 2 additions & 2 deletions tests/ui/needless_collect.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
use std::collections::{BTreeSet, HashMap, HashSet};

#[warn(clippy::needless_collect)]
#[allow(unused_variables, clippy::iter_cloned_collect)]
#[allow(unused_variables, clippy::iter_cloned_collect, clippy::iter_next_slice)]
fn main() {
let sample = [1; 5];
let len = sample.iter().count();
if sample.get(0).is_none() {
if sample.iter().next().is_none() {
// Empty
}
sample.iter().cloned().any(|x| x == 1);
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/needless_collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use std::collections::{BTreeSet, HashMap, HashSet};

#[warn(clippy::needless_collect)]
#[allow(unused_variables, clippy::iter_cloned_collect)]
#[allow(unused_variables, clippy::iter_cloned_collect, clippy::iter_next_slice)]
fn main() {
let sample = [1; 5];
let len = sample.iter().collect::<Vec<_>>().len();
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/needless_collect.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ LL | let len = sample.iter().collect::<Vec<_>>().len();
= note: `-D clippy::needless-collect` implied by `-D warnings`

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() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `get(0).is_none()`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `next().is_none()`

error: avoid using `collect()` when not needed
--> $DIR/needless_collect.rs:15:28
Expand Down