Rust-clippy: Warn on `push_str` with a single-character string literal

Created on 8 Aug 2020  路  5Comments  路  Source: rust-lang/rust-clippy

What it does

Warns when using push_str with a single-character string literal, and push with a char would work fine.

Categories (optional)

  • Kind: clippy::style and clippy::perf (maybe clippy::pedantic?)

What is the advantage of the recommended code over the original code?

  • It's more obvious what's going on; after all, you're not trying to push a string, you're trying to push a character
  • It should be more performant and reduce binary size to push a char, since it's just a number and will be inlined, than to push a &'static str, which must be stored in the binary and accessed via a pointer

Drawbacks

None.

Example

let mut string = String::new();
string.push_str("R");

Could be written as:

let mut string = String::new();
string.push('R');
L-enhancement L-lint good-first-issue

Most helpful comment

@rustbot modify labels: -A-style

Okay, seems to work, just had to add rustbot with write permissions to this repo.

All 5 comments

@rustbot modify labels to +A-style +A-performance

Hmm, rustbot's not responding...

@rustbot modify labels: -A-style

Okay, seems to work, just had to add rustbot with write permissions to this repo.

Correct me if I'm wrong, but this lint does not seem to working on the latest nightly. With this code:

// src/main.rs

fn main() {
    let mut string = String::new();
    string.push_str("R");
}

When I run nightly clippy, I don't get any warnings:

$ cargo +nightly clippy
    Checking clippy-issue-5875 v0.1.0 (~/clippy-issue-5875)
    Finished dev [unoptimized + debuginfo] target(s) in 0.11s

Output of cargo clippy --version:

clippy 0.0.212 (04488afe3 2020-08-24)

@camelid if I'm not mistaken, the clippy component is built now from the rust-lang/rust repo copy and not from this repo. The latest sync was merged yesterday (see here), so I think it should be available on the next nightly.

cc @flip1995 for any imprecision with the component release process, I don't want to lie to anyone :)

Was this page helpful?
0 / 5 - 0 ratings