|
1 | 1 | use clippy_utils::diagnostics::span_lint_and_sugg;
|
2 | 2 | use clippy_utils::ty::is_type_diagnostic_item;
|
3 | 3 | use rustc_errors::Applicability;
|
4 |
| -use rustc_hir::Expr; |
| 4 | +use rustc_hir::{Expr, ExprKind}; |
5 | 5 | use rustc_lint::LateContext;
|
6 | 6 | use rustc_span::{sym, Span};
|
7 | 7 |
|
8 | 8 | use super::OPTION_AS_REF_CLONED;
|
9 | 9 |
|
10 |
| -pub(super) fn check(cx: &LateContext<'_>, as_ref_recv: &Expr<'_>, as_ref_ident_span: Span, cloned_span: Span) { |
11 |
| - if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(as_ref_recv).peel_refs(), sym::Option) { |
| 10 | +pub(super) fn check(cx: &LateContext<'_>, cloned_recv: &Expr<'_>, cloned_ident_span: Span) { |
| 11 | + if let ExprKind::MethodCall(as_ref_path, as_ref_recv, ..) = cloned_recv.kind |
| 12 | + && is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(as_ref_recv).peel_refs(), sym::Option) |
| 13 | + && ["as_ref", "as_mut"].contains(&as_ref_path.ident.name.as_str()) |
| 14 | + { |
12 | 15 | span_lint_and_sugg(
|
13 | 16 | cx,
|
14 | 17 | OPTION_AS_REF_CLONED,
|
15 |
| - as_ref_ident_span.to(cloned_span), |
16 |
| - "cloning an `Option<_>` using `.as_ref().cloned()`", |
| 18 | + as_ref_path.ident.span.to(cloned_ident_span), |
| 19 | + &format!("cloning an `Option<_>` using `.{}().cloned()`", as_ref_path.ident), |
17 | 20 | "this can be written more concisely by cloning the `Option<_>` directly",
|
18 | 21 | "clone".into(),
|
19 | 22 | Applicability::MachineApplicable,
|
|
0 commit comments