diff --git a/clippy_config/src/types.rs b/clippy_config/src/types.rs index 5949eaca7bc1..70cf0915bb6f 100644 --- a/clippy_config/src/types.rs +++ b/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( 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::>()); + let path_split = path.split("::").collect::>(); + 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( }, _ => 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/tests/ui-toml/toml_unloaded_crate/clippy.toml b/tests/ui-toml/toml_unloaded_crate/clippy.toml new file mode 100644 index 000000000000..e664256d2a2f --- /dev/null +++ b/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/tests/ui-toml/toml_unloaded_crate/conf_unloaded_crate.rs b/tests/ui-toml/toml_unloaded_crate/conf_unloaded_crate.rs new file mode 100644 index 000000000000..2dbc5eca32ec --- /dev/null +++ b/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/tests/ui-toml/toml_unloaded_crate/conf_unloaded_crate.stderr b/tests/ui-toml/toml_unloaded_crate/conf_unloaded_crate.stderr new file mode 100644 index 000000000000..5d28e5fa970e --- /dev/null +++ b/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 +