Skip to content

Commit

Permalink
also lint .as_mut().cloned()
Browse files Browse the repository at this point in the history
  • Loading branch information
y21 committed Dec 30, 2023
1 parent f7f596e commit c5a2706
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 12 deletions.
8 changes: 4 additions & 4 deletions clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3832,10 +3832,10 @@ declare_clippy_lint! {

declare_clippy_lint! {
/// ### What it does
/// Checks for usage of `.as_ref().cloned()` on `Option`s
/// Checks for usage of `.as_ref().cloned()` and `.as_mut().cloned()` on `Option`s
///
/// ### Why is this bad?
/// This can be written more concisely by cloning the option directly.
/// This can be written more concisely by cloning the `Option` directly.
///
/// ### Example
/// ```no_run
Expand Down Expand Up @@ -4259,8 +4259,8 @@ impl Methods {
("assume_init", []) => uninit_assumed_init::check(cx, expr, recv),
("cloned", []) => {
cloned_instead_of_copied::check(cx, expr, recv, span, &self.msrv);
if let Some(("as_ref", recv, .., as_ref_ident_span)) = method_call(recv) {
option_as_ref_cloned::check(cx, recv, as_ref_ident_span, span);
if let Some((name @ ("as_ref" | "as_mut"), recv, .., as_ref_ident_span)) = method_call(recv) {
option_as_ref_cloned::check(cx, recv, as_ref_ident_span, span, name);
}
},
("collect", []) if is_trait_method(cx, expr, sym::Iterator) => {
Expand Down
12 changes: 9 additions & 3 deletions clippy_lints/src/methods/option_as_ref_cloned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@ use rustc_span::{sym, Span};

use super::OPTION_AS_REF_CLONED;

pub(super) fn check(cx: &LateContext<'_>, as_ref_recv: &Expr<'_>, as_ref_ident_span: Span, cloned_span: Span) {
pub(super) fn check(
cx: &LateContext<'_>,
as_ref_recv: &Expr<'_>,
as_ref_ident_span: Span,
cloned_ident_span: Span,
method_name: &str,
) {
if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(as_ref_recv).peel_refs(), sym::Option) {
span_lint_and_sugg(
cx,
OPTION_AS_REF_CLONED,
as_ref_ident_span.to(cloned_span),
"cloning an `Option<_>` using `.as_ref().cloned()`",
as_ref_ident_span.to(cloned_ident_span),
&format!("cloning an `Option<_>` using `.{method_name}().cloned()`"),
"this can be written more concisely by cloning the `Option<_>` directly",
"clone".into(),
Applicability::MachineApplicable,
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/option_as_ref_cloned.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![allow(clippy::clone_on_copy)]

fn main() {
let x = Some(String::new());
let mut x = Some(String::new());

let _: Option<String> = x.clone();
let _: Option<String> = x.clone();
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/option_as_ref_cloned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
#![allow(clippy::clone_on_copy)]

fn main() {
let x = Some(String::new());
let mut x = Some(String::new());

let _: Option<String> = x.as_ref().cloned();
let _: Option<String> = x.as_ref().cloned();
let _: Option<String> = x.as_mut().cloned();

let y = x.as_ref();
let _: Option<&String> = y.as_ref().cloned();
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/option_as_ref_cloned.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ help: this can be written more concisely by cloning the `Option<_>` directly
LL | let _: Option<String> = x.clone();
| ~~~~~

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

0 comments on commit c5a2706

Please sign in to comment.