|
1 | 1 | use super::utils::make_iterator_snippet;
|
2 | 2 | use super::MANUAL_FLATTEN;
|
3 | 3 | use clippy_utils::diagnostics::span_lint_and_then;
|
| 4 | +use clippy_utils::visitors::LocalUsedVisitor; |
4 | 5 | use clippy_utils::{is_lang_ctor, path_to_local_id};
|
5 | 6 | use if_chain::if_chain;
|
6 | 7 | use rustc_errors::Applicability;
|
@@ -37,16 +38,18 @@ pub(super) fn check<'tcx>(
|
37 | 38 | if_chain! {
|
38 | 39 | if let Some(inner_expr) = inner_expr;
|
39 | 40 | if let ExprKind::Match(
|
40 |
| - match_expr, match_arms, MatchSource::IfLetDesugar{ contains_else_clause: false } |
| 41 | + match_expr, [true_arm, _else_arm], MatchSource::IfLetDesugar{ contains_else_clause: false } |
41 | 42 | ) = inner_expr.kind;
|
42 | 43 | // Ensure match_expr in `if let` statement is the same as the pat from the for-loop
|
43 | 44 | if let PatKind::Binding(_, pat_hir_id, _, _) = pat.kind;
|
44 | 45 | if path_to_local_id(match_expr, pat_hir_id);
|
45 | 46 | // Ensure the `if let` statement is for the `Some` variant of `Option` or the `Ok` variant of `Result`
|
46 |
| - if let PatKind::TupleStruct(ref qpath, _, _) = match_arms[0].pat.kind; |
| 47 | + if let PatKind::TupleStruct(ref qpath, _, _) = true_arm.pat.kind; |
47 | 48 | let some_ctor = is_lang_ctor(cx, qpath, OptionSome);
|
48 | 49 | let ok_ctor = is_lang_ctor(cx, qpath, ResultOk);
|
49 | 50 | if some_ctor || ok_ctor;
|
| 51 | + // Ensure epxr in `if let` is not used afterwards |
| 52 | + if !LocalUsedVisitor::new(cx, pat_hir_id).check_arm(true_arm); |
50 | 53 | then {
|
51 | 54 | let if_let_type = if some_ctor { "Some" } else { "Ok" };
|
52 | 55 | // Prepare the error message
|
|
0 commit comments