Auto merge of #117772 - surechen:for_117448, r=petrochenkov

Tracking import use types for more accurate redundant import checking

fixes #117448

By tracking import use types to check whether it is scope uses or the other situations like module-relative uses,  we can do more accurate redundant import checking.

For example unnecessary imports in std::prelude that can be eliminated:

```rust
use std::option::Option::Some;//~ WARNING the item `Some` is imported redundantly
use std::option::Option::None; //~ WARNING the item `None` is imported redundantly
```
This commit is contained in:
bors 2024-02-18 13:56:07 +00:00
commit 8c9ca771c9
19 changed files with 20 additions and 34 deletions

View file

@ -6,7 +6,7 @@ use itertools::Itertools;
use crate::{ use crate::{
hir::{ hir::{
Array, BindingAnnotation, BindingId, CaptureBy, ClosureKind, Literal, LiteralOrConst, Array, BindingAnnotation, CaptureBy, ClosureKind, Literal, LiteralOrConst,
Movability, Statement, Movability, Statement,
}, },
pretty::{print_generic_args, print_path, print_type_ref}, pretty::{print_generic_args, print_path, print_type_ref},

View file

@ -3,7 +3,7 @@
use std::{fmt, hash::BuildHasherDefault}; use std::{fmt, hash::BuildHasherDefault};
use base_db::CrateId; use base_db::CrateId;
use fst::{self, raw::IndexedValue, Automaton, Streamer}; use fst::{raw::IndexedValue, Automaton, Streamer};
use hir_expand::name::Name; use hir_expand::name::Name;
use indexmap::IndexMap; use indexmap::IndexMap;
use itertools::Itertools; use itertools::Itertools;

View file

@ -2,12 +2,12 @@
use std::collections::hash_map::Entry; use std::collections::hash_map::Entry;
use hir_expand::{ast_id_map::AstIdMap, span_map::SpanMapRef, HirFileId}; use hir_expand::{ast_id_map::AstIdMap, span_map::SpanMapRef};
use syntax::ast::{self, HasModuleItem, HasTypeBounds, IsString}; use syntax::ast::{HasModuleItem, HasTypeBounds, IsString};
use crate::{ use crate::{
generics::{GenericParams, GenericParamsCollector, TypeParamData, TypeParamProvenance}, generics::{GenericParamsCollector, TypeParamData, TypeParamProvenance},
type_ref::{LifetimeRef, TraitBoundModifier, TraitRef}, type_ref::{LifetimeRef, TraitBoundModifier},
LocalLifetimeParamId, LocalTypeOrConstParamId, LocalLifetimeParamId, LocalTypeOrConstParamId,
}; };

View file

@ -1,13 +1,12 @@
//! `ItemTree` debug printer. //! `ItemTree` debug printer.
use std::fmt::{self, Write}; use std::fmt::Write;
use span::ErasedFileAstId; use span::ErasedFileAstId;
use crate::{ use crate::{
generics::{TypeOrConstParamData, WherePredicate, WherePredicateTypeTarget}, generics::{WherePredicate, WherePredicateTypeTarget},
pretty::{print_path, print_type_bounds, print_type_ref}, pretty::{print_path, print_type_bounds, print_type_ref},
visibility::RawVisibility,
}; };
use super::*; use super::*;

View file

@ -57,7 +57,7 @@ pub mod proc_macro;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
use std::{cmp::Ord, ops::Deref}; use std::ops::Deref;
use base_db::{CrateId, Edition, FileId}; use base_db::{CrateId, Edition, FileId};
use hir_expand::{ use hir_expand::{

View file

@ -2,7 +2,7 @@
//! //!
//! Originates from `rustc_hir::pat_util` //! Originates from `rustc_hir::pat_util`
use std::iter::{Enumerate, ExactSizeIterator}; use std::iter::Enumerate;
pub(crate) struct EnumerateAndAdjust<I> { pub(crate) struct EnumerateAndAdjust<I> {
enumerate: Enumerate<I>, enumerate: Enumerate<I>,

View file

@ -4,11 +4,7 @@
use std::cmp; use std::cmp;
use chalk_ir::TyKind; use chalk_ir::TyKind;
use hir_def::{ use hir_def::builtin_type::{BuiltinInt, BuiltinUint};
builtin_type::{BuiltinInt, BuiltinUint},
resolver::HasResolver,
};
use hir_expand::mod_path::ModPath;
use super::*; use super::*;

View file

@ -1,6 +1,6 @@
//! This module generates a polymorphic MIR from a hir body //! This module generates a polymorphic MIR from a hir body
use std::{fmt::Write, iter, mem}; use std::{fmt::Write, mem};
use base_db::{salsa::Cycle, FileId}; use base_db::{salsa::Cycle, FileId};
use chalk_ir::{BoundVar, ConstData, DebruijnIndex, TyKind}; use chalk_ir::{BoundVar, ConstData, DebruijnIndex, TyKind};
@ -14,23 +14,19 @@ use hir_def::{
lang_item::{LangItem, LangItemTarget}, lang_item::{LangItem, LangItemTarget},
path::Path, path::Path,
resolver::{resolver_for_expr, HasResolver, ResolveValueResult, ValueNs}, resolver::{resolver_for_expr, HasResolver, ResolveValueResult, ValueNs},
AdtId, DefWithBodyId, EnumVariantId, GeneralConstId, HasModule, ItemContainerId, LocalFieldId, AdtId, EnumVariantId, GeneralConstId, HasModule, ItemContainerId, LocalFieldId,
Lookup, TraitId, TupleId, TypeOrConstParamId, Lookup, TraitId, TupleId, TypeOrConstParamId,
}; };
use hir_expand::name::Name; use hir_expand::name::Name;
use la_arena::ArenaMap;
use rustc_hash::FxHashMap;
use syntax::TextRange; use syntax::TextRange;
use triomphe::Arc; use triomphe::Arc;
use crate::{ use crate::{
consteval::ConstEvalError, consteval::ConstEvalError,
db::{HirDatabase, InternedClosure}, db::InternedClosure,
display::HirDisplay,
infer::{CaptureKind, CapturedItem, TypeMismatch}, infer::{CaptureKind, CapturedItem, TypeMismatch},
inhabitedness::is_ty_uninhabited_from, inhabitedness::is_ty_uninhabited_from,
layout::LayoutError, layout::LayoutError,
mapping::ToChalk,
static_lifetime, static_lifetime,
traits::FnTrait, traits::FnTrait,
utils::{generics, ClosureSubst}, utils::{generics, ClosureSubst},

View file

@ -1,6 +1,6 @@
//! MIR lowering for patterns //! MIR lowering for patterns
use hir_def::{hir::LiteralOrConst, resolver::HasResolver, AssocItemId}; use hir_def::AssocItemId;
use crate::BindingMode; use crate::BindingMode;

View file

@ -1,4 +1,4 @@
use hir::{self, HasCrate, HasVisibility}; use hir::{HasCrate, HasVisibility};
use ide_db::{path_transform::PathTransform, FxHashSet}; use ide_db::{path_transform::PathTransform, FxHashSet};
use syntax::{ use syntax::{
ast::{ ast::{

View file

@ -31,7 +31,7 @@
//! } //! }
//! ``` //! ```
use hir::{self, HasAttrs}; use hir::HasAttrs;
use ide_db::{ use ide_db::{
documentation::HasDocs, path_transform::PathTransform, documentation::HasDocs, path_transform::PathTransform,
syntax_helpers::insert_whitespace_into_node, traits::get_missing_assoc_items, SymbolKind, syntax_helpers::insert_whitespace_into_node, traits::get_missing_assoc_items, SymbolKind,

View file

@ -31,7 +31,7 @@ use base_db::{
salsa::{self, ParallelDatabase}, salsa::{self, ParallelDatabase},
SourceDatabaseExt, SourceRootId, Upcast, SourceDatabaseExt, SourceRootId, Upcast,
}; };
use fst::{self, raw::IndexedValue, Automaton, Streamer}; use fst::{raw::IndexedValue, Automaton, Streamer};
use hir::{ use hir::{
db::HirDatabase, db::HirDatabase,
import_map::{AssocSearchMode, SearchMode}, import_map::{AssocSearchMode, SearchMode},

View file

@ -4,7 +4,7 @@ use std::mem;
use cfg::{CfgAtom, CfgExpr}; use cfg::{CfgAtom, CfgExpr};
use ide::{Cancellable, CrateId, FileId, RunnableKind, TestId}; use ide::{Cancellable, CrateId, FileId, RunnableKind, TestId};
use project_model::{self, CargoFeatures, ManifestPath, TargetKind}; use project_model::{CargoFeatures, ManifestPath, TargetKind};
use rustc_hash::FxHashSet; use rustc_hash::FxHashSet;
use vfs::AbsPathBuf; use vfs::AbsPathBuf;

View file

@ -13,7 +13,7 @@ use ide_db::{
LineIndexDatabase, LineIndexDatabase,
}; };
use load_cargo::{load_workspace, LoadCargoConfig, ProcMacroServerChoice}; use load_cargo::{load_workspace, LoadCargoConfig, ProcMacroServerChoice};
use lsp_types::{self, lsif}; use lsp_types::lsif;
use project_model::{CargoConfig, ProjectManifest, ProjectWorkspace, RustLibSource}; use project_model::{CargoConfig, ProjectManifest, ProjectWorkspace, RustLibSource};
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use vfs::{AbsPathBuf, Vfs}; use vfs::{AbsPathBuf, Vfs};

View file

@ -1,5 +1,4 @@
//! //!
use std::{convert::TryFrom, iter::FromIterator};
use crate::parenthesized::Parenthesized; use crate::parenthesized::Parenthesized;
use heck::ToUpperCamelCase; use heck::ToUpperCamelCase;

View file

@ -5,7 +5,6 @@ use crate::durability::Durability;
use crate::plumbing::QueryStorageOps; use crate::plumbing::QueryStorageOps;
use crate::Query; use crate::Query;
use crate::QueryTable; use crate::QueryTable;
use std::iter::FromIterator;
/// Additional methods on queries that can be used to "peek into" /// Additional methods on queries that can be used to "peek into"
/// their current state. These methods are meant for debugging and /// their current state. These methods are meant for debugging and

View file

@ -13,7 +13,6 @@ use crate::Runtime;
use crate::{Database, DatabaseKeyIndex, QueryDb, Revision}; use crate::{Database, DatabaseKeyIndex, QueryDb, Revision};
use parking_lot::RwLock; use parking_lot::RwLock;
use std::borrow::Borrow; use std::borrow::Borrow;
use std::convert::TryFrom;
use std::hash::Hash; use std::hash::Hash;
use std::marker::PhantomData; use std::marker::PhantomData;
use triomphe::Arc; use triomphe::Arc;

View file

@ -14,7 +14,6 @@ use crate::Runtime;
use crate::{DatabaseKeyIndex, QueryDb}; use crate::{DatabaseKeyIndex, QueryDb};
use indexmap::map::Entry; use indexmap::map::Entry;
use parking_lot::RwLock; use parking_lot::RwLock;
use std::convert::TryFrom;
use std::iter; use std::iter;
use tracing::debug; use tracing::debug;

View file

@ -13,7 +13,6 @@ use crate::{Database, DatabaseKeyIndex, QueryDb};
use parking_lot::RwLock; use parking_lot::RwLock;
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use std::collections::hash_map::Entry; use std::collections::hash_map::Entry;
use std::convert::From;
use std::fmt::Debug; use std::fmt::Debug;
use std::hash::Hash; use std::hash::Hash;
use triomphe::Arc; use triomphe::Arc;