about summary refs log tree commit diff
diff options
context:
space:
mode:
authorWeiTheShinobi <weitheshinobi@gmail.com>2024-08-25 20:51:26 +0800
committerWeiTheShinobi <weitheshinobi@gmail.com>2024-08-26 00:52:52 +0800
commitd40e04a1cb08d00736909bfb81be01e4cca9d6d6 (patch)
treee7ba89b78f29dc03847e6c54b9fb26f1d7d62e64
parentb615c821809ff3092f021fc669eb63cb47de9912 (diff)
downloadrust-d40e04a1cb08d00736909bfb81be01e4cca9d6d6.tar.gz
rust-d40e04a1cb08d00736909bfb81be01e4cca9d6d6.zip
`used_underscore_items` will not lint exteranl item
-rw-r--r--clippy_lints/src/misc.rs8
-rw-r--r--tests/ui/auxiliary/external_item.rs7
-rw-r--r--tests/ui/used_underscore_items.rs12
-rw-r--r--tests/ui/used_underscore_items.stderr36
4 files changed, 44 insertions, 19 deletions
diff --git a/clippy_lints/src/misc.rs b/clippy_lints/src/misc.rs
index 3fc3f2b3b7f..3e80e48a948 100644
--- a/clippy_lints/src/misc.rs
+++ b/clippy_lints/src/misc.rs
@@ -7,6 +7,7 @@ use clippy_utils::{
 };
 use rustc_errors::Applicability;
 use rustc_hir::def::Res;
+use rustc_hir::def_id::LOCAL_CRATE;
 use rustc_hir::intravisit::FnKind;
 use rustc_hir::{
     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<'_>) {
         },
         _ => return,
     };
+
     let name = ident.name.as_str();
     let definition_span = cx.tcx.def_span(def_id);
