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 Jan 1, 2024
1 parent 22ad679 commit 692ec68
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 16 deletions.
11 changes: 5 additions & 6 deletions clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3886,13 +3886,14 @@ declare_clippy_lint! {
pub STR_SPLIT_AT_NEWLINE,
pedantic,
"splitting a trimmed string at hard-coded newlines"

}

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 @@ -4318,9 +4319,7 @@ 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);
}
option_as_ref_cloned::check(cx, recv, span);
},
("collect", []) if is_trait_method(cx, expr, sym::Iterator) => {
needless_collect::check(cx, span, expr, recv, call_span);
Expand Down
12 changes: 7 additions & 5 deletions clippy_lints/src/methods/option_as_ref_cloned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ use rustc_hir::Expr;
use rustc_lint::LateContext;
use rustc_span::{sym, Span};

use super::OPTION_AS_REF_CLONED;
use super::{method_call, OPTION_AS_REF_CLONED};

pub(super) fn check(cx: &LateContext<'_>, as_ref_recv: &Expr<'_>, as_ref_ident_span: Span, cloned_span: Span) {
if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(as_ref_recv).peel_refs(), sym::Option) {
pub(super) fn check(cx: &LateContext<'_>, cloned_recv: &Expr<'_>, cloned_ident_span: Span) {
if let Some((method @ ("as_ref" | "as_mut"), as_ref_recv, [], as_ref_ident_span, _)) = method_call(cloned_recv)
&& 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}().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 692ec68

Please sign in to comment.