Move create_disallowed_map to clippy_config

This commit is contained in:
Philipp Krones 2024-11-15 19:28:20 +01:00
parent 4460db0850
commit 81483d4a6c
No known key found for this signature in database
GPG key ID: 1CA0DF2AF59D68A5
8 changed files with 24 additions and 19 deletions

View file

@ -2,10 +2,12 @@
name = "clippy_config"
version = "0.1.84"
edition = "2021"
publish = false
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
clippy_utils = { path = "../clippy_utils" }
itertools = "0.12"
serde = { version = "1.0", features = ["derive"] }
toml = "0.7.3"

View file

@ -14,6 +14,8 @@
)]
extern crate rustc_errors;
extern crate rustc_hir;
extern crate rustc_middle;
extern crate rustc_session;
extern crate rustc_span;

View file

@ -1,3 +1,6 @@
use clippy_utils::def_path_def_ids;
use rustc_hir::def_id::DefIdMap;
use rustc_middle::ty::TyCtxt;
use serde::de::{self, Deserializer, Visitor};
use serde::{Deserialize, Serialize, ser};
use std::collections::HashMap;
@ -31,6 +34,18 @@ impl DisallowedPath {
}
}
/// Creates a map of disallowed items to the reason they were disallowed.
pub fn create_disallowed_map(
tcx: TyCtxt<'_>,
disallowed: &'static [DisallowedPath],
) -> DefIdMap<(&'static str, Option<&'static str>)> {
disallowed
.iter()
.map(|x| (x.path(), x.path().split("::").collect::<Vec<_>>(), x.reason()))
.flat_map(|(name, path, reason)| def_path_def_ids(tcx, &path).map(move |id| (id, (name, reason))))
.collect()
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub enum MatchLintBehaviour {
AllTypes,

View file

@ -1,6 +1,7 @@
use clippy_config::Conf;
use clippy_config::types::create_disallowed_map;
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::{create_disallowed_map, match_def_path, paths};
use clippy_utils::{match_def_path, paths};
use rustc_hir as hir;
use rustc_hir::def_id::{DefId, DefIdMap};
use rustc_lint::{LateContext, LateLintPass};

View file

@ -1,5 +1,5 @@
use clippy_config::Conf;
use clippy_utils::create_disallowed_map;
use clippy_config::types::create_disallowed_map;
use clippy_utils::diagnostics::{span_lint_and_then, span_lint_hir_and_then};
use clippy_utils::macros::macro_backtrace;
use rustc_data_structures::fx::FxHashSet;

View file

@ -1,5 +1,5 @@
use clippy_config::Conf;
use clippy_utils::create_disallowed_map;
use clippy_config::types::create_disallowed_map;
use clippy_utils::diagnostics::span_lint_and_then;
use rustc_hir::def::{CtorKind, DefKind, Res};
use rustc_hir::def_id::DefIdMap;

View file

@ -2,10 +2,8 @@
name = "clippy_utils"
version = "0.1.84"
edition = "2021"
publish = false
[dependencies]
clippy_config = { path = "../clippy_config" }
arrayvec = { version = "0.7", default-features = false }
itertools = "0.12"
# FIXME(f16_f128): remove when no longer needed for parsing

View file

@ -91,7 +91,6 @@ use std::hash::BuildHasherDefault;
use std::iter::{once, repeat};
use std::sync::{Mutex, MutexGuard, OnceLock};
use clippy_config::types::DisallowedPath;
use itertools::Itertools;
use rustc_ast::ast::{self, LitKind, RangeLimits};
use rustc_data_structures::fx::FxHashMap;
@ -99,7 +98,7 @@ use rustc_data_structures::packed::Pu128;
use rustc_data_structures::unhash::UnhashMap;
use rustc_hir::LangItem::{OptionNone, OptionSome, ResultErr, ResultOk};
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LOCAL_CRATE, LocalDefId, LocalModDefId};
use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE, LocalDefId, LocalModDefId};
use rustc_hir::definitions::{DefPath, DefPathData};
use rustc_hir::hir_id::{HirIdMap, HirIdSet};
use rustc_hir::intravisit::{FnKind, Visitor, walk_expr};
@ -750,18 +749,6 @@ pub fn def_path_def_ids(tcx: TyCtxt<'_>, path: &[&str]) -> impl Iterator<Item =
def_path_res(tcx, path).into_iter().filter_map(|res| res.opt_def_id())
}
/// Creates a map of disallowed items to the reason they were disallowed.
pub fn create_disallowed_map(
tcx: TyCtxt<'_>,
disallowed: &'static [DisallowedPath],
) -> DefIdMap<(&'static str, Option<&'static str>)> {
disallowed
.iter()
.map(|x| (x.path(), x.path().split("::").collect::<Vec<_>>(), x.reason()))
.flat_map(|(name, path, reason)| def_path_def_ids(tcx, &path).map(move |id| (id, (name, reason))))
.collect()
}
/// Convenience function to get the `DefId` of a trait by path.
/// It could be a trait or trait alias.
///