-
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
10 changed files
with
234 additions
and
5 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
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,87 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::source::snippet_with_context; | ||
use clippy_utils::ty::{is_type_diagnostic_item, is_type_lang_item, match_type}; | ||
use clippy_utils::{is_expr_path_def_path, paths}; | ||
use if_chain::if_chain; | ||
use rustc_ast::util::parser::PREC_POSTFIX; | ||
use rustc_ast::LitKind; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Expr, ExprKind, LangItem}; | ||
use rustc_lint::LateContext; | ||
use rustc_span::symbol::{sym, Symbol}; | ||
|
||
use super::MANUAL_STR_REPEAT; | ||
|
||
enum RepeatKind { | ||
Str, | ||
String, | ||
Char, | ||
} | ||
|
||
fn parse_repeat_arg(cx: &LateContext<'_>, e: &Expr<'_>) -> Option<RepeatKind> { | ||
if let ExprKind::Lit(lit) = &e.kind { | ||
match lit.node { | ||
LitKind::Str(..) => Some(RepeatKind::Str), | ||
LitKind::Char(_) => Some(RepeatKind::Char), | ||
_ => None, | ||
} | ||
} else { | ||
let ty = cx.typeck_results().expr_ty(e); | ||
if is_type_diagnostic_item(cx, ty, sym::string_type) | ||
|| is_type_lang_item(cx, ty, LangItem::OwnedBox) | ||
|| match_type(cx, ty, &paths::COW) | ||
{ | ||
Some(RepeatKind::String) | ||
} else { | ||
let ty = ty.peel_refs(); | ||
(ty.is_str() || is_type_diagnostic_item(cx, ty, sym::string_type)).then(|| RepeatKind::Str) | ||
} | ||
} | ||
} | ||
|
||
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, method_name: Symbol, args: &[Expr<'_>]) { | ||
let ctxt = expr.span.ctxt(); | ||
if_chain! { | ||
if let [take_expr] = args; | ||
if method_name.as_str() == "collect"; | ||
if let ExprKind::MethodCall(take_name, _, [repeat_expr, take_arg], _) = take_expr.kind; | ||
if take_name.ident.as_str() == "take"; | ||
if let ExprKind::Call(repeat_fn, [repeat_arg]) = repeat_expr.kind; | ||
if is_expr_path_def_path(cx, repeat_fn, &paths::ITER_REPEAT); | ||
if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(expr), sym::string_type); | ||
if let Some(collect_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id); | ||
if let Some(take_id) = cx.typeck_results().type_dependent_def_id(take_expr.hir_id); | ||
if let Some(iter_trait_id) = cx.tcx.get_diagnostic_item(sym::Iterator); | ||
if cx.tcx.trait_of_item(collect_id) == Some(iter_trait_id); | ||
if cx.tcx.trait_of_item(take_id) == Some(iter_trait_id); | ||
if let Some(repeat_kind) = parse_repeat_arg(cx, repeat_arg); | ||
if ctxt == take_expr.span.ctxt(); | ||
if ctxt == repeat_expr.span.ctxt(); | ||
then { | ||
let mut app = Applicability::MachineApplicable; | ||
let (val_snip, val_is_mac) = snippet_with_context(cx, repeat_arg.span, ctxt, "..", &mut app); | ||
let count_snip = snippet_with_context(cx, take_arg.span, ctxt, "..", &mut app).0; | ||
|
||
let val_str = match repeat_kind { | ||
RepeatKind::String => format!("(&{})", val_snip), | ||
RepeatKind::Str if !val_is_mac && repeat_arg.precedence().order() < PREC_POSTFIX => { | ||
format!("({})", val_snip) | ||
}, | ||
RepeatKind::Str => val_snip.into(), | ||
RepeatKind::Char if val_snip == r#"'"'"# => r#""\"""#.into(), | ||
RepeatKind::Char if val_snip == r#"'\''"# => r#""'""#.into(), | ||
RepeatKind::Char => format!("\"{}\"", &val_snip[1..val_snip.len() - 1]), | ||
}; | ||
|
||
span_lint_and_sugg( | ||
cx, | ||
MANUAL_STR_REPEAT, | ||
expr.span, | ||
"manual implementation of `str::repeat` using iterators", | ||
"try this", | ||
format!("{}.repeat({})", val_str, count_snip), | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// run-rustfix | ||
|
||
#![warn(clippy::manual_str_repeat)] | ||
|
||
use std::iter::repeat; | ||
|
||
fn main() { | ||
let _: String = "test".repeat(10); | ||
let _: String = "x".repeat(10); | ||
let _: String = "'".repeat(10); | ||
let _: String = "\"".repeat(10); | ||
|
||
let x = "test"; | ||
let count = 10; | ||
let _ = x.repeat(count + 2); | ||
|
||
macro_rules! m { | ||
($e:expr) => {{ $e }}; | ||
} | ||
|
||
let _: String = m!("test").repeat(m!(count)); | ||
|
||
let x = &x; | ||
let _: String = (*x).repeat(count); | ||
|
||
macro_rules! repeat_m { | ||
($e:expr) => {{ repeat($e) }}; | ||
} | ||
// Don't lint, repeat is from a macro. | ||
let _: String = repeat_m!("test").take(count).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,31 @@ | ||
// run-rustfix | ||
|
||
#![warn(clippy::manual_str_repeat)] | ||
|
||
use std::iter::repeat; | ||
|
||
fn main() { | ||
let _: String = std::iter::repeat("test").take(10).collect(); | ||
let _: String = std::iter::repeat('x').take(10).collect(); | ||
let _: String = std::iter::repeat('\'').take(10).collect(); | ||
let _: String = std::iter::repeat('"').take(10).collect(); | ||
|
||
let x = "test"; | ||
let count = 10; | ||
let _ = repeat(x).take(count + 2).collect::<String>(); | ||
|
||
macro_rules! m { | ||
($e:expr) => {{ $e }}; | ||
} | ||
|
||
let _: String = repeat(m!("test")).take(m!(count)).collect(); | ||
|
||
let x = &x; | ||
let _: String = repeat(*x).take(count).collect(); | ||
|
||
macro_rules! repeat_m { | ||
($e:expr) => {{ repeat($e) }}; | ||
} | ||
// Don't lint, repeat is from a macro. | ||
let _: String = repeat_m!("test").take(count).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,46 @@ | ||
error: manual implementation of `str::repeat` using iterators | ||
--> $DIR/manual_str_repeat.rs:8:21 | ||
| | ||
LL | let _: String = std::iter::repeat("test").take(10).collect(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `"test".repeat(10)` | ||
| | ||
= note: `-D clippy::manual-str-repeat` implied by `-D warnings` | ||
|
||
error: manual implementation of `str::repeat` using iterators | ||
--> $DIR/manual_str_repeat.rs:9:21 | ||
| | ||
LL | let _: String = std::iter::repeat('x').take(10).collect(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `"x".repeat(10)` | ||
|
||
error: manual implementation of `str::repeat` using iterators | ||
--> $DIR/manual_str_repeat.rs:10:21 | ||
| | ||
LL | let _: String = std::iter::repeat('/'').take(10).collect(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `"'".repeat(10)` | ||
|
||
error: manual implementation of `str::repeat` using iterators | ||
--> $DIR/manual_str_repeat.rs:11:21 | ||
| | ||
LL | let _: String = std::iter::repeat('"').take(10).collect(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `"/"".repeat(10)` | ||
|
||
error: manual implementation of `str::repeat` using iterators | ||
--> $DIR/manual_str_repeat.rs:15:13 | ||
| | ||
LL | let _ = repeat(x).take(count + 2).collect::<String>(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `x.repeat(count + 2)` | ||
|
||
error: manual implementation of `str::repeat` using iterators | ||
--> $DIR/manual_str_repeat.rs:21:21 | ||
| | ||
LL | let _: String = repeat(m!("test")).take(m!(count)).collect(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `m!("test").repeat(m!(count))` | ||
|
||
error: manual implementation of `str::repeat` using iterators | ||
--> $DIR/manual_str_repeat.rs:24:21 | ||
| | ||
LL | let _: String = repeat(*x).take(count).collect(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `(*x).repeat(count)` | ||
|
||
error: aborting due to 7 previous errors | ||
|