Skip to content

Don't warn about unloaded crates #14733

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions clippy_config/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions tests/ui-toml/toml_unloaded_crate/clippy.toml
Original file line number Diff line number Diff line change
@@ -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"
6 changes: 6 additions & 0 deletions tests/ui-toml/toml_unloaded_crate/conf_unloaded_crate.rs
Original file line number Diff line number Diff line change
@@ -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() {}
16 changes: 16 additions & 0 deletions tests/ui-toml/toml_unloaded_crate/conf_unloaded_crate.stderr
Original file line number Diff line number Diff line change
@@ -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