Skip to content

Commit beb57f0

Browse files
committed
Don't ICE with late bound regions
1 parent 7c595b4 commit beb57f0

File tree

4 files changed

+45
-11
lines changed

4 files changed

+45
-11
lines changed

clippy_lints/src/methods/filter_map_bool_then.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use super::FILTER_MAP_BOOL_THEN;
12
use clippy_utils::diagnostics::span_lint_and_sugg;
23
use clippy_utils::paths::BOOL_THEN;
34
use clippy_utils::source::snippet_opt;
@@ -7,10 +8,9 @@ use rustc_errors::Applicability;
78
use rustc_hir::{Expr, ExprKind};
89
use rustc_lint::{LateContext, LintContext};
910
use rustc_middle::lint::in_external_macro;
11+
use rustc_middle::ty::Binder;
1012
use rustc_span::{sym, Span};
1113

12-
use super::FILTER_MAP_BOOL_THEN;
13-
1414
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, arg: &Expr<'_>, call_span: Span) {
1515
if !in_external_macro(cx.sess(), expr.span)
1616
&& is_trait_method(cx, expr, sym::Iterator)
@@ -21,7 +21,15 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, arg: &
2121
// `inputs` and `params` here as we need both the type and the span
2222
&& let param_ty = closure.fn_decl.inputs[0]
2323
&& let param = body.params[0]
24-
&& is_copy(cx, cx.typeck_results().node_type(param_ty.hir_id).peel_refs())
24+
// Issue #11309
25+
&& let param_ty = cx.tcx.liberate_late_bound_regions(
26+
closure.def_id.to_def_id(),
27+
Binder::bind_with_vars(
28+
cx.typeck_results().node_type(param_ty.hir_id),
29+
cx.tcx.late_bound_vars(cx.tcx.hir().local_def_id_to_hir_id(closure.def_id)),
30+
),
31+
)
32+
&& is_copy(cx, param_ty)
2533
&& let ExprKind::MethodCall(_, recv, [then_arg], _) = value.kind
2634
&& let ExprKind::Closure(then_closure) = then_arg.kind
2735
&& let then_body = peel_blocks(cx.tcx.hir().body(then_closure.body).value)

tests/ui/filter_map_bool_then.fixed

