Skip to content

Commit 64d40ef

Browse files
committed
Emit a note to explain unused_pub_items_in_binary
1 parent f12e960 commit 64d40ef

File tree

6 files changed

+25
-1
lines changed

6 files changed

+25
-1
lines changed

compiler/rustc_passes/src/dead.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ use rustc_session::lint::{self, Lint, LintExpectationId};
2626
use rustc_span::{Symbol, kw};
2727

2828
use crate::errors::{
29-
ChangeFields, IgnoredDerivedImpls, MultipleDeadCodes, ParentInfo, UselessAssignment,
29+
ChangeFields, IgnoredDerivedImpls, MultipleDeadCodes, ParentInfo, UnusedPubItemsInBinaryNote,
30+
UselessAssignment,
3031
};
3132

3233
/// Any local definition that may call something in its body block should be explored. For example,
@@ -983,6 +984,13 @@ impl<'tcx> DeadVisitor<'tcx> {
983984
(level.level, level.lint_id)
984985
}
985986

987+
fn unused_pub_items_in_binary_note(&self) -> Option<UnusedPubItemsInBinaryNote> {
988+
self.target_lint
989+
.name
990+
.eq(UNUSED_PUB_ITEMS_IN_BINARY.name)
991+
.then_some(UnusedPubItemsInBinaryNote)
992+
}
993+
986994
// # Panics
987995
// All `dead_codes` must have the same lint level, otherwise we will intentionally ICE.
988996
// This is because we emit a multi-spanned lint using the lint level of the `dead_codes`'s
@@ -1094,6 +1102,7 @@ impl<'tcx> DeadVisitor<'tcx> {
10941102
descr,
10951103
participle,
10961104
name_list,
1105+
unused_pub_items_in_binary_note: self.unused_pub_items_in_binary_note(),
10971106
change_fields_suggestion: fields_suggestion,
10981107
parent_info,
10991108
ignored_derived_impls,
@@ -1130,6 +1139,7 @@ impl<'tcx> DeadVisitor<'tcx> {
11301139
descr,
11311140
participle,
11321141
name_list,
1142+
unused_pub_items_in_binary_note: self.unused_pub_items_in_binary_note(),
11331143
parent_info,
11341144
ignored_derived_impls,
11351145
enum_variants_with_same_name,

compiler/rustc_passes/src/errors.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,8 @@ pub(crate) enum MultipleDeadCodes<'tcx> {
950950
participle: &'tcx str,
951951
name_list: DiagSymbolList,
952952
#[subdiagnostic]
953+
unused_pub_items_in_binary_note: Option<UnusedPubItemsInBinaryNote>,
954+
#[subdiagnostic]
953955
// only on DeadCodes since it's never a problem for tuple struct fields
954956
enum_variants_with_same_name: Vec<EnumVariantSameName<'tcx>>,
955957
#[subdiagnostic]
@@ -973,6 +975,8 @@ pub(crate) enum MultipleDeadCodes<'tcx> {
973975
participle: &'tcx str,
974976
name_list: DiagSymbolList,
975977
#[subdiagnostic]
978+
unused_pub_items_in_binary_note: Option<UnusedPubItemsInBinaryNote>,
979+
#[subdiagnostic]
976980
change_fields_suggestion: ChangeFields,
977981
#[subdiagnostic]
978982
parent_info: Option<ParentInfo<'tcx>>,
@@ -981,6 +985,12 @@ pub(crate) enum MultipleDeadCodes<'tcx> {
981985
},
982986
}
983987

988+
#[derive(Subdiagnostic)]
989+
#[note(
990+
"in libraries, `pub` items can be used by dependent crates; in binaries, they cannot, so this `pub` item is unused"
991+
)]
992+
pub(crate) struct UnusedPubItemsInBinaryNote;
993+
984994
#[derive(Subdiagnostic)]
985995
#[note(
986996
"it is impossible to refer to the {$dead_descr} `{$dead_name}` because it is shadowed by this enum variant with the same name"

tests/ui/lint/unused-pub-items-in-binary/allow-dead-code-transitive.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ error: function `g` is never used
44
LL | pub fn g() {}
55
| ^
66
|
7+
= note: in libraries, `pub` items can be used by dependent crates; in binaries, they cannot, so this `pub` item is unused
78
note: the lint level is defined here
89
--> $DIR/allow-dead-code-transitive.rs:2:9
910
|

tests/ui/lint/unused-pub-items-in-binary/basic.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ error: function `unused_pub_fn` is never used
44
LL | pub fn unused_pub_fn() {}
55
| ^^^^^^^^^^^^^
66
|
7+
= note: in libraries, `pub` items can be used by dependent crates; in binaries, they cannot, so this `pub` item is unused
78
note: the lint level is defined here
89
--> $DIR/basic.rs:2:9
910
|

tests/ui/lint/unused-pub-items-in-binary/deny-lint.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ error: function `unused_pub_fn` is never used
44
LL | pub fn unused_pub_fn() {}
55
| ^^^^^^^^^^^^^
66
|
7+
= note: in libraries, `pub` items can be used by dependent crates; in binaries, they cannot, so this `pub` item is unused
78
note: the lint level is defined here
89
--> $DIR/deny-lint.rs:1:9
910
|

tests/ui/lint/unused-pub-items-in-binary/interaction-allow-dead-code.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ error: function `unused_pub_fn` is never used
44
LL | pub fn unused_pub_fn() {}
55
| ^^^^^^^^^^^^^
66
|
7+
= note: in libraries, `pub` items can be used by dependent crates; in binaries, they cannot, so this `pub` item is unused
78
note: the lint level is defined here
89
--> $DIR/interaction-allow-dead-code.rs:2:9
910
|

0 commit comments

Comments
 (0)