-
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
6 changed files
with
109 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use clippy_utils::diagnostics::{span_lint_and_sugg}; | ||
use clippy_utils::source::snippet; | ||
use clippy_utils::ty::implements_trait; | ||
use rustc_errors::Applicability; | ||
use rustc_hir as hir; | ||
use rustc_lint::LateContext; | ||
use rustc_span::sym; | ||
|
||
use super::CLONED_LAST; | ||
|
||
/// lint use of `cloned().next()` for `Iterators` | ||
pub(super) fn check<'tcx>( | ||
cx: &LateContext<'tcx>, | ||
expr: &'tcx hir::Expr<'_>, | ||
recv: &'tcx hir::Expr<'_>, | ||
) { | ||
// lint if caller of `.filter().next()` is an Iterator | ||
let recv_impls_iterator = cx.tcx.get_diagnostic_item(sym::Iterator).map_or(false, |id| { | ||
implements_trait(cx, cx.typeck_results().expr_ty(recv), id, &[]) | ||
}); | ||
if recv_impls_iterator { | ||
let msg = "called `cloned().last()` on an `Iterator`. It may be more efficient to call | ||
`.last.cloned()` instead"; | ||
let iter_snippet = snippet(cx, recv.span, ".."); | ||
// add note if not multi-line | ||
span_lint_and_sugg( | ||
cx, | ||
CLONED_LAST, | ||
expr.span, | ||
msg, | ||
"try this", | ||
format!("{}.last().cloned()", iter_snippet), | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// run-rustfix | ||
#![warn(clippy::nursery)] | ||
|
||
fn main() { | ||
|
||
#[rustfmt::skip] | ||
let _: Option<String> = vec!["1".to_string(), "2".to_string(), "3".to_string()] | ||
.iter().last().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,9 @@ | ||
// run-rustfix | ||
#![warn(clippy::nursery)] | ||
|
||
fn main() { | ||
|
||
#[rustfmt::skip] | ||
let _: Option<String> = vec!["1".to_string(), "2".to_string(), "3".to_string()] | ||
.iter().cloned().last(); | ||
} |
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,18 @@ | ||
error: called `cloned().last()` on an `Iterator`. It may be more efficient to call | ||
`.last.cloned()` instead | ||
--> $DIR/cloned_last.rs:7:29 | ||
| | ||
LL | let _: Option<String> = vec!["1".to_string(), "2".to_string(), "3".to_string()] | ||
| _____________________________^ | ||
LL | | .iter().cloned().last(); | ||
| |_______________________________^ | ||
| | ||
= note: `-D clippy::cloned-last` implied by `-D warnings` | ||
help: try this | ||
| | ||
LL ~ let _: Option<String> = vec!["1".to_string(), "2".to_string(), "3".to_string()] | ||
LL ~ .iter().last().cloned(); | ||
| | ||
|
||
error: aborting due to previous error | ||
|