-
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
8 changed files
with
156 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
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::{ | ||
consts::{constant, Constant}, | ||
diagnostics::span_lint_and_sugg, | ||
is_from_proc_macro, is_trait_method, | ||
}; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::Expr; | ||
use rustc_lint::LateContext; | ||
use rustc_span::sym; | ||
|
||
use super::ITER_SKIP_ZERO; | ||
|
||
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, arg_expr: &Expr<'_>) { | ||
if !expr.span.from_expansion() | ||
&& is_trait_method(cx, expr, sym::Iterator) | ||
&& let Some(arg) = constant(cx, cx.typeck_results(), arg_expr).and_then(|constant| { | ||
if let Constant::Int(arg) = constant { | ||
Some(arg) | ||
} else { | ||
None | ||
} | ||
}) | ||
&& arg == 0 | ||
&& !is_from_proc_macro(cx, expr) | ||
{ | ||
span_lint_and_sugg( | ||
cx, | ||
ITER_SKIP_ZERO, | ||
arg_expr.span, | ||
"usage of `.skip(0)`", | ||
"if you meant to skip the first element, use", | ||
"1".to_owned(), | ||
Applicability::MaybeIncorrect, | ||
); | ||
} | ||
} |
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,25 @@ | ||
//@run-rustfix | ||
//@aux-build:proc_macros.rs:proc-macro | ||
#![allow(clippy::useless_vec, unused)] | ||
#![warn(clippy::iter_skip_zero)] | ||
|
||
#[macro_use] | ||
extern crate proc_macros; | ||
|
||
use std::iter::once; | ||
|
||
fn main() { | ||
let _ = [1, 2, 3].iter().skip(1); | ||
let _ = vec![1, 2, 3].iter().skip(1); | ||
let _ = once([1, 2, 3]).skip(1); | ||
let _ = vec![1, 2, 3].iter().chain([1, 2, 3].iter().skip(1)).skip(1); | ||
// Don't lint | ||
let _ = [1, 2, 3].iter().skip(1); | ||
let _ = vec![1, 2, 3].iter().skip(1); | ||
external! { | ||
let _ = [1, 2, 3].iter().skip(0); | ||
} | ||
with_span! { | ||
let _ = [1, 2, 3].iter().skip(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,25 @@ | ||
//@run-rustfix | ||
//@aux-build:proc_macros.rs:proc-macro | ||
#![allow(clippy::useless_vec, unused)] | ||
#![warn(clippy::iter_skip_zero)] | ||
|
||
#[macro_use] | ||
extern crate proc_macros; | ||
|
||
use std::iter::once; | ||
|
||
fn main() { | ||
let _ = [1, 2, 3].iter().skip(0); | ||
let _ = vec![1, 2, 3].iter().skip(0); | ||
let _ = once([1, 2, 3]).skip(0); | ||
let _ = vec![1, 2, 3].iter().chain([1, 2, 3].iter().skip(0)).skip(0); | ||
// Don't lint | ||
let _ = [1, 2, 3].iter().skip(1); | ||
let _ = vec![1, 2, 3].iter().skip(1); | ||
external! { | ||
let _ = [1, 2, 3].iter().skip(0); | ||
} | ||
with_span! { | ||
let _ = [1, 2, 3].iter().skip(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,34 @@ | ||
error: usage of `.skip(0)` | ||
--> $DIR/iter_skip_zero.rs:12:35 | ||
| | ||
LL | let _ = [1, 2, 3].iter().skip(0); | ||
| ^ help: if you meant to skip the first element, try: `1` | ||
| | ||
= note: `-D clippy::iter-skip-zero` implied by `-D warnings` | ||
|
||
error: usage of `.skip(0)` | ||
--> $DIR/iter_skip_zero.rs:13:39 | ||
| | ||
LL | let _ = vec![1, 2, 3].iter().skip(0); | ||
| ^ help: if you meant to skip the first element, try: `1` | ||
|
||
error: usage of `.skip(0)` | ||
--> $DIR/iter_skip_zero.rs:14:34 | ||
| | ||
LL | let _ = once([1, 2, 3]).skip(0); | ||
| ^ help: if you meant to skip the first element, try: `1` | ||
|
||
error: usage of `.skip(0)` | ||
--> $DIR/iter_skip_zero.rs:15:71 | ||
| | ||
LL | let _ = vec![1, 2, 3].iter().chain([1, 2, 3].iter().skip(0)).skip(0); | ||
| ^ help: if you meant to skip the first element, try: `1` | ||
|
||
error: usage of `.skip(0)` | ||
--> $DIR/iter_skip_zero.rs:15:62 | ||
| | ||
LL | let _ = vec![1, 2, 3].iter().chain([1, 2, 3].iter().skip(0)).skip(0); | ||
| ^ help: if you meant to skip the first element, try: `1` | ||
|
||
error: aborting due to 5 previous errors | ||
|