Skip to content

Commit 9a83403

Browse files
committed
fix another edge case and add more unit tests
1 parent 69b251f commit 9a83403

File tree

3 files changed

+7
-5
lines changed

3 files changed

+7
-5
lines changed

clippy_lints/src/and_then_then_some.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ fn then_some_closure_arg<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>) -> Opt
6666
..
6767
}) => {
6868
if let Node::Expr(expr) = cx.tcx.hir_node(body.hir_id) &&
69-
let Body{ params: [ Param{ hir_id: arg_id, .. } ], .. } =
69+
let Body{ params: [ Param{ hir_id: arg_id, pat: Pat{ span, .. }, .. } ], .. } =
7070
cx.tcx.hir().body(*body)
7171
{
7272

73-
(peel_closure_body(cx, expr, *arg_id)).map(|body| (cx.tcx.hir().span(*arg_id), body))
73+
(peel_closure_body(cx, expr, *arg_id)).map(|body| (*span, body))
7474
} else {
7575
None
7676
}
@@ -135,8 +135,10 @@ fn is_local_defined_at(cx: &LateContext<'_>, local: &Expr<'_>, arg_hid: HirId) -
135135

136136
fn show_sugg(cx: &LateContext<'_>, span: Span, selfarg: &Expr<'_>, closure_args: Span, predicate: &Expr<'_>) {
137137
let mut appl = Applicability::MachineApplicable;
138+
// FIXME: this relies on deref coertion, which won't work correctly if the predicate involves something
139+
// other than a method call.
138140
let sugg = format!(
139-
"{}.filter(|&{}| {})",
141+
"{}.filter(|{}| {})",
140142
snippet_with_applicability(cx, selfarg.span, "<OPTION>", &mut appl),
141143
snippet_with_applicability(cx, closure_args, "<ARGS>", &mut appl),
142144
snippet_with_applicability(cx, predicate.span, "<PREDICATE>", &mut appl)

tests/ui/and_then_then_some.fixed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ fn main() {
66
let _y = x.clone().filter(|v| v.starts_with('f'));
77

88
let _z = x.clone().filter(|v| v.starts_with('f'));
9-
let _w = x.filter(|v: String| v.starts_with('f'));
9+
let _w = x.filter(|v| v.starts_with('f'));
1010
}

tests/ui/and_then_then_some.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ error: use of `Option::and_then` and `bool::then_some` is equivelent to `filter`
1717
--> tests/ui/and_then_then_some.rs:9:14
1818
|
1919
LL | let _w = Option::and_then(x, |v: String| bool::then_some(v.starts_with('f'), v));
20-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `Option::filter` instead: `x.filter(|v: String| v.starts_with('f'))`
20+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `Option::filter` instead: `x.filter(|v| v.starts_with('f'))`
2121

2222
error: aborting due to 3 previous errors
2323

0 commit comments

Comments
 (0)