Skip to content

Commit 4bb4f13

Browse files
committed
saftey commit: it kinda works for the basic case
1 parent 1d78783 commit 4bb4f13

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

clippy_lints/src/and_then_then_some.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,16 @@ impl<'tcx> LateLintPass<'tcx> for AndThenThenSome {
4242
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
4343
match expr.kind {
4444
ExprKind::MethodCall(method_name, selfarg, [ arg ], span) => {
45+
dbg!(expr);
4546
//let option_id = cx.tcx.get_diagnostic_item(sym::Option);
4647
// TODO: check if type of reciever is diagnostic item Option.
4748
let tckr = cx.typeck_results();
4849
let def_id = tckr.type_dependent_def_id(expr.hir_id).unwrap();
4950
//dbg!(method_name, selfarg, arg);
50-
if match_def_path(cx, def_id,
51-
&["core", "option", "Option", "and_then"])
51+
if dbg!(match_def_path(cx, dbg!(def_id),
52+
&["core", "option", "Option", "and_then"]))
5253
{
53-
if let Some((closure_args, predicate)) = then_some_closure_arg(cx, arg) {
54+
if let Some((closure_args, predicate)) = dbg!(then_some_closure_arg(cx, arg)) {
5455
//dbg!(predicate);
5556
show_sugg(cx, expr.span, selfarg, closure_args, predicate);
5657
}
@@ -104,7 +105,7 @@ fn peel_closure_body<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, closu
104105
None
105106
}
106107
}
107-
ExprKind::Call(func /*@ Expr{ kind: ExprKind::Path(QPath::Resolved(_, Path{ res: Res::Local(local_hid), ..})), ..},*/, [ pred, arg ]) => {
108+
ExprKind::Call(func, [ pred, arg ]) => {
108109
//dbg!(func, fn_def_id(cx, expr));
109110
//todo!();
110111
if is_then_some(cx, func) && dbg!(is_local_defined_at(cx, arg, closure_arg_id)) {
@@ -163,3 +164,13 @@ fn is_then_some(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
163164
}
164165
}
165166

167+
fn is_and_then(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
168+
if let Some(def_id) = fn_def_id(cx, expr) {
169+
match_def_path(
170+
cx, dbg!(def_id),
171+
&["core", "option", "Option", "and_then"])
172+
} else {
173+
//todo!("not type dependent");
174+
false
175+
}
176+
}

tests/ui/and_then_then_some.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@
33
fn main() {
44
let x = Some("foo".to_string());
55

6-
//let _y = x.clone().and_then(|v| v.starts_with('f')
7-
// .then_some(v));
6+
let _y = x.clone().and_then(|v| v.starts_with('f')
7+
.then_some(v));
88

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

0 commit comments

Comments
 (0)