Skip to content

Commit 7a0adc4

Browse files
committed
working ui test
1 parent 3b9bba4 commit 7a0adc4

File tree

3 files changed

+36
-32
lines changed

3 files changed

+36
-32
lines changed

clippy_lints/src/and_then_then_some.rs

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,19 @@ impl<'tcx> LateLintPass<'tcx> for AndThenThenSome {
4747
// TODO: check if type of reciever is diagnostic item Option.
4848
//let tckr = cx.typeck_results();
4949
//let def_id = tckr.type_dependent_def_id(expr.hir_id).unwrap();
50-
//dbg!(method_name, selfarg, arg);
50+
//(method_name, selfarg, arg);
5151
if is_and_then(cx, expr)
5252
{
53-
if let Some((closure_args, predicate)) = dbg!(then_some_closure_arg(cx, arg)) {
54-
//dbg!(predicate);
53+
if let Some((closure_args, predicate)) = (then_some_closure_arg(cx, arg)) {
54+
//(predicate);
5555
show_sugg(cx, expr.span, selfarg, closure_args, predicate);
5656
}
5757
}
5858
}
5959
ExprKind::Call(_func, [ selfarg, arg ]) => {
60-
if dbg!(is_and_then(cx, expr)) {
61-
if let Some((closure_args, predicate)) = dbg!(then_some_closure_arg(cx, arg)) {
62-
//dbg!(predicate);
60+
if (is_and_then(cx, expr)) {
61+
if let Some((closure_args, predicate)) = (then_some_closure_arg(cx, arg)) {
62+
//(predicate);
6363
show_sugg(cx, expr.span, selfarg, closure_args, predicate);
6464
}
6565
}
@@ -74,16 +74,16 @@ impl<'tcx> LateLintPass<'tcx> for AndThenThenSome {
7474
fn then_some_closure_arg<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>)
7575
-> Option<(Span, &'tcx Expr<'tcx>)>
7676
{
77-
dbg!(expr);
77+
(expr);
7878
match expr.kind {
7979
ExprKind::Closure(Closure{
8080
fn_decl: FnDecl{ inputs: [ Ty{ hir_id: arg_id, ..} ], .. },
8181
body,
8282
..
8383
}) => {
84-
if let Node::Expr(expr) = dbg!(cx.tcx.hir_node(body.hir_id)) {
85-
//dbg!(arg_id);
86-
if let Some(body) = dbg!(peel_closure_body(cx, expr, *arg_id)) {
84+
if let Node::Expr(expr) = (cx.tcx.hir_node(body.hir_id)) {
85+
//(arg_id);
86+
if let Some(body) = (peel_closure_body(cx, expr, *arg_id)) {
8787
Some((cx.tcx.hir().span(*arg_id), body))
8888
} else {
8989
None
@@ -114,35 +114,29 @@ fn peel_closure_body<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, closu
114114
}
115115
}
116116
ExprKind::Call(func, [ pred, arg ]) => {
117-
//dbg!(func, fn_def_id(cx, expr));
118-
//todo!();
119-
if dbg!(is_then_some(cx, expr)) && dbg!(is_local_defined_at(cx, arg, closure_arg_id)) {
120-
//todo!("it worked!!");
117+
if (is_then_some(cx, expr)) && (is_local_defined_at(cx, arg, closure_arg_id)) {
121118
Some(pred)
122119

123120
} else {
124-
//todo!("nope");
125121
None
126122
}
127123
}
128124
_ => {
129-
eprintln!("cannot peel {expr:#?}");
130125
None
131126
}
132127
}
133128
}
134129

135130
fn is_local_defined_at<'tcx>(cx: &LateContext<'tcx>, local: &Expr<'_>, arg_hid: HirId) -> bool {
136-
dbg!(local);
137131
match local.kind {
138132
ExprKind::Path(QPath::Resolved(_, Path{ res: Res::Local(local_hid), .. })) => {
139133
// FIXME: this is the best way i could find to compare if
140134
// a local refers to a specific closure argument.
141135
//
142136
// it breaks if the closure argument has an explicitly declared type,
143137
// since the spans only align for TyKind::Infer
144-
if let Node::Pat(Pat{ span: local_span, .. }) = dbg!(cx.tcx.hir_node(*local_hid)) &&
145-
let Node::Ty(Ty{ span: arg_span, .. }) = dbg!(cx.tcx.hir_node(arg_hid)) &&
138+
if let Node::Pat(Pat{ span: local_span, .. }) = (cx.tcx.hir_node(*local_hid)) &&
139+
let Node::Ty(Ty{ span: arg_span, .. }) = (cx.tcx.hir_node(arg_hid)) &&
146140
local_span == arg_span
147141
{
148142
true
@@ -170,19 +164,19 @@ fn show_sugg(cx: &LateContext<'_>, span: Span, selfarg: &Expr<'_>, closure_args:
170164

171165
fn is_then_some(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
172166
if let Some(def_id) = fn_def_id(cx, expr) {
173-
dbg!(match_def_path(
174-
cx, dbg!(def_id),
167+
(match_def_path(
168+
cx, (def_id),
175169
&["core", "bool", "<impl bool>", "then_some"]))
176170
} else {
177-
dbg!(expr);
171+
(expr);
178172
false
179173
}
180174
}
181175

182176
fn is_and_then(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
183177
if let Some(def_id) = fn_def_id(cx, expr) {
184-
dbg!(match_def_path(
185-
cx, dbg!(def_id),
178+
(match_def_path(
179+
cx, (def_id),
186180
&["core", "option", "Option", "and_then"]))
187181
} else {
188182
false

tests/ui/and_then_then_some.fixed

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ fn main() {
55

66
let _y = x.clone().filter(|v| v.starts_with('f'));
77

8-
let ts = bool::then_some;
9-
// even if the function is renamed, it should still fire
10-
let _z = x.clone().and_then(|v| (ts(v.starts_with('f'), v)));
11-
// even if it's called as an associated method with a block body
12-
/*let _w = Option::and_then(x, |v: String| {
13-
bool::then_some(v.starts_with('f'), v)
14-
});*/
8+
let _z = x.clone().filter(|v| v.starts_with('f'));
9+
let _w = x.filter(|v| v.starts_with('f'));
1510
}

tests/ui/and_then_then_some.stderr

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,20 @@ LL | | .then_some(v));
99
= note: `-D clippy::and-then-then-some` implied by `-D warnings`
1010
= help: to override `-D warnings` add `#[allow(clippy::and_then_then_some)]`
1111

12-
error: aborting due to 1 previous error
12+
error: use of `and_then` + `then_some` is equivelent to `filter`
13+
--> tests/ui/and_then_then_some.rs:9:11
14+
|
15+
LL | let _z = x.clone().and_then(|v| bool::then_some(v.starts_with('f'), v));
16+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `Option::filter` instead: `x.clone().filter(|v| v.starts_with('f'))`
17+
18+
error: use of `and_then` + `then_some` is equivelent to `filter`
19+
--> tests/ui/and_then_then_some.rs:10:11
20+
|
21+
LL | let _w = Option::and_then(x, |v| {
22+
| ______________^
23+
LL | | bool::then_some(v.starts_with('f'), v)
24+
LL | | });
25+
| |______^ help: use `Option::filter` instead: `x.filter(|v| v.starts_with('f'))`
26+
27+
error: aborting due to 3 previous errors
1328

0 commit comments

Comments
 (0)