-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
136 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::ty::is_type_diagnostic_item; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::Expr; | ||
use rustc_lint::LateContext; | ||
use rustc_span::{sym, Span}; | ||
|
||
use super::{method_call, OPTION_AS_REF_CLONED}; | ||
|
||
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_ident_span), | ||
&format!("cloning an `Option<_>` using `.{method}().cloned()`"), | ||
"this can be written more concisely by cloning the `Option<_>` directly", | ||
"clone".into(), | ||
Applicability::MachineApplicable, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#![warn(clippy::option_as_ref_cloned)] | ||
#![allow(clippy::clone_on_copy)] | ||
|
||
fn main() { | ||
let mut x = Some(String::new()); | ||
|
||
let _: Option<String> = x.clone(); | ||
let _: Option<String> = x.clone(); | ||
|
||
let y = x.as_ref(); | ||
let _: Option<&String> = y.clone(); | ||
|
||
macro_rules! cloned_recv { | ||
() => { | ||
x.as_ref() | ||
}; | ||
} | ||
|
||
// Don't lint when part of the expression is from a macro | ||
let _: Option<String> = cloned_recv!().cloned(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#![warn(clippy::option_as_ref_cloned)] | ||
#![allow(clippy::clone_on_copy)] | ||
|
||
fn main() { | ||
let mut x = Some(String::new()); | ||
|
||
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(); | ||
|
||
macro_rules! cloned_recv { | ||
() => { | ||
x.as_ref() | ||
}; | ||
} | ||
|
||
// Don't lint when part of the expression is from a macro | ||
let _: Option<String> = cloned_recv!().cloned(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
error: cloning an `Option<_>` using `.as_ref().cloned()` | ||
--> $DIR/option_as_ref_cloned.rs:7:31 | ||
| | ||
LL | let _: Option<String> = x.as_ref().cloned(); | ||
| ^^^^^^^^^^^^^^^ | ||
| | ||
= note: `-D clippy::option-as-ref-cloned` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::option_as_ref_cloned)]` | ||
help: this can be written more concisely by cloning the `Option<_>` directly | ||
| | ||
LL | let _: Option<String> = x.clone(); | ||
| ~~~~~ | ||
|
||
error: cloning an `Option<_>` using `.as_mut().cloned()` | ||
--> $DIR/option_as_ref_cloned.rs:8:31 | ||
| | ||
LL | let _: Option<String> = x.as_mut().cloned(); | ||
| ^^^^^^^^^^^^^^^ | ||
| | ||
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()` | ||
--> $DIR/option_as_ref_cloned.rs:11:32 | ||
| | ||
LL | let _: Option<&String> = y.as_ref().cloned(); | ||
| ^^^^^^^^^^^^^^^ | ||
| | ||
help: this can be written more concisely by cloning the `Option<_>` directly | ||
| | ||
LL | let _: Option<&String> = y.clone(); | ||
| ~~~~~ | ||
|
||
error: aborting due to 3 previous errors | ||
|