Clippy lints / Restriction
Clippy lint · Restriction · checked with Clippy 0.1.99
clippy::unused_trait_names: importing trait that is only used anonymously
What it meansChecks for use Trait where the Trait is only used for its methods and not referenced by a path directly. It is in the restriction group and off by default.
What you see
warning: importing trait that is only used anonymously --> main.rs:1:15 | 1 | use std::fmt::Write; | ^^^^^ help: use: `Write as _` | = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#unused_trait_names = note: requested on the command line with `-W clippy::unused-trait-names`
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?
Traits imported that aren’t used directly can be imported anonymously with use Trait as _. It is more explicit, avoids polluting the current scope with unused names and can be useful to show which imports are required for traits.
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 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
use std::fmt::Write;
fn main() {
let mut s = String::new();
let _ = write!(s, "hello, world!");
println!("{s}");
}
Write this instead
use std::fmt::Write as _;
fn main() {
let mut s = String::new();
let _ = write!(s, "hello, world!");
println!("{s}");
}
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]
unused_trait_names = "warn"In code
#![warn(clippy::unused_trait_names)]One run
cargo clippy -- -W clippy::unused-trait-namesThe [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.
Settings in clippy.toml
msrv: The minimum rust version that the project supports. Defaults to the rust-version field in Cargo.toml
Clippy's own documentation
What it does
Checks for use Trait where the Trait is only used for its methods and not referenced by a path directly.
Why is this bad?
Traits imported that aren’t used directly can be imported anonymously with use Trait as _.
It is more explicit, avoids polluting the current scope with unused names and can be useful to show which imports are required for traits.
Example
use std::fmt::Write;
fn main() {
let mut s = String::new();
let _ = write!(s, "hello, world!");
println!("{s}");
}
Use instead:
use std::fmt::Write as _;
fn main() {
let mut s = String::new();
let _ = write!(s, "hello, world!");
println!("{s}");
}
Configuration
-
msrv: The minimum rust version that the project supports. Defaults to therust-versionfield inCargo.toml(default:
current version)
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
- unreachableusage of the `unreachable!` macro
- unseparated_literal_suffixinteger type suffix should be separated by an underscore
- unused_result_okChecks for calls to Result::ok() without using the returned Option
- unwrap_in_result`expect` used in a function that returns a `Result`
- use_debugChecks for usage of Debug formatting
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?