-
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
1 parent
0f9cc8d
commit 8e422ea
Showing
15 changed files
with
159 additions
and
19 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,35 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::msrvs::{self, Msrv}; | ||
use clippy_utils::source::{snippet, snippet_with_context}; | ||
use clippy_utils::{fn_def_id, is_trait_method}; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Expr, ExprKind}; | ||
use rustc_lint::LateContext; | ||
use rustc_span::sym; | ||
|
||
use super::MANUAL_REPEAT_N; | ||
|
||
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, repeat_expr: &Expr<'_>, take_arg: &Expr<'_>, msrv: &Msrv) { | ||
if msrv.meets(msrvs::REPEAT_N) | ||
&& !expr.span.from_expansion() | ||
&& is_trait_method(cx, expr, sym::Iterator) | ||
&& let ExprKind::Call(_, [repeat_arg]) = repeat_expr.kind | ||
&& let Some(def_id) = fn_def_id(cx, repeat_expr) | ||
&& cx.tcx.is_diagnostic_item(sym::iter_repeat, def_id) | ||
{ | ||
let mut app = Applicability::MachineApplicable; | ||
span_lint_and_sugg( | ||
cx, | ||
MANUAL_REPEAT_N, | ||
expr.span, | ||
"this `.repeat().take()` can be written more concisely", | ||
"consider using `repeat_n()` instead", | ||
format!( | ||
"std::iter::repeat_n({}, {})", | ||
snippet_with_context(cx, repeat_arg.span, expr.span.ctxt(), "..", &mut app).0, | ||
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
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,15 @@ | ||
#![warn(clippy::manual_repeat_n)] | ||
|
||
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_n(5, 3) {} | ||
|
||
let _: Vec<_> = std::iter::repeat_n(String::from("bar"), 10).collect(); | ||
|
||
let _ = std::iter::repeat_n(vec![1, 2], 2); | ||
} |
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,15 @@ | ||
#![warn(clippy::manual_repeat_n)] | ||
|
||
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(); | ||
|
||
let _ = repeat(vec![1, 2]).take(2); | ||
} |
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 @@ | ||
error: this `.repeat().take()` can be written more concisely | ||
--> tests/ui/manual_repeat_n.rs:6:13 | ||
| | ||
LL | let _ = repeat(10).take(3); | ||
| ^^^^^^^^^^^^^^^^^^ help: consider using `repeat_n()` instead: `std::iter::repeat_n(10, 3)` | ||
| | ||
= note: `-D clippy::manual-repeat-n` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::manual_repeat_n)]` | ||
|
||
error: this `.repeat().take()` can be written more concisely | ||
--> tests/ui/manual_repeat_n.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: this `.repeat().take()` can be written more concisely | ||
--> tests/ui/manual_repeat_n.rs:10:18 | ||
| | ||
LL | for value in std::iter::repeat(5).take(3) {} | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `repeat_n()` instead: `std::iter::repeat_n(5, 3)` | ||
|
||
error: this `.repeat().take()` can be written more concisely | ||
--> tests/ui/manual_repeat_n.rs:12:21 | ||
| | ||
LL | let _: Vec<_> = std::iter::repeat(String::from("bar")).take(10).collect(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `repeat_n()` instead: `std::iter::repeat_n(String::from("bar"), 10)` | ||
|
||
error: this `.repeat().take()` can be written more concisely | ||
--> tests/ui/manual_repeat_n.rs:14:13 | ||
| | ||
LL | let _ = repeat(vec![1, 2]).take(2); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `repeat_n()` instead: `std::iter::repeat_n(vec![1, 2], 2)` | ||
|
||
error: aborting due to 5 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
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
#![allow(clippy::manual_repeat_n)] | ||
|
||
//@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