rust-lang / rust-lang/rust-clippy

string_add is overly sensitive when a String is constructed purely to be passed as an argument

Open
#9,296 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

The string_add lint triggers in situations where it makes no sense to put a push_str on a separate line because the String's only purpose is to be fed as an argument.

At the very least, I think this deserves to be acknowledged in the docs.

Lint Name

string_add

Reproducer

I tried this code:

return Url::parse(&("https://".to_owned() + word)).ok();

(it's part of an HTML form spam pre-filter that normalizes loose domain names into Url objects with the help of the Public Suffix List so they get properly evaluated when applying spam rules.)

I saw this happen:

warning: you added something to a string. Consider using `String::push_str()` instead
   --> src/lib.rs:210:36
    |
210 |                 return Url::parse(&("https://".to_owned() + word)).ok();
    |                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#string_add

I expected to see this happen:

Nothing.

In the code the reproducer is from, word is an immutable slice of a larger string, so String::insert_str isn't an option either.

Version
rustc 1.62.1 (e092d0b6b 2022-07-16)
binary: rustc
commit-hash: e092d0b6b43f2de967af0887873151bb1c0b18d3
commit-date: 2022-07-16
host: x86_64-unknown-linux-gnu
release: 1.62.1
LLVM version: 14.0.5
Additional Labels

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the string_add lint and the reproducer at src/lib.rs:210, then review the lint guidance linked in the issue. Determine whether this argument-only String construction should be exempted or documented, and validate the chosen behavior against the reproducer.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.