-
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.
- Encourage iter.next() rather than iter.nth(0), which is less readable
- Loading branch information
1 parent
578e712
commit feaf179
Showing
7 changed files
with
90 additions
and
2 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,57 @@ | ||
use crate::utils::span_lint_and_sugg; | ||
|
||
use rustc::declare_lint_pass; | ||
use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass}; | ||
use rustc_errors::Applicability; | ||
use rustc_session::declare_tool_lint; | ||
use syntax::ast::{Expr, ExprKind, PathSegment}; | ||
|
||
declare_clippy_lint! { | ||
/// **What it does:** Checks for the use of `iter.nth(0)`. | ||
/// | ||
/// **Why is this bad?** `iter.nth(0)` is unnecessary, and `iter.next()` | ||
/// is more readable. | ||
/// | ||
/// **Known problems:** None. | ||
/// | ||
/// **Example:** | ||
/// | ||
/// ```rust,ignore | ||
/// // Bad | ||
/// let mut s = HashSet::new(); | ||
/// s.insert(1); | ||
/// let x = s.iter().nth(0); | ||
/// | ||
/// // Good | ||
/// let mut s = HashSet::new(); | ||
/// s.insert(1); | ||
/// let x = s.iter().next(); | ||
/// ``` | ||
pub ITER_NTH_ZERO, | ||
style, | ||
"replace `iter.nth(0)` with `iter.next()`" | ||
} | ||
|
||
declare_lint_pass!(IterNthZero => [ITER_NTH_ZERO]); | ||
|
||
impl EarlyLintPass for IterNthZero { | ||
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) { | ||
if let ExprKind::MethodCall(PathSegment { ident, .. }, args) = &expr.kind { | ||
if ident.name.as_str() == "nth" { | ||
if let ExprKind::Lit(literal) = &args[1].kind { | ||
if literal.token.symbol.as_str() == "0" { | ||
span_lint_and_sugg( | ||
cx, | ||
ITER_NTH_ZERO, | ||
expr.span, | ||
"calling `iter.nth(0)` is redundant", | ||
"try", | ||
"using `iter.next()` instead".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
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,8 @@ | ||
#![warn(clippy::iter_nth_zero)] | ||
use std::collections::HashSet; | ||
|
||
fn main() { | ||
let mut s = HashSet::new(); | ||
s.insert(1); | ||
let x = s.iter().nth(0); | ||
} |
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,10 @@ | ||
error: calling `iter.nth(0)` is redundant | ||
--> $DIR/iter_nth_zero.rs:7:13 | ||
| | ||
LL | let x = s.iter().nth(0); | ||
| ^^^^^^^^^^^^^^^ help: try: `using `iter.next()` instead` | ||
| | ||
= note: `-D clippy::iter-nth-zero` implied by `-D warnings` | ||
|
||
error: aborting due to previous error | ||
|