rust-lang / rust-lang/rust-clippy

new lint: unneccessary usage of String::from() involving string constants

Open
#2,449 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint L-perf
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

let length = String::from("some constant").len();

can be replaced by

let length = "some constant".len();

The second one will actually emit less code:
String::from().len()

  lea rsi, [rip + str.0]
  lea rdi, [rbp - 64]
  mov edx, 13
  call <alloc::string::String as core::convert::From<&'a str>>::from@PLT
  mov rax, qword ptr [rbp - 48]
  mov qword ptr [rbp - 16], rax
  mov rsi, qword ptr [rbp - 56]

.len()

  sub rsp, 96
  mov qword ptr [rbp - 8], 13

according to godbolt rust/ 1.23.0 with opt-level=3

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 by reproducing the reported examples with Rust 1.23.0 and compare the generated code, using the issue's godbolt reference as the baseline. Done means a Clippy lint identifies String::from() around string constants and suggests using the string literal directly, with coverage for the shown cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
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.