diff options
| author | Alex Macleod <alex@macleod.io> | 2025-05-04 13:44:16 +0000 |
|---|---|---|
| committer | Josh Stone <jistone@redhat.com> | 2025-06-20 10:01:19 -0700 |
| commit | 9fec7f0c948b5e14efcfde9f28b38a3195a52552 (patch) | |
| tree | f475ff91ac85e59384db0dd885dfe490f39fc8c6 | |
| parent | c0900b891992c105cd780fe31c8021b8f1926b82 (diff) | |
| download | rust-9fec7f0c948b5e14efcfde9f28b38a3195a52552.tar.gz rust-9fec7f0c948b5e14efcfde9f28b38a3195a52552.zip | |
Don't warn about unloaded crates (rust-lang/rust-clippy#14733)
Fixes https://github.com/rust-lang/rust-clippy/pull/14397#issuecomment-2848328221 r? @samueltardieu changelog: Don't warn about clippy.toml disallowed paths for crates that were not loaded
4 files changed, 41 insertions, 4 deletions
diff --git a/src/tools/clippy/clippy_config/src/types.rs b/src/tools/clippy/clippy_config/src/types.rs index 5949eaca7bc..70cf0915bb6 100644 --- a/src/tools/clippy/clippy_config/src/types.rs +++ b/src/tools/clippy/clippy_config/src/types.rs @@ -4,7 +4,7 @@ use rustc_hir::PrimTy; use rustc_hir::def::{DefKind, Res}; use rustc_hir::def_id::DefIdMap; use rustc_middle::ty::TyCtxt; -use rustc_span::Span; +use rustc_span::{Span, Symbol}; use serde::de::{self, Deserializer, Visitor}; use serde::{Deserialize, Serialize, ser}; use std::collections::HashMap; @@ -145,7 +145,8 @@ pub fn create_disallowed_map<const REPLACEMENT_ALLOWED: bool>( FxHashMap::default(); for disallowed_path in disallowed_paths { let path = disallowed_path.path(); - let mut resolutions = clippy_utils::def_path_res(tcx, &path.split("::").collect::<Vec<_>>()); + let path_split = path.split("::").collect::<Vec<_>>(); + let mut resolutions = clippy_utils::def_path_res(tcx, &path_split); let mut found_def_id = None; let mut found_prim_ty = false; @@ -160,8 +161,12 @@ pub fn create_disallowed_map<const REPLACEMENT_ALLOWED: bool>( }, _ => false, }); - - if resolutions.is_empty() { + if resolutions.is_empty() + // Don't warn about unloaded crates: + // https://github.com/rust-lang/rust-clippy/pull/14397#issuecomment-2848328221 + && (path_split.len() < 2 + || !clippy_utils::find_crates(tcx, Symbol::intern(path_split[0])).is_empty()) + { let span = disallowed_path.span(); if let Some(def_id) = found_def_id { diff --git a/src/tools/clippy/tests/ui-toml/toml_unloaded_crate/clippy.toml b/src/tools/clippy/tests/ui-toml/toml_unloaded_crate/clippy.toml new file mode 100644 index 00000000000..e664256d2a2 --- /dev/null +++ b/src/tools/clippy/tests/ui-toml/toml_unloaded_crate/clippy.toml @@ -0,0 +1,10 @@ +# The first two `disallowed-methods` paths should generate warnings, but the third should not. + +[[disallowed-methods]] +path = "regex::Regex::new_" + +[[disallowed-methods]] +path = "regex::Regex_::new" + +[[disallowed-methods]] +path = "regex_::Regex::new" diff --git a/src/tools/clippy/tests/ui-toml/toml_unloaded_crate/conf_unloaded_crate.rs b/src/tools/clippy/tests/ui-toml/toml_unloaded_crate/conf_unloaded_crate.rs new file mode 100644 index 00000000000..2dbc5eca32e --- /dev/null +++ b/src/tools/clippy/tests/ui-toml/toml_unloaded_crate/conf_unloaded_crate.rs @@ -0,0 +1,6 @@ +//@error-in-other-file: `regex::Regex::new_` does not refer to an existing function +//@error-in-other-file: `regex::Regex_::new` does not refer to an existing function + +extern crate regex; + +fn main() {} diff --git a/src/tools/clippy/tests/ui-toml/toml_unloaded_crate/conf_unloaded_crate.stderr b/src/tools/clippy/tests/ui-toml/toml_unloaded_crate/conf_unloaded_crate.stderr new file mode 100644 index 00000000000..5d28e5fa970 --- /dev/null +++ b/src/tools/clippy/tests/ui-toml/toml_unloaded_crate/conf_unloaded_crate.stderr @@ -0,0 +1,16 @@ +warning: `regex::Regex::new_` does not refer to an existing function + --> $DIR/tests/ui-toml/toml_unloaded_crate/clippy.toml:3:1 + | +LL | / [[disallowed-methods]] +LL | | path = "regex::Regex::new_" + | |___________________________^ + +warning: `regex::Regex_::new` does not refer to an existing function + --> $DIR/tests/ui-toml/toml_unloaded_crate/clippy.toml:6:1 + | +LL | / [[disallowed-methods]] +LL | | path = "regex::Regex_::new" + | |___________________________^ + +warning: 2 warnings emitted + |
