-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add lint for calling last() on DoubleEndedIterator
- Loading branch information
Showing
7 changed files
with
160 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,42 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::is_trait_method; | ||
use clippy_utils::ty::implements_trait; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::Expr; | ||
use rustc_lint::LateContext; | ||
use rustc_span::{Span, sym}; | ||
|
||
use super::DOUBLE_ENDED_ITERATOR_LAST; | ||
|
||
pub(super) fn check(cx: &LateContext<'_>, expr: &'_ Expr<'_>, self_expr: &'_ Expr<'_>, call_span: Span) { | ||
let typeck = cx.typeck_results(); | ||
|
||
// Check if the current "last" method is that of the Iterator trait | ||
if !is_trait_method(cx, expr, sym::Iterator) { | ||
return; | ||
} | ||
|
||
// Find id for DoubleEndedIterator trait | ||
let Some(deiter_id) = cx.tcx.get_diagnostic_item(sym::DoubleEndedIterator) else { | ||
return; | ||
}; | ||
|
||
// Find the type of self | ||
let self_type = typeck.expr_ty(self_expr).peel_refs(); | ||
|
||
// Check that the object implements the DoubleEndedIterator trait | ||
if !implements_trait(cx, self_type, deiter_id, &[]) { | ||
return; | ||
} | ||
|
||
// Emit lint | ||
span_lint_and_sugg( | ||
cx, | ||
DOUBLE_ENDED_ITERATOR_LAST, | ||
call_span, | ||
"called `Iterator::last` on a `DoubleEndedIterator`; this will needlessly iterate the entire iterator", | ||
"try", | ||
"next_back()".to_string(), | ||
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,35 @@ | ||
#![warn(clippy::double_ended_iterator_last)] | ||
|
||
// Typical case | ||
pub fn last_arg(s: &str) -> Option<&str> { | ||
s.split(' ').next_back() | ||
} | ||
|
||
fn main() { | ||
// General case | ||
struct DeIterator; | ||
impl Iterator for DeIterator { | ||
type Item = (); | ||
fn next(&mut self) -> Option<Self::Item> { | ||
Some(()) | ||
} | ||
} | ||
impl DoubleEndedIterator for DeIterator { | ||
fn next_back(&mut self) -> Option<Self::Item> { | ||
Some(()) | ||
} | ||
} | ||
let _ = DeIterator.next_back(); | ||
// Should not apply to other methods of Iterator | ||
let _ = DeIterator.count(); | ||
|
||
// Should not apply to simple iterators | ||
struct SimpleIterator; | ||
impl Iterator for SimpleIterator { | ||
type Item = (); | ||
fn next(&mut self) -> Option<Self::Item> { | ||
Some(()) | ||
} | ||
} | ||
let _ = SimpleIterator.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,35 @@ | ||
#![warn(clippy::double_ended_iterator_last)] | ||
|
||
// Typical case | ||
pub fn last_arg(s: &str) -> Option<&str> { | ||
s.split(' ').last() | ||
} | ||
|
||
fn main() { | ||
// General case | ||
struct DeIterator; | ||
impl Iterator for DeIterator { | ||
type Item = (); | ||
fn next(&mut self) -> Option<Self::Item> { | ||
Some(()) | ||
} | ||
} | ||
impl DoubleEndedIterator for DeIterator { | ||
fn next_back(&mut self) -> Option<Self::Item> { | ||
Some(()) | ||
} | ||
} | ||
let _ = DeIterator.last(); | ||
// Should not apply to other methods of Iterator | ||
let _ = DeIterator.count(); | ||
|
||
// Should not apply to simple iterators | ||
struct SimpleIterator; | ||
impl Iterator for SimpleIterator { | ||
type Item = (); | ||
fn next(&mut self) -> Option<Self::Item> { | ||
Some(()) | ||
} | ||
} | ||
let _ = SimpleIterator.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,17 @@ | ||
error: called `Iterator::last` on a `DoubleEndedIterator`; this will needlessly iterate the entire iterator | ||
--> tests/ui/double_ended_iterator_last.rs:5:18 | ||
| | ||
LL | s.split(' ').last() | ||
| ^^^^^^ help: try: `next_back()` | ||
| | ||
= note: `-D clippy::double-ended-iterator-last` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::double_ended_iterator_last)]` | ||
|
||
error: called `Iterator::last` on a `DoubleEndedIterator`; this will needlessly iterate the entire iterator | ||
--> tests/ui/double_ended_iterator_last.rs:22:24 | ||
| | ||
LL | let _ = DeIterator.last(); | ||
| ^^^^^^ help: try: `next_back()` | ||
|
||
error: aborting due to 2 previous errors | ||
|