Clippy lints / Restriction
Clippy lint · Restriction · checked with Clippy 0.1.99
clippy::absolute_paths: consider bringing this path into scope with the `use` keyword
What it meansChecks for usage of items through absolute paths, like std::f64::consts::PI. It is in the restriction group and off by default.
What you see
warning: consider bringing this path into scope with the `use` keyword --> main.rs:3:9 | 3 | let x = std::f64::consts::PI; | ^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#absolute_paths = note: requested on the command line with `-W clippy::absolute-paths`
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?
Many codebases have their own style when it comes to importing, but one that is seldom used is using absolute paths everywhere. This is generally considered unidiomatic, and you should add a use statement. The default maximum segments (2) is pretty strict, you may want to increase this in clippy.toml. Note: One exception to this is code from macro expansion - this does not lint such cases, as using absolute paths is the proper way of referencing items in one.
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 x = std::f64::consts::PI;
Write this instead
use std::f64;
use std::f64::consts;
use std::f64::consts::PI;
let x = f64::consts::PI;
let x = consts::PI;
let x = PI;
use std::f64::consts as f64_consts;
let x = f64_consts::PI;
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]
absolute_paths = "warn"In code
#![warn(clippy::absolute_paths)]One run
cargo clippy -- -W clippy::absolute-pathsThe [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
absolute-paths-allowed-crates: Which crates to allow absolute paths fromabsolute-paths-max-segments: The maximum number of segments a path can have before being linted, anything above this will be linted.
Clippy's own documentation
What it does
Checks for usage of items through absolute paths, like std::f64::consts::PI.
Why restrict this?
Many codebases have their own style when it comes to importing, but one that is seldom used
is using absolute paths everywhere. This is generally considered unidiomatic, and you
should add a use statement.
The default maximum segments (2) is pretty strict, you may want to increase this in
clippy.toml.
Note: One exception to this is code from macro expansion - this does not lint such cases, as using absolute paths is the proper way of referencing items in one.
Known issues
There are currently a few cases which are not caught by this lint:
- Macro calls. e.g.
path::to::macro!() - Derive macros. e.g.
#[derive(path::to::macro)] - Attribute macros. e.g.
#[path::to::macro]
Example
let x = std::f64::consts::PI;
Use any of the below instead, or anything else:
use std::f64;
use std::f64::consts;
use std::f64::consts::PI;
let x = f64::consts::PI;
let x = consts::PI;
let x = PI;
use std::f64::consts as f64_consts;
let x = f64_consts::PI;
Configuration
-
absolute-paths-allowed-crates: Which crates to allow absolute paths from(default:
[]) -
absolute-paths-max-segments: The maximum number of segments a path can have before being linted, anything above this will be linted.(default:
2)
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
- alloc_instead_of_coreFinds items imported through alloc when available through core
- allow_attributes#[allow] attribute found
- allow_attributes_without_reason`allow` attribute without specifying a reason
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?