-
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
1 parent
b9241c3
commit d8ded3f
Showing
7 changed files
with
194 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::msrvs::{self, Msrv}; | ||
use clippy_utils::source::snippet; | ||
use clippy_utils::ty::get_type_diagnostic_name; | ||
use rustc_ast::ast::UnOp; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Expr, ExprKind}; | ||
use rustc_lint::LateContext; | ||
use rustc_span::{Symbol, sym}; | ||
|
||
use super::CLONE_ON_ARC_OR_RC; | ||
|
||
pub(super) fn check( | ||
cx: &LateContext<'_>, | ||
expr: &Expr<'_>, | ||
method_name: Symbol, | ||
receiver: &Expr<'_>, | ||
args: &[Expr<'_>], | ||
msrv: &Msrv, | ||
) { | ||
if !msrv.meets(msrvs::ARC_RC_UNWRAP_OR_CLONE) { | ||
return; | ||
} | ||
|
||
if method_name == sym::clone | ||
&& args.is_empty() | ||
&& let ExprKind::Unary(UnOp::Deref, recv) = receiver.kind | ||
&& let Some(arc_or_rc_path) = is_arc_or_rc(cx, recv) | ||
{ | ||
span_lint_and_sugg( | ||
cx, | ||
CLONE_ON_ARC_OR_RC, | ||
expr.span, | ||
"using `unwrap_or_clone` on an `Arc` or `Rc` is more efficient", | ||
"try", | ||
format!( | ||
"{arc_or_rc_path}::unwrap_or_clone({snip})", | ||
snip = snippet(cx, recv.span, "..") | ||
), | ||
Applicability::MachineApplicable, | ||
); | ||
} | ||
} | ||
|
||
fn is_arc_or_rc(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<&'static str> { | ||
match get_type_diagnostic_name(cx, cx.typeck_results().expr_ty(expr)) { | ||
Some(sym::Arc) => Some("std::sync::Arc"), | ||
Some(sym::Rc) => Some("std::rc::Rc"), | ||
_ => None, | ||
} | ||
} |
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,36 @@ | ||
#![warn(clippy::clone_on_arc_or_rc)] | ||
|
||
use std::rc::Rc; | ||
use std::sync::Arc; | ||
|
||
fn main() { | ||
let arc: Arc<String> = Arc::new("foo".into()); | ||
let _: String = std::sync::Arc::unwrap_or_clone(arc); | ||
//~^ error: using `unwrap_or_clone` on an `Arc` or `Rc` is more efficient | ||
|
||
let rc: Rc<String> = Rc::new("foo".into()); | ||
let _: String = std::rc::Rc::unwrap_or_clone(rc); | ||
//~^ error: using `unwrap_or_clone` on an `Arc` or `Rc` is more efficient | ||
|
||
let arc: Arc<String> = Arc::new("foo bar".into()); | ||
let _: Vec<_> = std::sync::Arc::unwrap_or_clone(arc).split(" ").collect(); | ||
//~^ error: using `unwrap_or_clone` on an `Arc` or `Rc` is more efficient | ||
|
||
let rc: Rc<Vec<u32>> = Rc::new(vec![1, 2, 3]); | ||
let _: Vec<_> = std::rc::Rc::unwrap_or_clone(rc).iter().map(|x| x + 1).collect(); | ||
//~^ error: using `unwrap_or_clone` on an `Arc` or `Rc` is more efficient | ||
|
||
let _: String = std::sync::Arc::unwrap_or_clone(Arc::<String>::new("foo".into())); | ||
//~^ error: using `unwrap_or_clone` on an `Arc` or `Rc` is more efficient | ||
} | ||
|
||
#[clippy::msrv = "1.75"] | ||
fn msrv_check() { | ||
let arc: Arc<String> = Arc::new("foo".into()); | ||
let _: String = (*arc).clone(); | ||
// Should not lint | ||
|
||
let rc: Rc<String> = Rc::new("foo".into()); | ||
let _: String = (*rc).clone(); | ||
// Should not lint | ||
} |
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,36 @@ | ||
#![warn(clippy::clone_on_arc_or_rc)] | ||
|
||
use std::rc::Rc; | ||
use std::sync::Arc; | ||
|
||
fn main() { | ||
let arc: Arc<String> = Arc::new("foo".into()); | ||
let _: String = (*arc).clone(); | ||
//~^ error: using `unwrap_or_clone` on an `Arc` or `Rc` is more efficient | ||
|
||
let rc: Rc<String> = Rc::new("foo".into()); | ||
let _: String = (*rc).clone(); | ||
//~^ error: using `unwrap_or_clone` on an `Arc` or `Rc` is more efficient | ||
|
||
let arc: Arc<String> = Arc::new("foo bar".into()); | ||
let _: Vec<_> = (*arc).clone().split(" ").collect(); | ||
//~^ error: using `unwrap_or_clone` on an `Arc` or `Rc` is more efficient | ||
|
||
let rc: Rc<Vec<u32>> = Rc::new(vec![1, 2, 3]); | ||
let _: Vec<_> = (*rc).clone().iter().map(|x| x + 1).collect(); | ||
//~^ error: using `unwrap_or_clone` on an `Arc` or `Rc` is more efficient | ||
|
||
let _: String = (*Arc::<String>::new("foo".into())).clone(); | ||
//~^ error: using `unwrap_or_clone` on an `Arc` or `Rc` is more efficient | ||
} | ||
|
||
#[clippy::msrv = "1.75"] | ||
fn msrv_check() { | ||
let arc: Arc<String> = Arc::new("foo".into()); | ||
let _: String = (*arc).clone(); | ||
// Should not lint | ||
|
||
let rc: Rc<String> = Rc::new("foo".into()); | ||
let _: String = (*rc).clone(); | ||
// Should not lint | ||
} |
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,35 @@ | ||
error: using `unwrap_or_clone` on an `Arc` or `Rc` is more efficient | ||
--> tests/ui/clone_on_arc_or_rc.rs:8:21 | ||
| | ||
LL | let _: String = (*arc).clone(); | ||
| ^^^^^^^^^^^^^^ help: try: `std::sync::Arc::unwrap_or_clone(arc)` | ||
| | ||
= note: `-D clippy::clone-on-arc-or-rc` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::clone_on_arc_or_rc)]` | ||
|
||
error: using `unwrap_or_clone` on an `Arc` or `Rc` is more efficient | ||
--> tests/ui/clone_on_arc_or_rc.rs:12:21 | ||
| | ||
LL | let _: String = (*rc).clone(); | ||
| ^^^^^^^^^^^^^ help: try: `std::rc::Rc::unwrap_or_clone(rc)` | ||
|
||
error: using `unwrap_or_clone` on an `Arc` or `Rc` is more efficient | ||
--> tests/ui/clone_on_arc_or_rc.rs:16:21 | ||
| | ||
LL | let _: Vec<_> = (*arc).clone().split(" ").collect(); | ||
| ^^^^^^^^^^^^^^ help: try: `std::sync::Arc::unwrap_or_clone(arc)` | ||
|
||
error: using `unwrap_or_clone` on an `Arc` or `Rc` is more efficient | ||
--> tests/ui/clone_on_arc_or_rc.rs:20:21 | ||
| | ||
LL | let _: Vec<_> = (*rc).clone().iter().map(|x| x + 1).collect(); | ||
| ^^^^^^^^^^^^^ help: try: `std::rc::Rc::unwrap_or_clone(rc)` | ||
|
||
error: using `unwrap_or_clone` on an `Arc` or `Rc` is more efficient | ||
--> tests/ui/clone_on_arc_or_rc.rs:23:21 | ||
| | ||
LL | let _: String = (*Arc::<String>::new("foo".into())).clone(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::sync::Arc::unwrap_or_clone(Arc::<String>::new("foo".into()))` | ||
|
||
error: aborting due to 5 previous errors | ||
|