-
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 new lint for
repeat().take()
that can be replaced with `repea…
- Loading branch information
Showing
16 changed files
with
190 additions
and
22 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,43 @@ | ||
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::{expr_use_ctxt, fn_def_id, is_trait_method, std_or_core}; | ||
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<'tcx>( | ||
cx: &LateContext<'tcx>, | ||
expr: &'tcx Expr<'tcx>, | ||
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) | ||
&& !expr_use_ctxt(cx, expr).is_ty_unified | ||
&& let Some(std_or_core) = std_or_core(cx) | ||
{ | ||
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_or_core}::iter::repeat_n({}, {})", | ||
snippet_with_context(cx, repeat_arg.span, expr.span.ctxt(), "..", &mut app).0, | ||
snippet(cx, take_arg.span, "..") | ||
), | ||
app, | ||
); | ||
} | ||
} |
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,30 @@ | ||
#![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); | ||
} | ||
|
||
mod foo_lib { | ||
pub fn iter() -> std::iter::Take<std::iter::Repeat<&'static [u8]>> { | ||
todo!() | ||
} | ||
} | ||
|
||
fn foo() { | ||
let _ = match 1 { | ||
1 => foo_lib::iter(), | ||
// Shouldn't lint because `external_lib::iter` doesn't return `std::iter::RepeatN`. | ||
2 => std::iter::repeat([1, 2].as_slice()).take(2), | ||
_ => todo!(), | ||
}; | ||
} |
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,30 @@ | ||
#![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); | ||
} | ||
|
||
mod foo_lib { | ||
pub fn iter() -> std::iter::Take<std::iter::Repeat<&'static [u8]>> { | ||
todo!() | ||
} | ||
} | ||
|
||
fn foo() { | ||
let _ = match 1 { | ||
1 => foo_lib::iter(), | ||
// Shouldn't lint because `external_lib::iter` doesn't return `std::iter::RepeatN`. | ||
2 => std::iter::repeat([1, 2].as_slice()).take(2), | ||
_ => todo!(), | ||
}; | ||
} |
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
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