Skip to content

Commit 5b520b6

Browse files
committed
handle another edge case
1 parent 4bb4f13 commit 5b520b6

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

clippy_lints/src/and_then_then_some.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,13 @@ 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);
45+
//(expr);
4646
//let option_id = cx.tcx.get_diagnostic_item(sym::Option);
4747
// TODO: check if type of reciever is diagnostic item Option.
48-
let tckr = cx.typeck_results();
49-
let def_id = tckr.type_dependent_def_id(expr.hir_id).unwrap();
48+
//let tckr = cx.typeck_results();
49+
//let def_id = tckr.type_dependent_def_id(expr.hir_id).unwrap();
5050
//dbg!(method_name, selfarg, arg);
51-
if dbg!(match_def_path(cx, dbg!(def_id),
52-
&["core", "option", "Option", "and_then"]))
51+
if is_and_then(cx, expr)
5352
{
5453
if let Some((closure_args, predicate)) = dbg!(then_some_closure_arg(cx, arg)) {
5554
//dbg!(predicate);
@@ -108,7 +107,7 @@ fn peel_closure_body<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, closu
108107
ExprKind::Call(func, [ pred, arg ]) => {
109108
//dbg!(func, fn_def_id(cx, expr));
110109
//todo!();
111-
if is_then_some(cx, func) && dbg!(is_local_defined_at(cx, arg, closure_arg_id)) {
110+
if dbg!(is_then_some(cx, expr)) && dbg!(is_local_defined_at(cx, arg, closure_arg_id)) {
112111
//todo!("it worked!!");
113112
Some(pred)
114113

@@ -155,22 +154,21 @@ fn show_sugg(cx: &LateContext<'_>, span: Span, selfarg: &Expr<'_>, closure_args:
155154

156155
fn is_then_some(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
157156
if let Some(def_id) = fn_def_id(cx, expr) {
158-
match_def_path(
157+
dbg!(match_def_path(
159158
cx, dbg!(def_id),
160-
&["core", "bool", "<impl bool>", "then_some"])
159+
&["core", "bool", "<impl bool>", "then_some"]))
161160
} else {
162-
//todo!("not type dependent");
161+
dbg!(expr);
163162
false
164163
}
165164
}
166165

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

tests/ui/and_then_then_some.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ fn main() {
66
let _y = x.clone().and_then(|v| v.starts_with('f')
77
.then_some(v));
88

9-
/*let _z = x.clone().and_then(|v| bool::then_some(v.starts_with('f'), v));
10-
// even if it's called as an associated method with a block body
9+
let _z = x.clone().and_then(|v| bool::then_some(v.starts_with('f'), v));
1110
let _w = Option::and_then(x, |v: String| {
1211
bool::then_some(v.starts_with('f'), v)
13-
});*/
12+
});
1413
}

0 commit comments

Comments
 (0)