-    if name.starts_with('_') && !name.starts_with("__") && !definition_span.from_expansion() {
+    if name.starts_with('_')
+        && !name.starts_with("__")
+        && !definition_span.from_expansion()
+        && def_id.krate == LOCAL_CRATE
+    {
         span_lint_and_then(
             cx,
             USED_UNDERSCORE_ITEMS,
diff --git a/tests/ui/auxiliary/external_item.rs b/tests/ui/auxiliary/external_item.rs
new file mode 100644
index 00000000000..ca4bc369e44
--- /dev/null
+++ b/tests/ui/auxiliary/external_item.rs
@@ -0,0 +1,7 @@
+pub struct _ExternalStruct {}
+
+impl _ExternalStruct {
+    pub fn _foo(self) {}
+}
+
+pub fn _exernal_foo() {}
diff --git a/tests/ui/used_underscore_items.rs b/tests/ui/used_underscore_items.rs
index 6b2ca44e32c..223016a5c96 100644
--- a/tests/ui/used_underscore_items.rs
+++ b/tests/ui/used_underscore_items.rs
@@ -1,6 +1,9 @@
+//@aux-build:external_item.rs
 #![allow(unused)]
 #![warn(clippy::used_underscore_items)]
 
+extern crate external_item;
+
 // should not lint macro
 macro_rules! macro_wrap_func {
     () => {
@@ -49,3 +52,12 @@ fn main() {
     let foo_struct2 = a::b::c::_FooStruct2 {};
     foo_struct2._method_call();
 }
+
+// should not lint exteranl crate.
+// user cannot control how others name their items
+fn external_item_call() {
+    let foo_struct3 = external_item::_ExternalStruct {};
+    foo_struct3._foo();
+
+    external_item::_exernal_foo();
+}
diff --git a/tests/ui/used_underscore_items.stderr b/tests/ui/used_underscore_items.stderr
index 127d8248a94..93ac3a6fec6 100644
--- a/tests/ui/used_underscore_items.stderr
+++ b/tests/ui/used_underscore_items.stderr
@@ -1,11 +1,11 @@
 error: used underscore-prefixed item
-  --> tests/ui/used_underscore_items.rs:40:5
+  --> tests/ui/used_underscore_items.rs:43:5
    |
 LL |     _foo1();
    |     ^^^^^^^
    |
 note: item is defined here
-  --> tests/ui/used_underscore_items.rs:19:1
+  --> tests/ui/used_underscore_items.rs:22:1
    |
 LL | fn _foo1() {}
    | ^^^^^^^^^^
@@ -13,97 +13,97 @@ LL | fn _foo1() {}
    = help: to override `-D warnings` add `#[allow(clippy::used_underscore_items)]`
 
 error: used underscore-prefixed item
-  --> tests/ui/used_underscore_items.rs:41:13
+  --> tests/ui/used_underscore_items.rs:44:13
    |
 LL |     let _ = _foo2();
    |             ^^^^^^^
    |
 note: item is defined here
-  --> tests/ui/used_underscore_items.rs:21:1
+  --> tests/ui/used_underscore_items.rs:24:1
    |
 LL | fn _foo2() -> i32 {
    | ^^^^^^^^^^^^^^^^^
 
 error: used underscore-prefixed item
-  --> tests/ui/used_underscore_items.rs:42:5
+  --> tests/ui/used_underscore_items.rs:45:5
    |
 LL |     a::b::c::_foo3();
    |     ^^^^^^^^^^^^^^^^
    |
 note: item is defined here
-  --> tests/ui/used_underscore_items.rs:28:13
+  --> tests/ui/used_underscore_items.rs:31:13
    |
 LL |             pub fn _foo3() {}
    |             ^^^^^^^^^^^^^^
 
 error: used underscore-prefixed item
-  --> tests/ui/used_underscore_items.rs:43:14
+  --> tests/ui/used_underscore_items.rs:46:14
    |
 LL |     let _ = &_FooStruct {};
    |              ^^^^^^^^^^^^^
    |
 note: item is defined here
-  --> tests/ui/used_underscore_items.rs:13:1
+  --> tests/ui/used_underscore_items.rs:16:1
    |
 LL | struct _FooStruct {}
    | ^^^^^^^^^^^^^^^^^
 
 error: used underscore-prefixed item
-  --> tests/ui/used_underscore_items.rs:44:13
+  --> tests/ui/used_underscore_items.rs:47:13
    |
 LL |     let _ = _FooStruct {};
    |             ^^^^^^^^^^^^^
    |
 note: item is defined here
-  --> tests/ui/used_underscore_items.rs:13:1
+  --> tests/ui/used_underscore_items.rs:16:1
    |
 LL | struct _FooStruct {}
    | ^^^^^^^^^^^^^^^^^
 
 error: used underscore-prefixed item
-  --> tests/ui/used_underscore_items.rs:46:22
+  --> tests/ui/used_underscore_items.rs:49:22
    |
 LL |     let foo_struct = _FooStruct {};
    |                      ^^^^^^^^^^^^^
    |
 note: item is defined here
-  --> tests/ui/used_underscore_items.rs:13:1
+  --> tests/ui/used_underscore_items.rs:16:1
    |
 LL | struct _FooStruct {}
    | ^^^^^^^^^^^^^^^^^
 
 error: used underscore-prefixed item
-  --> tests/ui/used_underscore_items.rs:47:5
+  --> tests/ui/used_underscore_items.rs:50:5
    |
 LL |     foo_struct._method_call();
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^
    |
 note: item is defined here
-  --> tests/ui/used_underscore_items.rs:16:5
+  --> tests/ui/used_underscore_items.rs:19:5
    |
 LL |     fn _method_call(self) {}
    |     ^^^^^^^^^^^^^^^^^^^^^
 
 error: used underscore-prefixed item
-  --> tests/ui/used_underscore_items.rs:49:23
+  --> tests/ui/used_underscore_items.rs:52:23
    |
 LL |     let foo_struct2 = a::b::c::_FooStruct2 {};
    |                       ^^^^^^^^^^^^^^^^^^^^^^^
    |
 note: item is defined here
-  --> tests/ui/used_underscore_items.rs:30:13
+  --> tests/ui/used_underscore_items.rs:33:13
    |
 LL |             pub struct _FooStruct2 {}
    |             ^^^^^^^^^^^^^^^^^^^^^^
 
 error: used underscore-prefixed item
-  --> tests/ui/used_underscore_items.rs:50:5
+  --> tests/ui/used_underscore_items.rs:53:5
    |
 LL |     foo_struct2._method_call();
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
 note: item is defined here
-  --> tests/ui/used_underscore_items.rs:33:17
+  --> tests/ui/used_underscore_items.rs:36:17
    |
 LL |                 pub fn _method_call(self) {}
    |                 ^^^^^^^^^^^^^^^^^^^^^^^^^