diff options
| author | Samuel Moelius <samuel.moelius@trailofbits.com> | 2025-05-04 09:09:39 -0400 |
|---|---|---|
| committer | Samuel Moelius <samuel.moelius@trailofbits.com> | 2025-05-04 09:22:29 -0400 |
| commit | 7b7a9a6ecefed92114062af8a84e6b3fcead6a53 (patch) | |
| tree | 623354e5a79847585feb1dc35204f93db68e7765 | |
| parent | 56f018286b8feeed22284afc14c97aa2600afb88 (diff) | |
| download | rust-7b7a9a6ecefed92114062af8a84e6b3fcead6a53.tar.gz rust-7b7a9a6ecefed92114062af8a84e6b3fcead6a53.zip | |
Don't warn about unloaded crates
Fixes https://github.com/rust-lang/rust-clippy/pull/14397#issuecomment-2848328221
| -rw-r--r-- | clippy_config/src/types.rs | 13 | ||||
| -rw-r--r-- | tests/ui-toml/toml_unloaded_crate/clippy.toml | 10 | ||||
| -rw-r--r-- | tests/ui-toml/toml_unloaded_crate/conf_unloaded_crate.rs | 6 | ||||
| -rw-r--r-- | tests/ui-toml/toml_unloaded_crate/conf_unloaded_crate.stderr | 16 |
4 files changed, 41 insertions, 4 deletions
diff --git a/clippy_config/src/types.rs b/clippy_config/src/types.rs index 5949eaca7bc..70cf0915bb6 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<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/tests/ui-toml/toml_unloaded_crate/clippy.toml b/tests/ui-toml/toml_unloaded_crate/clippy.toml new file mode 100644 index 00000000000..e664256d2a2 --- /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 00000000000..2dbc5eca32e --- /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 00000000000..5d28e5fa970 --- /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 + |
