Skip to content

Commit 46ce1c3

Browse files
committed
warn on unused linker_messages warning attributes
1 parent 604d7c7 commit 46ce1c3

File tree

7 files changed

+71
-0
lines changed

7 files changed

+71
-0
lines changed

compiler/rustc_passes/messages.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,9 @@ passes_unused_duplicate =
797797
passes_unused_empty_lints_note =
798798
attribute `{$name}` with an empty list has no effect
799799
800+
passes_unused_linker_warnings_note =
801+
the `linker_warnings` lint can only be controlled at the root of a crate with `--crate-type bin`
802+
800803
passes_unused_multiple =
801804
multiple `{$name}` attributes
802805
.suggestion = remove this attribute

compiler/rustc_passes/src/check_attr.rs

Lines changed: 28 additions & 0 deletions
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,
@@ -2270,6 +2271,33 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
22702271
&& item.path == sym::reason
22712272
{
22722273
errors::UnusedNote::NoLints { name: attr.name_or_empty() }
2274+
} else if matches!(
2275+
attr.name_or_empty(),
2276+
sym::allow | sym::warn | sym::deny | sym::forbid | sym::expect
2277+
) && let Some(meta) = attr.meta_item_list()
2278+
&& meta.iter().any(|meta| {
2279+
meta.meta_item().map_or(false, |item| item.path == sym::linker_messages)
2280+
})
2281+
{
2282+
if hir_id != CRATE_HIR_ID {
2283+
let err = match attr.style {
2284+
ast::AttrStyle::Outer => errors::OuterCrateLevelAttr,
2285+
ast::AttrStyle::Inner => errors::OuterCrateLevelAttr,
2286+
};
2287+
self.tcx.emit_node_span_lint(UNUSED_ATTRIBUTES, hir_id, attr.span, err);
2288+
return;
2289+
} else {
2290+
let never_needs_link = self
2291+
.tcx
2292+
.crate_types()
2293+
.iter()
2294+
.all(|kind| matches!(kind, CrateType::Rlib | CrateType::Staticlib));
2295+
if never_needs_link {
2296+
errors::UnusedNote::LinkerWarningsBinaryCrateOnly
2297+
} else {
2298+
return;
2299+
}
2300+
}
22732301
} else if attr.name_or_empty() == sym::default_method_body_is_const {
22742302
errors::UnusedNote::DefaultMethodBodyConst
22752303
} else {

compiler/rustc_passes/src/errors.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,8 @@ pub(crate) enum UnusedNote {
763763
NoLints { name: Symbol },
764764
#[note(passes_unused_default_method_body_const_note)]
765765
DefaultMethodBodyConst,
766+
#[note(passes_unused_linker_warnings_note)]
767+
LinkerWarningsBinaryCrateOnly,
766768
}
767769

768770
#[derive(LintDiagnostic)]

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
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,
Lines changed: 6 additions & 0 deletions
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

Lines changed: 9 additions & 0 deletions
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() {}
Lines changed: 22 additions & 0 deletions
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 with `--crate-type bin`
20+
21+
warning: 2 warnings emitted
22+

0 commit comments

Comments
 (0)