Clippy lints / Pedantic
Clippy lint · Pedantic · checked with Clippy 0.1.99
clippy::manual_string_new: empty String is being created manually
What it meansChecks for usage of "" to create a String, such as "".to_string(), "".to_owned(), String::from("") and others. It is in the pedantic group and off by default.
What you see
warning: empty String is being created manually --> main.rs:3:9 | 3 | let a = "".to_string(); | ^^^^^^^^^^^^^^ help: consider using: `String::new()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#manual_string_new = note: `-W clippy::manual-string-new` implied by `-W clippy::pedantic`
Real output of Clippy 0.1.99 (Rust 1.99) on the example below. Names and line numbers in yours will differ.
Does it matter?
Different ways of creating an empty string makes your code less standardized, which can be confusing.
Stricter lints with some false positives. Off by default; opt in with clippy::pedantic.
How to fix it
Clippy can fix this one for you: run cargo clippy --fix (add --allow-dirty if you have uncommitted changes). Review the diff before you commit.
Flagged
let a = "".to_string();
let b: String = "".into();
Write this instead
let a = String::new();
let b = String::new();
Turn it off, or on
It is off by default. To use it, set it to warn or deny:
Whole crate (Cargo.toml)
[lints.clippy]
manual_string_new = "warn"In code
#![warn(clippy::manual_string_new)]One run
cargo clippy -- -W clippy::manual-string-newThe [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.
Clippy's own documentation
What it does
Checks for usage of "" to create a String, such as "".to_string(), "".to_owned(),
String::from("") and others.
Why is this bad?
Different ways of creating an empty string makes your code less standardized, which can be confusing.
Example
let a = "".to_string();
let b: String = "".into();
Use instead:
let a = String::new();
let b = String::new();
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- too_many_linesChecks for functions with a large amount of lines
- missing_errors_docChecks the doc comments of publicly visible functions that return a Result type and warns if there is no # Errors section
- must_use_candidateChecks for public functions that have no #[must_use] attribute, but return something not already marked must-use, have no mutable arg and mutate no statics
- manual_is_variant_andChecks for usage of option.map(f).unwrap_or_default() and result.map(f).unwrap_or_default() where f is a function or closure that returns the bool type. Also checks for equality comparisons like option.map(f) == Some(true) and result.map(f) == Ok(true), as well as result.ok().is_some_and(f)
- manual_let_elseWarn of cases where let...else could be used
- manual_midpointChecks for manual implementation of midpoint
- many_single_char_names7 bindings with single-character names in scope
- map_unwrap_orChecks for usage of option.map(_).unwrap_or(_) or option.map(_).unwrap_or_else(_) or result.map(_).unwrap_or_else(_)
- match_boolChecks for matches where match expression is a bool
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::too_many_linesChecks for functions with a large amount of linesRead it next →Did this answer your question?