-
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.
add a lint for redundant repeat().take()
- Loading branch information
1 parent
0f9cc8d
commit 33a5b21
Showing
13 changed files
with
147 additions
and
16 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,32 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::source::snippet; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Expr, ExprKind, QPath}; | ||
use rustc_lint::LateContext; | ||
|
||
use super::REDUNDANT_REPEAT_TAKE; | ||
|
||
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, repeat_expr: &Expr<'_>, take_arg: &Expr<'_>) { | ||
if !expr.span.from_expansion() | ||
&& let ExprKind::Call(inner, repeat_args) = repeat_expr.kind | ||
&& repeat_args.len() == 1 | ||
&& let ExprKind::Path(ref qpath) = inner.kind | ||
&& let QPath::Resolved(_, path) = qpath | ||
&& path.segments.len() == 1 | ||
&& path.segments[0].ident.name.as_str() == "repeat" | ||
{ | ||
span_lint_and_sugg( | ||
cx, | ||
REDUNDANT_REPEAT_TAKE, | ||
expr.span, | ||
"redundant `repeat().take()`", | ||
"consider using `repeat_n()` instead", | ||
format!( | ||
"std::iter::repeat_n({}, {})", | ||
snippet(cx, repeat_args[0].span, ""), | ||
snippet(cx, take_arg.span, "") | ||
), | ||
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
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,13 @@ | ||
#![warn(clippy::redundant_repeat_take)] | ||
|
||
use std::iter::repeat; | ||
|
||
fn main() { | ||
let _ = std::iter::repeat_n(10, 3); | ||
|
||
let _ = std::iter::repeat_n(String::from("foo"), 4); | ||
|
||
for value in std::iter::repeat(5).take(3) {} | ||
|
||
let _: Vec<_> = std::iter::repeat(String::from("bar")).take(10).collect(); | ||
} |
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,13 @@ | ||
#![warn(clippy::redundant_repeat_take)] | ||
|
||
use std::iter::repeat; | ||
|
||
fn main() { | ||
let _ = repeat(10).take(3); | ||
|
||
let _ = repeat(String::from("foo")).take(4); | ||
|
||
for value in std::iter::repeat(5).take(3) {} | ||
|
||
let _: Vec<_> = std::iter::repeat(String::from("bar")).take(10).collect(); | ||
} |
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: redundant `repeat().take()` | ||
--> tests/ui/redundant_repeat_take.rs:6:13 | ||
| | ||
LL | let _ = repeat(10).take(3); | ||
| ^^^^^^^^^^^^^^^^^^ help: consider using `repeat_n()` instead: `std::iter::repeat_n(10, 3)` | ||
| | ||
= note: `-D clippy::redundant-repeat-take` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::redundant_repeat_take)]` | ||
|
||
error: redundant `repeat().take()` | ||
--> tests/ui/redundant_repeat_take.rs:8:13 | ||
| | ||
LL | let _ = repeat(String::from("foo")).take(4); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `repeat_n()` instead: `std::iter::repeat_n(String::from("foo"), 4)` | ||
|
||
error: aborting due to 2 previous errors | ||
|
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
#![allow(clippy::redundant_repeat_take)] | ||
|
||
//@no-rustfix | ||
use std::iter::repeat; | ||
fn main() { | ||
|
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