Skip to content

Commit 21ec339

Browse files
committed
warn on unused linker_messages warning attributes
1 parent a1e8c05 commit 21ec339

File tree

7 files changed

+71
-0
lines changed

7 files changed

+71
-0
lines changed

compiler/rustc_passes/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,9 @@ passes_unused_duplicate =
804804
passes_unused_empty_lints_note =
805805
attribute `{$name}` with an empty list has no effect
806806
807+
passes_unused_linker_warnings_note =
808+
the `linker_warnings` lint can only be controlled at the root of a crate that needs to be linked
809+
807810
passes_unused_multiple =
808811
multiple `{$name}` attributes
809812
.suggestion = remove this attribute

compiler/rustc_passes/src/check_attr.rs

+28
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use rustc_middle::traits::ObligationCause;
2525
use rustc_middle::ty::error::{ExpectedFound, TypeError};
2626
use rustc_middle::ty::{self, TyCtxt, TypingMode};
2727
use rustc_middle::{bug, span_bug};
28+
use rustc_session::config::CrateType;
2829
use rustc_session::lint::builtin::{
2930
CONFLICTING_REPR_HINTS, INVALID_DOC_ATTRIBUTES, INVALID_MACRO_EXPORT_ARGUMENTS,
3031
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES, UNUSED_ATTRIBUTES,
@@ -2313,6 +2314,33 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
23132314
&& item.path == sym::reason
23142315
{
23152316
errors::UnusedNote::NoLints { name: attr.name_or_empty() }
2317+
} else if matches!(
2318+
attr.name_or_empty(),
2319+
sym::allow | sym::warn | sym::deny | sym::forbid | sym::expect
2320+
) && let Some(meta) = attr.meta_item_list()
2321+
&& meta.iter().any(|meta| {
2322+
meta.meta_item().map_or(false, |item| item.path == sym::linker_messages)
2323+
})
2324+
{
2325+
if hir_id != CRATE_HIR_ID {
2326+
let err = match attr.style {
2327+
ast::AttrStyle::Outer => errors::OuterCrateLevelAttr,
2328+
ast::AttrStyle::Inner => errors::OuterCrateLevelAttr,
2329+
};
2330+
self.tcx.emit_node_span_lint(UNUSED_ATTRIBUTES, hir_id, attr.span, err);
2331+
return;
2332+
} else {
2333+
let never_needs_link = self
2334+
.tcx
2335+
.crate_types()
2336+
.iter()
2337+
.all(|kind| matches!(kind, CrateType::Rlib | CrateType::Staticlib));
2338+
if never_needs_link {
2339+
errors::UnusedNote::LinkerWarningsBinaryCrateOnly
2340+
} else {
2341+
return;
2342+
}
2343+
}
23162344
} else if attr.name_or_empty() == sym::default_method_body_is_const {
23172345
errors::UnusedNote::DefaultMethodBodyConst
23182346
} else {

compiler/rustc_passes/src/errors.rs

+2
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,8 @@ pub(crate) enum UnusedNote {
776776
NoLints { name: Symbol },
777777
#[note(passes_unused_default_method_body_const_note)]
778778
DefaultMethodBodyConst,
779+
#[note(passes_unused_linker_warnings_note)]
780+
LinkerWarningsBinaryCrateOnly,
779781
}
780782

781783
#[derive(LintDiagnostic)]

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1184,6 +1184,7 @@ symbols! {
11841184
link_section,
11851185
linkage,
11861186
linker,
1187+
linker_messages,
11871188
lint_reasons,
11881189
literal,
11891190
load,

tests/ui/lint/linker-warning-bin.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//@ build-pass
2+
#![crate_type = "bin"]
3+
#![warn(unused_attributes)]
4+
#![allow(linker_messages)]
5+
6+
fn main() {}

tests/ui/lint/linker-warning.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//@ check-pass
2+
#![crate_type = "lib"]
3+
#![warn(unused_attributes)]
4+
#![allow(linker_messages)]
5+
//~^ WARNING unused attribute
6+
7+
#[allow(linker_messages)]
8+
//~^ WARNING should be an inner attribute
9+
fn foo() {}

tests/ui/lint/linker-warning.stderr

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
warning: crate-level attribute should be an inner attribute: add an exclamation mark: `#![foo]`
2+
--> $DIR/linker-warning.rs:7:1
3+
|
4+
LL | #[allow(linker_messages)]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/linker-warning.rs:3:9
9+
|
10+
LL | #![warn(unused_attributes)]
11+
| ^^^^^^^^^^^^^^^^^
12+
13+
warning: unused attribute
14+
--> $DIR/linker-warning.rs:4:1
15+
|
16+
LL | #![allow(linker_messages)]
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove this attribute
18+
|
19+
= note: the `linker_warnings` lint can only be controlled at the root of a crate that needs to be linked
20+
21+
warning: 2 warnings emitted
22+

0 commit comments

Comments
 (0)