Skip to content

Commit 692ec68

Browse files
committed
also lint .as_mut().cloned()
1 parent 22ad679 commit 692ec68

File tree

5 files changed

+17
-16
lines changed

5 files changed

+17
-16
lines changed

clippy_lints/src/methods/mod.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -3886,13 +3886,14 @@ declare_clippy_lint! {
38863886
pub STR_SPLIT_AT_NEWLINE,
38873887
pedantic,
38883888
"splitting a trimmed string at hard-coded newlines"
3889-
3889+
}
3890+
38903891
declare_clippy_lint! {
38913892
/// ### What it does
3892-
/// Checks for usage of `.as_ref().cloned()` on `Option`s
3893+
/// Checks for usage of `.as_ref().cloned()` and `.as_mut().cloned()` on `Option`s
38933894
///
38943895
/// ### Why is this bad?
3895-
/// This can be written more concisely by cloning the option directly.
3896+
/// This can be written more concisely by cloning the `Option` directly.
38963897
///
38973898
/// ### Example
38983899
/// ```no_run
@@ -4318,9 +4319,7 @@ impl Methods {
43184319
("assume_init", []) => uninit_assumed_init::check(cx, expr, recv),
43194320
("cloned", []) => {
43204321
cloned_instead_of_copied::check(cx, expr, recv, span, &self.msrv);
4321-
if let Some(("as_ref", recv, .., as_ref_ident_span)) = method_call(recv) {
4322-
option_as_ref_cloned::check(cx, recv, as_ref_ident_span, span);
4323-
}
4322+
option_as_ref_cloned::check(cx, recv, span);
43244323
},
43254324
("collect", []) if is_trait_method(cx, expr, sym::Iterator) => {
43264325
needless_collect::check(cx, span, expr, recv, call_span);

clippy_lints/src/methods/option_as_ref_cloned.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@ use rustc_hir::Expr;
55
use rustc_lint::LateContext;
66
use rustc_span::{sym, Span};
77

8-
use super::OPTION_AS_REF_CLONED;
8+
use super::{method_call, OPTION_AS_REF_CLONED};
99

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 Some((method @ ("as_ref" | "as_mut"), as_ref_recv, [], as_ref_ident_span, _)) = method_call(cloned_recv)
12+
&& is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(as_ref_recv).peel_refs(), sym::Option)
13+
{
1214
span_lint_and_sugg(
1315
cx,
1416
OPTION_AS_REF_CLONED,
15-
as_ref_ident_span.to(cloned_span),
16-
"cloning an `Option<_>` using `.as_ref().cloned()`",
17+
as_ref_ident_span.to(cloned_ident_span),
18+
&format!("cloning an `Option<_>` using `.{method}().cloned()`"),
1719
"this can be written more concisely by cloning the `Option<_>` directly",
1820
"clone".into(),
1921
Applicability::MachineApplicable,

tests/ui/option_as_ref_cloned.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![allow(clippy::clone_on_copy)]
33

44
fn main() {
5-
let x = Some(String::new());
5+
let mut x = Some(String::new());
66

77
let _: Option<String> = x.clone();
88
let _: Option<String> = x.clone();

tests/ui/option_as_ref_cloned.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
#![allow(clippy::clone_on_copy)]
33

44
fn main() {
5-
let x = Some(String::new());
5+
let mut x = Some(String::new());
66

77
let _: Option<String> = x.as_ref().cloned();
8-
let _: Option<String> = x.as_ref().cloned();
8+
let _: Option<String> = x.as_mut().cloned();
99

1010
let y = x.as_ref();
1111
let _: Option<&String> = y.as_ref().cloned();

tests/ui/option_as_ref_cloned.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ help: this can be written more concisely by cloning the `Option<_>` directly
1111
LL | let _: Option<String> = x.clone();
1212
| ~~~~~
1313

14-
error: cloning an `Option<_>` using `.as_ref().cloned()`
14+
error: cloning an `Option<_>` using `.as_mut().cloned()`
1515
--> $DIR/option_as_ref_cloned.rs:8:31
1616
|
17-
LL | let _: Option<String> = x.as_ref().cloned();
17+
LL | let _: Option<String> = x.as_mut().cloned();
1818
| ^^^^^^^^^^^^^^^
1919
|
2020
help: this can be written more concisely by cloning the `Option<_>` directly

0 commit comments

Comments
 (0)