forked from rust-lang/rust
-
-
Notifications
You must be signed in to change notification settings - Fork 2
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
1e0a3ff
commit 0462666
Showing
8 changed files
with
151 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::is_trait_method; | ||
use clippy_utils::ty::{get_iterator_item_ty, is_copy}; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::Expr; | ||
use rustc_lint::LateContext; | ||
use rustc_middle::ty; | ||
use rustc_span::{sym, Span}; | ||
|
||
use super::CLONED_INSTEAD_OF_COPIED; | ||
|
||
pub fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, span: Span) { | ||
let recv_ty = cx.typeck_results().expr_ty_adjusted(recv); | ||
let inner_ty = match recv_ty.kind() { | ||
// `Option<T>` -> `T` | ||
ty::Adt(adt, subst) if cx.tcx.is_diagnostic_item(sym::option_type, adt.did) => subst.type_at(0), | ||
_ if is_trait_method(cx, expr, sym::Iterator) => match get_iterator_item_ty(cx, recv_ty) { | ||
// <T as Iterator>::Item | ||
Some(ty) => ty, | ||
_ => return, | ||
}, | ||
_ => return, | ||
}; | ||
match inner_ty.kind() { | ||
// &T where T: Copy | ||
ty::Ref(_, ty, _) if is_copy(cx, ty) => {}, | ||
_ => return, | ||
}; | ||
span_lint_and_sugg( | ||
cx, | ||
CLONED_INSTEAD_OF_COPIED, | ||
span, | ||
"used `cloned` where `copied` could be used instead", | ||
"try", | ||
"copied".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
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,15 @@ | ||
// run-rustfix | ||
#![warn(clippy::cloned_instead_of_copied)] | ||
|
||
fn main() { | ||
// yay | ||
let _ = [1].iter().copied(); | ||
let _ = vec!["hi"].iter().copied(); | ||
let _ = Some(&1).copied(); | ||
let _ = Box::new([1].iter()).copied(); | ||
let _ = Box::new(Some(&1)).copied(); | ||
|
||
// nay | ||
let _ = [String::new()].iter().cloned(); | ||
let _ = Some(&String::new()).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,15 @@ | ||
// run-rustfix | ||
#![warn(clippy::cloned_instead_of_copied)] | ||
|
||
fn main() { | ||
// yay | ||
let _ = [1].iter().cloned(); | ||
let _ = vec!["hi"].iter().cloned(); | ||
let _ = Some(&1).cloned(); | ||
let _ = Box::new([1].iter()).cloned(); | ||
let _ = Box::new(Some(&1)).cloned(); | ||
|
||
// nay | ||
let _ = [String::new()].iter().cloned(); | ||
let _ = Some(&String::new()).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,34 @@ | ||
error: used `cloned` where `copied` could be used instead | ||
--> $DIR/cloned_instead_of_copied.rs:6:24 | ||
| | ||
LL | let _ = [1].iter().cloned(); | ||
| ^^^^^^ help: try: `copied` | ||
| | ||
= note: `-D clippy::cloned-instead-of-copied` implied by `-D warnings` | ||
|
||
error: used `cloned` where `copied` could be used instead | ||
--> $DIR/cloned_instead_of_copied.rs:7:31 | ||
| | ||
LL | let _ = vec!["hi"].iter().cloned(); | ||
| ^^^^^^ help: try: `copied` | ||
|
||
error: used `cloned` where `copied` could be used instead | ||
--> $DIR/cloned_instead_of_copied.rs:8:22 | ||
| | ||
LL | let _ = Some(&1).cloned(); | ||
| ^^^^^^ help: try: `copied` | ||
|
||
error: used `cloned` where `copied` could be used instead | ||
--> $DIR/cloned_instead_of_copied.rs:9:34 | ||
| | ||
LL | let _ = Box::new([1].iter()).cloned(); | ||
| ^^^^^^ help: try: `copied` | ||
|
||
error: used `cloned` where `copied` could be used instead | ||
--> $DIR/cloned_instead_of_copied.rs:10:32 | ||
| | ||
LL | let _ = Box::new(Some(&1)).cloned(); | ||
| ^^^^^^ help: try: `copied` | ||
|
||
error: aborting due to 5 previous errors | ||
|