]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/methods/obfuscated_if_else.rs
Rollup merge of #105615 - WaffleLapkin:remove_opt_scope_span_mention, r=compiler...
[rust.git] / src / tools / clippy / clippy_lints / src / methods / obfuscated_if_else.rs
1 // run-rustfix
2
3 use super::OBFUSCATED_IF_ELSE;
4 use clippy_utils::{diagnostics::span_lint_and_sugg, source::snippet_with_applicability};
5 use rustc_errors::Applicability;
6 use rustc_hir as hir;
7 use rustc_lint::LateContext;
8
9 pub(super) fn check<'tcx>(
10     cx: &LateContext<'tcx>,
11     expr: &'tcx hir::Expr<'_>,
12     then_recv: &'tcx hir::Expr<'_>,
13     then_arg: &'tcx hir::Expr<'_>,
14     unwrap_arg: &'tcx hir::Expr<'_>,
15 ) {
16     // something.then_some(blah).unwrap_or(blah)
17     // ^^^^^^^^^-then_recv ^^^^-then_arg   ^^^^- unwrap_arg
18     // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- expr
19
20     let recv_ty = cx.typeck_results().expr_ty(then_recv);
21
22     if recv_ty.is_bool() {
23         let mut applicability = Applicability::MachineApplicable;
24         let sugg = format!(
25             "if {} {{ {} }} else {{ {} }}",
26             snippet_with_applicability(cx, then_recv.span, "..", &mut applicability),
27             snippet_with_applicability(cx, then_arg.span, "..", &mut applicability),
28             snippet_with_applicability(cx, unwrap_arg.span, "..", &mut applicability)
29         );
30
31         span_lint_and_sugg(
32             cx,
33             OBFUSCATED_IF_ELSE,
34             expr.span,
35             "use of `.then_some(..).unwrap_or(..)` can be written \
36             more clearly with `if .. else ..`",
37             "try",
38             sugg,
39             applicability,
40         );
41     }
42 }