Clippy lints / Restriction
Clippy lint · Restriction · checked with Clippy 0.1.99
clippy::redundant_type_annotations: redundant type annotation
What it meansWarns about needless / redundant type annotations. It is in the restriction group and off by default.
What you see
warning: redundant type annotation --> main.rs:3:1 | 3 | let foo: String = String::new(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#redundant_type_annotations = note: requested on the command line with `-W clippy::redundant-type-annotations`
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?
Code without type annotations is shorter and in most cases more idiomatic and easier to modify.
Lints that forbid language features for specific codebases (no unwrap, no indexing...). Off by default; pick them one by one, never the whole group.
How to fix it
Clippy does not fix this one automatically. Change the code by hand:
Flagged
let foo: String = String::new();
Write this instead
let foo = 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]
redundant_type_annotations = "warn"In code
#![warn(clippy::redundant_type_annotations)]One run
cargo clippy -- -W clippy::redundant-type-annotationsThe [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
Warns about needless / redundant type annotations.
Why restrict this?
Code without type annotations is shorter and in most cases more idiomatic and easier to modify.
Limitations
This lint doesn’t support:
- Generics
- Refs returned from anything else than a
MethodCall - Complex types (tuples, arrays, etc…)
Pathto anything else than a primitive type.
Example
let foo: String = String::new();
Use instead:
let foo = String::new();
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- unwrap_usedChecks for .unwrap() or .unwrap_err() calls on Results and .unwrap() call on Options
- module_name_repetitionsDetects public item names that are prefixed or suffixed by the containing public module’s name
- expect_usedChecks for .expect() or .expect_err() calls on Results and .expect() call on Options
- rc_bufferChecks for Rc<T> and Arc<T> when T is a mutable buffer type such as String or Vec
- rc_mutexChecks for Rc<Mutex<T>>
- redundant_test_prefixChecks for test functions (functions annotated with #[test]) that are prefixed with test_ which is redundant
- ref_patternsusage of ref pattern
- renamed_function_paramsrenamed function parameter of trait impl
- rest_pat_in_fully_bound_structsChecks for unnecessary ‘..’ pattern binding on struct when all fields are explicitly matched
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::unwrap_usedChecks for .unwrap() or .unwrap_err() calls on Results and .unwrap() call on OptionsRead it next →Did this answer your question?