+11-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
clippy::clone_on_copy,
55
clippy::map_identity,
66
clippy::unnecessary_lazy_evaluations,
7+
clippy::unnecessary_filter_map,
78
unused
89
)]
910
#![warn(clippy::filter_map_bool_then)]
@@ -29,9 +30,14 @@ fn main() {
2930
.copied()
3031
.filter(|&i| i != 1000)
3132
.filter(|&i| (i.clone() % 2 == 0)).map(|i| i + 1);
33+
// Despite this is non-copy, `is_copy` still returns true (at least now) because it's `&NonCopy`,
34+
// and any `&` is `Copy`. So since we can dereference it in `filter` (since it's then `&&NonCopy`),
35+
// we can lint this and still get the same input type.
36+
let v = vec![NonCopy, NonCopy];
37+
v.clone().iter().filter(|&i| (i == &NonCopy)).map(|i| i);
3238
// Do not lint
3339
let v = vec![NonCopy, NonCopy];
34-
v.clone().iter().filter_map(|i| (i == &NonCopy).then(|| i));
40+
v.clone().into_iter().filter_map(|i| (i == NonCopy).then(|| i));
3541
external! {
3642
let v = vec![1, 2, 3, 4, 5, 6];
3743
v.clone().into_iter().filter_map(|i| (i % 2 == 0).then(|| i + 1));
@@ -42,3 +48,7 @@ fn main() {
4248
v.clone().into_iter().filter_map(|i| (i % 2 == 0).then(|| i + 1));
4349
}
4450
}
51+
52+
fn issue11309<'a>(iter: impl Iterator<Item = (&'a str, &'a str)>) -> Vec<&'a str> {
53+
iter.filter_map(|(_, s): (&str, _)| Some(s)).collect()
54+
}

tests/ui/filter_map_bool_then.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
clippy::clone_on_copy,
55
clippy::map_identity,
66
clippy::unnecessary_lazy_evaluations,
7+
clippy::unnecessary_filter_map,
78
unused
89
)]
910
#![warn(clippy::filter_map_bool_then)]
@@ -29,9 +30,14 @@ fn main() {
2930
.copied()
3031
.filter(|&i| i != 1000)
3132
.filter_map(|i| (i.clone() % 2 == 0).then(|| i + 1));
32-
// Do not lint
33+
// Despite this is non-copy, `is_copy` still returns true (at least now) because it's `&NonCopy`,
34+
// and any `&` is `Copy`. So since we can dereference it in `filter` (since it's then `&&NonCopy`),
35+
// we can lint this and still get the same input type.
3336
let v = vec![NonCopy, NonCopy];
3437
v.clone().iter().filter_map(|i| (i == &NonCopy).then(|| i));
38+
// Do not lint
39+
let v = vec![NonCopy, NonCopy];
40+
v.clone().into_iter().filter_map(|i| (i == NonCopy).then(|| i));
3541
external! {
3642
let v = vec![1, 2, 3, 4, 5, 6];
3743
v.clone().into_iter().filter_map(|i| (i % 2 == 0).then(|| i + 1));
@@ -42,3 +48,7 @@ fn main() {
4248
v.clone().into_iter().filter_map(|i| (i % 2 == 0).then(|| i + 1));
4349
}
4450
}
51+
52+
fn issue11309<'a>(iter: impl Iterator<Item = (&'a str, &'a str)>) -> Vec<&'a str> {
53+
iter.filter_map(|(_, s): (&str, _)| Some(s)).collect()
54+
}

tests/ui/filter_map_bool_then.stderr

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,40 @@
11
error: usage of `bool::then` in `filter_map`
2-
--> $DIR/filter_map_bool_then.rs:19:22
2+
--> $DIR/filter_map_bool_then.rs:20:22
33
|
44
LL | v.clone().iter().filter_map(|i| (i % 2 == 0).then(|| i + 1));
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `filter` then `map` instead: `filter(|&i| (i % 2 == 0)).map(|i| i + 1)`
66
|
77
= note: `-D clippy::filter-map-bool-then` implied by `-D warnings`
88

99
error: usage of `bool::then` in `filter_map`
10-
--> $DIR/filter_map_bool_then.rs:20:27
10+
--> $DIR/filter_map_bool_then.rs:21:27
1111
|
1212
LL | v.clone().into_iter().filter_map(|i| (i % 2 == 0).then(|| i + 1));
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `filter` then `map` instead: `filter(|&i| (i % 2 == 0)).map(|i| i + 1)`
1414

1515
error: usage of `bool::then` in `filter_map`
16-
--> $DIR/filter_map_bool_then.rs:23:10
16+
--> $DIR/filter_map_bool_then.rs:24:10
1717
|
1818
LL | .filter_map(|i| -> Option<_> { (i % 2 == 0).then(|| i + 1) });
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `filter` then `map` instead: `filter(|&i| (i % 2 == 0)).map(|i| i + 1)`
2020

2121
error: usage of `bool::then` in `filter_map`
22-
--> $DIR/filter_map_bool_then.rs:27:10
22+
--> $DIR/filter_map_bool_then.rs:28:10
2323
|
2424
LL | .filter_map(|i| (i % 2 == 0).then(|| i + 1));
2525
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `filter` then `map` instead: `filter(|&i| (i % 2 == 0)).map(|i| i + 1)`
2626

2727
error: usage of `bool::then` in `filter_map`
28-
--> $DIR/filter_map_bool_then.rs:31:10
28+
--> $DIR/filter_map_bool_then.rs:32:10
2929
|
3030
LL | .filter_map(|i| (i.clone() % 2 == 0).then(|| i + 1));
3131
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `filter` then `map` instead: `filter(|&i| (i.clone() % 2 == 0)).map(|i| i + 1)`
3232

33-
error: aborting due to 5 previous errors
33+
error: usage of `bool::then` in `filter_map`
34+
--> $DIR/filter_map_bool_then.rs:37:22
35+
|
36+
LL | v.clone().iter().filter_map(|i| (i == &NonCopy).then(|| i));
37+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `filter` then `map` instead: `filter(|&i| (i == &NonCopy)).map(|i| i)`
38+
39+
error: aborting due to 6 previous errors
3440

0 commit comments

Comments
 (0)