Repository navigation
|
I'd like to have all absolute module usages declared via imports. Are there any existing lints that can do this? I've tried Suppose that this is my whole file: pub fn get_mime_type(ext: &str) -> Option<mime::Mime> {
if ext == "html" {
Some(mime::TEXT_HTML)
} else {
None
}
}I want Clippy to complain until the absolute module is explicitly imported: use mime;
pub fn get_mime_type(ext: &str) -> Option<mime::Mime> {
if ext == "html" {
Some(mime::TEXT_HTML)
} else {
None
}
}[1] absolute_paths with Instead it requires all children to be imported individually (not something I want to force): use mime::Mime;
use mime::TEXT_HTML;
pub fn get_mime_type(ext: &str) -> Option<Mime> {
if ext == "html" {
Some(TEXT_HTML)
} else {
None
}
} |
Replies: 3 comments 3 replies
|
I would consider the |
|
If you are looking for an automated tool to refactor existing absolute/qualified paths into top-level DemoBefore: fn handle_data() -> Result<(), std::io::Error> {
let now = std::time::Instant::now();
let code = std::io::ErrorKind::NotFound;
Ok(())
}After running use std::io::{Error, ErrorKind};
use std::time::Instant;
fn handle_data() -> Result<(), Error> {
let now = Instant::now();
let code = ErrorKind::NotFound;
Ok(())
}Installationcargo install fixrsUsage# Preview
fixrs -n
# In-place rewrite and auto rustfmt
fixrsIt parses your code via |
|
Short answer: there is currently no stock Clippy lint that enforces the "root crate import as a module alias" style you describe. The Options to get what you want:
#[allow(clippy::absolute_paths)]
pub fn get_mime_type(ext: &str) -> Option<mime::Mime> { /* ... */ }If
# naive version; add your crate list for precision
if grep -rnE '\b(mime|tokio|serde)::' src/; then exit 1; fi
Recommendation: combine |
The Ruby on Rails thing is not a pattern that works in Rust. If you don't import something, you can only access it through the full path, which is
crate::[modules::]*Item.Back in Rust 2015, if you want to use something from an extern crate
A, you had to writeextern crate A;. Rust 2018 got rid of this restriction and made it more convenient to use something from an extern crate. What you're asking is pretty much a lint that adds this back withusestatements.I don't think this lint would get a lot of use. But I'd accept it as an
restrictionlint, if you were to open a PR for it :)