Skip to content

Commit f60807d

Browse files
authored
Resolve through local re-exports in lookup_path (#14772)
Fixes #14767 A long standing issue revealed by #14397 changelog: none
2 parents 5262ab2 + e4d82ae commit f60807d

File tree

4 files changed

+53
-19
lines changed

4 files changed

+53
-19
lines changed

clippy_utils/src/paths.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ use crate::{MaybePath, path_def_id, sym};
88
use rustc_ast::Mutability;
99
use rustc_data_structures::fx::FxHashMap;
1010
use rustc_hir::def::Namespace::{MacroNS, TypeNS, ValueNS};
11-
use rustc_hir::def::{DefKind, Namespace};
11+
use rustc_hir::def::{DefKind, Namespace, Res};
1212
use rustc_hir::def_id::{DefId, LOCAL_CRATE, LocalDefId};
13-
use rustc_hir::{ImplItemRef, ItemKind, Node, OwnerId, TraitItemRef};
13+
use rustc_hir::{ImplItemRef, ItemKind, Node, OwnerId, TraitItemRef, UseKind};
1414
use rustc_lint::LateContext;
1515
use rustc_middle::ty::fast_reject::SimplifiedType;
1616
use rustc_middle::ty::{FloatTy, IntTy, Ty, TyCtxt, UintTy};
@@ -302,8 +302,19 @@ fn local_item_child_by_name(tcx: TyCtxt<'_>, local_id: LocalDefId, ns: PathNS, n
302302

303303
match item_kind {
304304
ItemKind::Mod(_, r#mod) => r#mod.item_ids.iter().find_map(|&item_id| {
305-
let ident = tcx.hir_item(item_id).kind.ident()?;
306-
res(ident, item_id.owner_id)
305+
let item = tcx.hir_item(item_id);
306+
if let ItemKind::Use(path, UseKind::Single(ident)) = item.kind {
307+
if ident.name == name {
308+
path.res
309+
.iter()
310+
.find(|res| ns.matches(res.ns()))
311+
.and_then(Res::opt_def_id)
312+
} else {
313+
None
314+
}
315+
} else {
316+
res(item.kind.ident()?, item_id.owner_id)
317+
}
307318
}),
308319
ItemKind::Impl(r#impl) => r#impl
309320
.items

tests/ui-toml/toml_disallowed_methods/clippy.toml

+3
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,7 @@ disallowed-methods = [
1414
"conf_disallowed_methods::Struct::method",
1515
"conf_disallowed_methods::Trait::provided_method",
1616
"conf_disallowed_methods::Trait::implemented_method",
17+
# re-exports
18+
"conf_disallowed_methods::identity",
19+
"conf_disallowed_methods::renamed",
1720
]

tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs

+8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ extern crate regex;
88
use futures::stream::{empty, select_all};
99
use regex::Regex;
1010

11+
use std::convert::identity;
12+
use std::hint::black_box as renamed;
13+
1114
fn local_fn() {}
1215

1316
struct Struct;
@@ -71,4 +74,9 @@ fn main() {
7174
//~^ disallowed_methods
7275
s.implemented_method();
7376
//~^ disallowed_methods
77+
78+
identity(());
79+
//~^ disallowed_methods
80+
renamed(1);
81+
//~^ disallowed_methods
7482
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: use of a disallowed method `regex::Regex::new`
2-
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:33:14
2+
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:36:14
33
|
44
LL | let re = Regex::new(r"ab.*c").unwrap();
55
| ^^^^^^^^^^
@@ -8,84 +8,96 @@ LL | let re = Regex::new(r"ab.*c").unwrap();
88
= help: to override `-D warnings` add `#[allow(clippy::disallowed_methods)]`
99

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

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

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

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

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

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

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

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

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

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

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

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

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

90-
error: aborting due to 14 previous errors
90+
error: use of a disallowed method `conf_disallowed_methods::identity`
91+
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:78:5
92+
|
93+
LL | identity(());
94+
| ^^^^^^^^
95+
96+
error: use of a disallowed method `conf_disallowed_methods::renamed`
97+
--> tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs:80:5
98+
|
99+
LL | renamed(1);
100+
| ^^^^^^^
101+
102+
error: aborting due to 16 previous errors
91103

0 commit comments

Comments
 (0)