Skip to content

Commit d40e04a

Browse files
committed
used_underscore_items will not lint exteranl item
1 parent b615c82 commit d40e04a

File tree

4 files changed

+44
-19
lines changed

4 files changed

+44
-19
lines changed

clippy_lints/src/misc.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use clippy_utils::{
77
};
88
use rustc_errors::Applicability;
99
use rustc_hir::def::Res;
10+
use rustc_hir::def_id::LOCAL_CRATE;
1011
use rustc_hir::intravisit::FnKind;
1112
use rustc_hir::{
1213
BinOpKind, BindingMode, Body, ByRef, Expr, ExprKind, FnDecl, Mutability, PatKind, QPath, Stmt, StmtKind,
@@ -281,9 +282,14 @@ fn used_underscore_items<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
281282
},
282283
_ => return,
283284
};
285+
284286
let name = ident.name.as_str();
285287
let definition_span = cx.tcx.def_span(def_id);
286-
if name.starts_with('_') && !name.starts_with("__") && !definition_span.from_expansion() {
288+
if name.starts_with('_')
289+
&& !name.starts_with("__")
290+
&& !definition_span.from_expansion()
291+
&& def_id.krate == LOCAL_CRATE
292+
{
287293
span_lint_and_then(
288294
cx,
289295
USED_UNDERSCORE_ITEMS,

tests/ui/auxiliary/external_item.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
pub struct _ExternalStruct {}
2+
3+
impl _ExternalStruct {
4+
pub fn _foo(self) {}
5+
}
6+
7+
pub fn _exernal_foo() {}

tests/ui/used_underscore_items.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
//@aux-build:external_item.rs
12
#![allow(unused)]
23
#![warn(clippy::used_underscore_items)]
34

5+
extern crate external_item;
6+
47
// should not lint macro
58
macro_rules! macro_wrap_func {
69
() => {
@@ -49,3 +52,12 @@ fn main() {
4952
let foo_struct2 = a::b::c::_FooStruct2 {};
5053
foo_struct2._method_call();
5154
}
55+
56+
// should not lint exteranl crate.
57+
// user cannot control how others name their items
58+
fn external_item_call() {
59+
let foo_struct3 = external_item::_ExternalStruct {};
60+
foo_struct3._foo();
61+
62+
external_item::_exernal_foo();
63+
}

tests/ui/used_underscore_items.stderr

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,109 @@
11
error: used underscore-prefixed item
2-
--> tests/ui/used_underscore_items.rs:40:5
2+
--> tests/ui/used_underscore_items.rs:43:5
33
|
44
LL | _foo1();
55
| ^^^^^^^
66
|
77
note: item is defined here
8-
--> tests/ui/used_underscore_items.rs:19:1
8+
--> tests/ui/used_underscore_items.rs:22:1
99
|
1010
LL | fn _foo1() {}
1111
| ^^^^^^^^^^
1212
= note: `-D clippy::used-underscore-items` implied by `-D warnings`
1313
= help: to override `-D warnings` add `#[allow(clippy::used_underscore_items)]`
1414

1515
error: used underscore-prefixed item
16-
--> tests/ui/used_underscore_items.rs:41:13
16+
--> tests/ui/used_underscore_items.rs:44:13
1717
|
1818
LL | let _ = _foo2();
1919
| ^^^^^^^
2020
|
2121
note: item is defined here
22-
--> tests/ui/used_underscore_items.rs:21:1
22+
--> tests/ui/used_underscore_items.rs:24:1
2323
|
2424
LL | fn _foo2() -> i32 {
2525
| ^^^^^^^^^^^^^^^^^
2626

2727
error: used underscore-prefixed item
28-
--> tests/ui/used_underscore_items.rs:42:5
28+
--> tests/ui/used_underscore_items.rs:45:5
2929
|
3030
LL | a::b::c::_foo3();
3131
| ^^^^^^^^^^^^^^^^
3232
|
3333
note: item is defined here
34-
--> tests/ui/used_underscore_items.rs:28:13
34+
--> tests/ui/used_underscore_items.rs:31:13
3535
|
3636
LL | pub fn _foo3() {}
3737
| ^^^^^^^^^^^^^^
3838

3939
error: used underscore-prefixed item
40-
--> tests/ui/used_underscore_items.rs:43:14
40+
--> tests/ui/used_underscore_items.rs:46:14
4141
|
4242
LL | let _ = &_FooStruct {};
4343
| ^^^^^^^^^^^^^
4444
|
4545
note: item is defined here
46-
--> tests/ui/used_underscore_items.rs:13:1
46+
--> tests/ui/used_underscore_items.rs:16:1
4747
|
4848
LL | struct _FooStruct {}
4949
| ^^^^^^^^^^^^^^^^^
5050

5151
error: used underscore-prefixed item
52-
--> tests/ui/used_underscore_items.rs:44:13
52+
--> tests/ui/used_underscore_items.rs:47:13
5353
|
5454
LL | let _ = _FooStruct {};
5555
| ^^^^^^^^^^^^^
5656
|
5757
note: item is defined here
58-
--> tests/ui/used_underscore_items.rs:13:1
58+
--> tests/ui/used_underscore_items.rs:16:1
5959
|
6060
LL | struct _FooStruct {}
6161
| ^^^^^^^^^^^^^^^^^
6262

6363
error: used underscore-prefixed item
64-
--> tests/ui/used_underscore_items.rs:46:22
64+
--> tests/ui/used_underscore_items.rs:49:22
6565
|
6666
LL | let foo_struct = _FooStruct {};
6767
| ^^^^^^^^^^^^^
6868
|
6969
note: item is defined here
70-
--> tests/ui/used_underscore_items.rs:13:1
70+
--> tests/ui/used_underscore_items.rs:16:1
7171
|
7272
LL | struct _FooStruct {}
7373
| ^^^^^^^^^^^^^^^^^
7474

7575
error: used underscore-prefixed item
76-
--> tests/ui/used_underscore_items.rs:47:5
76+
--> tests/ui/used_underscore_items.rs:50:5
7777
|
7878
LL | foo_struct._method_call();
7979
| ^^^^^^^^^^^^^^^^^^^^^^^^^
8080
|
8181
note: item is defined here
82-
--> tests/ui/used_underscore_items.rs:16:5
82+
--> tests/ui/used_underscore_items.rs:19:5
8383
|
8484
LL | fn _method_call(self) {}
8585
| ^^^^^^^^^^^^^^^^^^^^^
8686

8787
error: used underscore-prefixed item
88-
--> tests/ui/used_underscore_items.rs:49:23
88+
--> tests/ui/used_underscore_items.rs:52:23
8989
|
9090
LL | let foo_struct2 = a::b::c::_FooStruct2 {};
9191
| ^^^^^^^^^^^^^^^^^^^^^^^
9292
|
9393
note: item is defined here
94-
--> tests/ui/used_underscore_items.rs:30:13
94+
--> tests/ui/used_underscore_items.rs:33:13
9595
|
9696
LL | pub struct _FooStruct2 {}
9797
| ^^^^^^^^^^^^^^^^^^^^^^
9898

9999
error: used underscore-prefixed item
100-
--> tests/ui/used_underscore_items.rs:50:5
100+
--> tests/ui/used_underscore_items.rs:53:5
101101
|
102102
LL | foo_struct2._method_call();
103103
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
104104
|
105105
note: item is defined here
106-
--> tests/ui/used_underscore_items.rs:33:17
106+
--> tests/ui/used_underscore_items.rs:36:17
107107
|
108108
LL | pub fn _method_call(self) {}
109109
| ^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)