Skip to content

Commit fda2384

Browse files
committed
warn on unused linker_messages warning attributes
1 parent 537218a commit fda2384

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
@@ -817,6 +817,9 @@ passes_unused_duplicate =
817817
passes_unused_empty_lints_note =
818818
attribute `{$name}` with an empty list has no effect
819819
820+
passes_unused_linker_warnings_note =
821+
the `linker_warnings` lint can only be controlled at the root of a crate that needs to be linked
822+
820823
passes_unused_multiple =
821824
multiple `{$name}` attributes
822825
.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,
@@ -2328,6 +2329,33 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
23282329
&& item.path == sym::reason
23292330
{
23302331
errors::UnusedNote::NoLints { name: attr.name_or_empty() }
2332+
} else if matches!(
2333+
attr.name_or_empty(),
2334+
sym::allow | sym::warn | sym::deny | sym::forbid | sym::expect
2335+
) && let Some(meta) = attr.meta_item_list()
2336+
&& meta.iter().any(|meta| {
2337+
meta.meta_item().map_or(false, |item| item.path == sym::linker_messages)
2338+
})
2339+
{
2340+
if hir_id != CRATE_HIR_ID {
2341+
let err = match attr.style {
2342+
ast::AttrStyle::Outer => errors::OuterCrateLevelAttr,
2343+
ast::AttrStyle::Inner => errors::OuterCrateLevelAttr,
2344+
};
2345+
self.tcx.emit_node_span_lint(UNUSED_ATTRIBUTES, hir_id, attr.span, err);
2346+
return;
2347+
} else {
2348+
let never_needs_link = self
2349+
.tcx
2350+
.crate_types()
2351+
.iter()
2352+
.all(|kind| matches!(kind, CrateType::Rlib | CrateType::Staticlib));
2353+
if never_needs_link {
2354+
errors::UnusedNote::LinkerWarningsBinaryCrateOnly
2355+
} else {
2356+
return;
2357+
}
2358+
}
23312359
} else if attr.name_or_empty() == sym::default_method_body_is_const {
23322360
errors::UnusedNote::DefaultMethodBodyConst
23332361
} else {

compiler/rustc_passes/src/errors.rs

+2
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,8 @@ pub(crate) enum UnusedNote {
802802
NoLints { name: Symbol },
803803
#[note(passes_unused_default_method_body_const_note)]
804804
DefaultMethodBodyConst,
805+
#[note(passes_unused_linker_warnings_note)]
806+
LinkerWarningsBinaryCrateOnly,
805807
}
806808

807809
#[derive(LintDiagnostic)]

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1189,6 +1189,7 @@ symbols! {
11891189
link_section,
11901190
linkage,
11911191
linker,
1192+
linker_messages,
11921193
lint_reasons,
11931194
literal,
11941195
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)