Skip to content

Commit c6c71af

Browse files
refactor(option_as_ref_cloned): move the method_call call out of the check (#15917)
This is more consistent with what the other `methods/` lints do. changelog: none
2 parents fb82de5 + 4e3fa96 commit c6c71af

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5050,7 +5050,11 @@ impl Methods {
50505050
(sym::bytes, []) => unbuffered_bytes::check(cx, expr, recv),
50515051
(sym::cloned, []) => {
50525052
cloned_instead_of_copied::check(cx, expr, recv, span, self.msrv);
5053-
option_as_ref_cloned::check(cx, recv, span);
5053+
if let Some((method @ (sym::as_ref | sym::as_mut), as_ref_recv, [], as_ref_ident_span, _)) =
5054+
method_call(recv)
5055+
{
5056+
option_as_ref_cloned::check(cx, span, method, as_ref_recv, as_ref_ident_span);
5057+
}
50545058
},
50555059
(sym::collect, []) if cx.ty_based_def(expr).opt_parent(cx).is_diag_item(cx, sym::Iterator) => {
50565060
needless_collect::check(cx, span, expr, recv, call_span);

clippy_lints/src/methods/option_as_ref_cloned.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,28 @@ use clippy_utils::sym;
44
use rustc_errors::Applicability;
55
use rustc_hir::Expr;
66
use rustc_lint::LateContext;
7-
use rustc_span::Span;
7+
use rustc_span::{Span, Symbol};
88

9-
use super::{OPTION_AS_REF_CLONED, method_call};
9+
use super::OPTION_AS_REF_CLONED;
1010

11-
pub(super) fn check(cx: &LateContext<'_>, cloned_recv: &Expr<'_>, cloned_ident_span: Span) {
12-
if let Some((method @ (sym::as_ref | sym::as_mut), as_ref_recv, [], as_ref_ident_span, _)) =
13-
method_call(cloned_recv)
14-
&& cx
15-
.typeck_results()
16-
.expr_ty(as_ref_recv)
17-
.peel_refs()
18-
.is_diag_item(cx, sym::Option)
11+
pub(super) fn check(
12+
cx: &LateContext<'_>,
13+
cloned_ident_span: Span,
14+
as_ref_method: Symbol,
15+
as_ref_recv: &Expr<'_>,
16+
as_ref_ident_span: Span,
17+
) {
18+
if cx
19+
.typeck_results()
20+
.expr_ty(as_ref_recv)
21+
.peel_refs()
22+
.is_diag_item(cx, sym::Option)
1923
{
2024
span_lint_and_sugg(
2125
cx,
2226
OPTION_AS_REF_CLONED,
2327
as_ref_ident_span.to(cloned_ident_span),
24-
format!("cloning an `Option<_>` using `.{method}().cloned()`"),
28+
format!("cloning an `Option<_>` using `.{as_ref_method}().cloned()`"),
2529
"this can be written more concisely by cloning the `Option<_>` directly",
2630
"clone".into(),
2731
Applicability::MachineApplicable,

0 commit comments

Comments
 (0)