-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lint against String.repeat(1) #3060
Conversation
clippy_lints/src/strings.rs
Outdated
|
||
/// **What it does:** Checks for usage of `.repeat(1)` since its equivalent to clone() | ||
/// | ||
/// **Why is this bad?** It's more readable. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd say it's the opposite.
clippy_lints/src/strings.rs
Outdated
declare_clippy_lint! { | ||
pub STRING_REPEAT_ONCE, | ||
pedantic, | ||
" using `String.repeat(1)` instead of `String.clone()`" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove white space preceding using
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your contribution and welcome to Clippy! I commented on mostly small things that you could improve.
let a = "this shouldn't work".repeat(1); | ||
let b = String::from("neither should this"); | ||
let c = b.repeat(1); | ||
let d = "this works".repeat(2); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if you use a constant as the argument of repeat:
const N: u32 = 1;
b.repeat(N);
use syntax::ast::LitKind; | ||
use crate::utils::{in_macro, snippet}; | ||
|
||
if let ExprKind::MethodCall(ref path, _, ref args) = e.node { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: you can use the if_chain!()
macro here to avoid the nested if
syntax
/// **What it does:** Checks for usage of `.repeat(1)` since its equivalent to clone() | ||
/// | ||
/// **Why is this bad?** `.repeat(1)` is not very readable. | ||
/// |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
**Known problems:** None.
@@ -169,3 +169,61 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for StringLitAsBytes { | |||
} | |||
} | |||
} | |||
|
|||
/// **What it does:** Checks for usage of `.repeat(1)` since its equivalent to clone() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing backticks around .clone()
Maybe add something like "while clone represents the intention better."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please also include #3060 (comment) in the lint documentation.
|
||
/// **What it does:** Checks for usage of `.repeat(1)` since its equivalent to clone() | ||
/// | ||
/// **Why is this bad?** `.repeat(1)` is not very readable. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here it might also be better to talk about the hidden intention behind the repeat(1)
call.
"Isn't very readable" is a very vague argument for a lint.
@@ -303,6 +303,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) { | |||
reg.register_late_lint_pass(box block_in_if_condition::BlockInIfCondition); | |||
reg.register_late_lint_pass(box unicode::Unicode); | |||
reg.register_late_lint_pass(box strings::StringAdd); | |||
reg.register_late_lint_pass(box strings::StringRepeatOnce); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You also have to add the Lint to the right point group. Running the util/update_lints.py
script should do this for you!
/// ``` | ||
declare_clippy_lint! { | ||
pub STRING_REPEAT_ONCE, | ||
pedantic, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would classify this as a complexity lint. If too many people complain about this lint (which I don't think) we can move this to pedantic later.
Should we perhaps limit this to a few known std types that implement When I make my own type and impl repeat(), we can't really know that struct Hello {
x: i32,
}
impl Hello {
fn repeat(&mut self, number: i32) {
self.x +=number * 3;
}
}
fn main() {
let mut a = Hello { x: 7 };
a.repeat(1);
println!("a: {}", a.x);
} If I tested this correctly I also got a warning for this case. |
@matthiaskrgr Yes good point! Searching in the doc of the std library there are 3 types where it makes sense:
This should cover all the cases: Playground Did I miss something? @einashaddad You practically just have to add a match over the type of |
Thanks for the review @flip1995, @matthiaskrgr & @mati865. Sounds good, I'll address. |
ping @einashaddad |
Ping from triage @einashaddad. Do you want to continue working on this? |
Ping from triage @einashaddad: It looks like this PR hasn't received any updates in a while, so I'm closing it per our new guidelines. Thank you for your contributions and please feel free to re-open in the future. |
@rustbot label -S-inactive-closed |
No description provided.