Skip to content

Resolve through local re-exports in lookup_path #14772

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 10, 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
19 changes: 15 additions & 4 deletions clippy_utils/src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use crate::{MaybePath, path_def_id, sym};
use rustc_ast::Mutability;
use rustc_data_structures::fx::FxHashMap;
use rustc_hir::def::Namespace::{MacroNS, TypeNS, ValueNS};
use rustc_hir::def::{DefKind, Namespace};
use rustc_hir::def::{DefKind, Namespace, Res};
use rustc_hir::def_id::{DefId, LOCAL_CRATE, LocalDefId};
use rustc_hir::{ImplItemRef, ItemKind, Node, OwnerId, TraitItemRef};
use rustc_hir::{ImplItemRef, ItemKind, Node, OwnerId, TraitItemRef, UseKind};
use rustc_lint::LateContext;
use rustc_middle::ty::fast_reject::SimplifiedType;
use rustc_middle::ty::{FloatTy, IntTy, Ty, TyCtxt, UintTy};
Expand Down Expand Up @@ -302,8 +302,19 @@ fn local_item_child_by_name(tcx: TyCtxt<'_>, local_id: LocalDefId, ns: PathNS, n

match item_kind {
ItemKind::Mod(_, r#mod) => r#mod.item_ids.iter().find_map(|&item_id| {
let ident = tcx.hir_item(item_id).kind.ident()?;
res(ident, item_id.owner_id)
let item = tcx.hir_item(item_id);
if let ItemKind::Use(path, UseKind::Single(ident)) = item.kind {
if ident.name == name {
path.res
.iter()
.find(|res| ns.matches(res.ns()))
.and_then(Res::opt_def_id)
} else {
None
}
} else {
res(item.kind.ident()?, item_id.owner_id)
}
}),
ItemKind::Impl(r#impl) => r#impl
.items
Expand Down
3 changes: 3 additions & 0 deletions tests/ui-toml/toml_disallowed_methods/clippy.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ disallowed-methods = [
"conf_disallowed_methods::Struct::method",
"conf_disallowed_methods::Trait::provided_method",
"conf_disallowed_methods::Trait::implemented_method",
# re-exports
"conf_disallowed_methods::identity",
"conf_disallowed_methods::renamed",
]
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ extern crate regex;
use futures::stream::{empty, select_all};
use regex::Regex;

use std::convert::identity;
use std::hint::black_box as renamed;

fn local_fn() {}

struct Struct;
Expand Down Expand Up @@ -71,4 +74,9 @@ fn main() {
//~^ disallowed_methods
s.implemented_method();
//~^ disallowed_methods

identity(());
//~^ disallowed_methods
renamed(1);
//~^ disallowed_methods
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: use of a disallowed method `regex::Regex::new`
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:33:14
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:36:14
|
LL | let re = Regex::new(r"ab.*c").unwrap();
| ^^^^^^^^^^
Expand All @@ -8,84 +8,96 @@ LL | let re = Regex::new(r"ab.*c").unwrap();
= help: to override `-D warnings` add `#[allow(clippy::disallowed_methods)]`

error: use of a disallowed method `regex::Regex::is_match`
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:35:8
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:38:8
|
LL | re.is_match("abc");
| ^^^^^^^^
|
= note: no matching allowed

error: use of a disallowed method `std::iter::Iterator::sum`
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:39:14
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:42:14
|
LL | a.iter().sum::<i32>();
| ^^^

error: use of a disallowed method `slice::sort_unstable`
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:42:7
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:45:7
|
LL | a.sort_unstable();
| ^^^^^^^^^^^^^

error: use of a disallowed method `f32::clamp`
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:46:20
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:49:20
|
LL | let _ = 2.0f32.clamp(3.0f32, 4.0f32);
| ^^^^^

error: use of a disallowed method `regex::Regex::new`
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:50:61
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:53:61
|
LL | let indirect: fn(&str) -> Result<Regex, regex::Error> = Regex::new;
| ^^^^^^^^^^

error: use of a disallowed method `f32::clamp`
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:54:28
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:57:28
|
LL | let in_call = Box::new(f32::clamp);
| ^^^^^^^^^^

error: use of a disallowed method `regex::Regex::new`
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:56:53
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:59:53
|
LL | let in_method_call = ["^", "$"].into_iter().map(Regex::new);
| ^^^^^^^^^^

error: use of a disallowed method `futures::stream::select_all`
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:60:31
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:63:31
|
LL | let same_name_as_module = select_all(vec![empty::<()>()]);
| ^^^^^^^^^^

error: use of a disallowed method `conf_disallowed_methods::local_fn`
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:63:5
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:66:5
|
LL | local_fn();
| ^^^^^^^^

error: use of a disallowed method `conf_disallowed_methods::local_mod::f`
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:65:5
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:68:5
|
LL | local_mod::f();
| ^^^^^^^^^^^^

error: use of a disallowed method `conf_disallowed_methods::Struct::method`
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:68:7
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:71:7
|
LL | s.method();
| ^^^^^^

error: use of a disallowed method `conf_disallowed_methods::Trait::provided_method`
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:70:7
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:73:7
|
LL | s.provided_method();
| ^^^^^^^^^^^^^^^

error: use of a disallowed method `conf_disallowed_methods::Trait::implemented_method`
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:72:7
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:75:7
|
LL | s.implemented_method();
| ^^^^^^^^^^^^^^^^^^

error: aborting due to 14 previous errors
error: use of a disallowed method `conf_disallowed_methods::identity`
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:78:5
|
LL | identity(());
| ^^^^^^^^

error: use of a disallowed method `conf_disallowed_methods::renamed`
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:80:5
|
LL | renamed(1);
| ^^^^^^^

error: aborting due to 16 previous errors