-
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.
Auto merge of #7265 - Jarcho:manual_str_repeat, r=giraffate
Add lint `manual_str_repeat` fixes: #7260 There's a similar function for slices. Should this be renamed to include it, or should that be a separate lint? If we are going to have them as one lint a better name will be needed. `manual_repeat` isn't exactly clear as it's replacing a call to `iter::repeat`. changelog: Add new lint `manual_str_repeat`
- Loading branch information
Showing
12 changed files
with
359 additions
and
10 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,99 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::source::{snippet_with_applicability, snippet_with_context}; | ||
use clippy_utils::sugg::Sugg; | ||
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::LitKind; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Expr, ExprKind, LangItem}; | ||
use rustc_lint::LateContext; | ||
use rustc_middle::ty::{self, Ty, TyS}; | ||
use rustc_span::symbol::sym; | ||
use std::borrow::Cow; | ||
|
||
use super::MANUAL_STR_REPEAT; | ||
|
||
enum RepeatKind { | ||
String, | ||
Char(char), | ||
} | ||
|
||
fn get_ty_param(ty: Ty<'_>) -> Option<Ty<'_>> { | ||
if let ty::Adt(_, subs) = ty.kind() { | ||
subs.types().next() | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
fn parse_repeat_arg(cx: &LateContext<'_>, e: &Expr<'_>) -> Option<RepeatKind> { | ||
if let ExprKind::Lit(lit) = &e.kind { | ||
match lit.node { | ||
LitKind::Str(..) => Some(RepeatKind::String), | ||
LitKind::Char(c) => Some(RepeatKind::Char(c)), | ||
_ => 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) && get_ty_param(ty).map_or(false, TyS::is_str)) | ||
|| (match_type(cx, ty, &paths::COW) && get_ty_param(ty).map_or(false, TyS::is_str)) | ||
{ | ||
Some(RepeatKind::String) | ||
} else { | ||
let ty = ty.peel_refs(); | ||
(ty.is_str() || is_type_diagnostic_item(cx, ty, sym::string_type)).then(|| RepeatKind::String) | ||
} | ||
} | ||
} | ||
|
||
pub(super) fn check( | ||
cx: &LateContext<'_>, | ||
collect_expr: &Expr<'_>, | ||
take_expr: &Expr<'_>, | ||
take_self_arg: &Expr<'_>, | ||
take_arg: &Expr<'_>, | ||
) { | ||
if_chain! { | ||
if let ExprKind::Call(repeat_fn, [repeat_arg]) = take_self_arg.kind; | ||
if is_expr_path_def_path(cx, repeat_fn, &paths::ITER_REPEAT); | ||
if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(collect_expr), sym::string_type); | ||
if let Some(collect_id) = cx.typeck_results().type_dependent_def_id(collect_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); | ||
let ctxt = collect_expr.span.ctxt(); | ||
if ctxt == take_expr.span.ctxt(); | ||
if ctxt == take_self_arg.span.ctxt(); | ||
then { | ||
let mut app = Applicability::MachineApplicable; | ||
let count_snip = snippet_with_context(cx, take_arg.span, ctxt, "..", &mut app).0; | ||
|
||
let val_str = match repeat_kind { | ||
RepeatKind::Char(_) if repeat_arg.span.ctxt() != ctxt => return, | ||
RepeatKind::Char('\'') => r#""'""#.into(), | ||
RepeatKind::Char('"') => r#""\"""#.into(), | ||
RepeatKind::Char(_) => | ||
match snippet_with_applicability(cx, repeat_arg.span, "..", &mut app) { | ||
Cow::Owned(s) => Cow::Owned(format!("\"{}\"", &s[1..s.len() - 1])), | ||
s @ Cow::Borrowed(_) => s, | ||
}, | ||
RepeatKind::String => | ||
Sugg::hir_with_context(cx, repeat_arg, ctxt, "..", &mut app).maybe_par().to_string().into(), | ||
}; | ||
|
||
span_lint_and_sugg( | ||
cx, | ||
MANUAL_STR_REPEAT, | ||
collect_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
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,66 @@ | ||
// run-rustfix | ||
|
||
#![feature(custom_inner_attributes)] | ||
#![warn(clippy::manual_str_repeat)] | ||
|
||
use std::borrow::Cow; | ||
use std::iter::{repeat, FromIterator}; | ||
|
||
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 }}; | ||
} | ||
// FIXME: macro args are fine | ||
let _: String = repeat(m!("test")).take(m!(count)).collect(); | ||
|
||
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(); | ||
|
||
let x: Box<str> = Box::from("test"); | ||
let _: String = x.repeat(count); | ||
|
||
#[derive(Clone)] | ||
struct S; | ||
impl FromIterator<Box<S>> for String { | ||
fn from_iter<T: IntoIterator<Item = Box<S>>>(_: T) -> Self { | ||
Self::new() | ||
} | ||
} | ||
// Don't lint, wrong box type | ||
let _: String = repeat(Box::new(S)).take(count).collect(); | ||
|
||
let _: String = Cow::Borrowed("test").repeat(count); | ||
|
||
let x = "x".to_owned(); | ||
let _: String = x.repeat(count); | ||
|
||
let x = 'x'; | ||
// Don't lint, not char literal | ||
let _: String = repeat(x).take(count).collect(); | ||
} | ||
|
||
fn _msrv_1_15() { | ||
#![clippy::msrv = "1.15"] | ||
// `str::repeat` was stabilized in 1.16. Do not lint this | ||
let _: String = std::iter::repeat("test").take(10).collect(); | ||
} | ||
|
||
fn _msrv_1_16() { | ||
#![clippy::msrv = "1.16"] | ||
let _: String = "test".repeat(10); | ||
} |
Oops, something went wrong.