Skip to content

New lint: adding a char to a String through to_string() #12775

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
jakubdabek opened this issue May 7, 2024 · 2 comments · Fixed by #12915
Closed

New lint: adding a char to a String through to_string() #12775

jakubdabek opened this issue May 7, 2024 · 2 comments · Fixed by #12915
Assignees
Labels
A-lint Area: New lints C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good first issue These issues are a good way to get started with Clippy

Comments

@jakubdabek
Copy link

jakubdabek commented May 7, 2024

What it does

Warns when turning a char into a String before pushing it to another String.

Advantage

  • It's more clear that we're pushing only a single char
  • It doesn't allocate a new String

Drawbacks

No response

Example

let mut s = String::from("...");
let c = 'a';
// or: let c = &'a';
s.push_str(&c.to_string());
// or: s = s + &c.to_string();
// or: s += &c.to_string();

Could be written as:

let mut s = String::from("...");
let c = 'a';
s.push(c);
// or: s.push(*c); for the reference case
@jakubdabek jakubdabek added the A-lint Area: New lints label May 7, 2024
@J-ZhengLi
Copy link
Member

could be an extension of clippy::single_char_add_str, which is a lot easier

@J-ZhengLi J-ZhengLi added good first issue These issues are a good way to get started with Clippy C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages labels May 11, 2024
@belyakov-am
Copy link
Contributor

@rustbot claim

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-lint Area: New lints C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good first issue These issues are a good way to get started with Clippy
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants