Skip to content

Commit 814451a

Browse files
committed
warn on unused linker_messages warning attributes
1 parent 1fbb3cc commit 814451a

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
@@ -798,6 +798,9 @@ passes_unused_duplicate =
798798
passes_unused_empty_lints_note =
799799
attribute `{$name}` with an empty list has no effect
800800
801+
passes_unused_linker_warnings_note =
802+
the `linker_warnings` lint can only be controlled at the root of a crate with `--crate-type bin`
803+
801804
passes_unused_multiple =
802805
multiple `{$name}` attributes
803806
.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
@@ -27,6 +27,7 @@ use rustc_middle::traits::ObligationCause;
2727
use rustc_middle::ty::error::{ExpectedFound, TypeError};
2828
use rustc_middle::ty::{self, TyCtxt, TypingMode};
2929
use rustc_middle::{bug, span_bug};
30+
use rustc_session::config::CrateType;
3031
use rustc_session::lint::builtin::{
3132
CONFLICTING_REPR_HINTS, INVALID_DOC_ATTRIBUTES, INVALID_MACRO_EXPORT_ARGUMENTS,
3233
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES, UNUSED_ATTRIBUTES,
@@ -2271,6 +2272,33 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
22712272
&& item.path == sym::reason
22722273
{
22732274
errors::UnusedNote::NoLints { name: attr.name_or_empty() }
2275+
} else if matches!(
2276+
attr.name_or_empty(),
2277+
sym::allow | sym::warn | sym::deny | sym::forbid | sym::expect
2278+
) && let Some(meta) = attr.meta_item_list()
2279+
&& meta.iter().any(|meta| {
2280+
meta.meta_item().map_or(false, |item| item.path == sym::linker_messages)
2281+
})
2282+
{
2283+
if hir_id != CRATE_HIR_ID {
2284+
let err = match attr.style {
2285+
ast::AttrStyle::Outer => errors::OuterCrateLevelAttr,
2286+
ast::AttrStyle::Inner => errors::OuterCrateLevelAttr,
2287+
};
2288+
self.tcx.emit_node_span_lint(UNUSED_ATTRIBUTES, hir_id, attr.span, err);
2289+
return;
2290+
} else {
2291+
let never_needs_link = self
2292+
.tcx
2293+
.crate_types()
2294+
.iter()
2295+
.all(|kind| matches!(kind, CrateType::Rlib | CrateType::Staticlib));
2296+
if never_needs_link {
2297+
errors::UnusedNote::LinkerWarningsBinaryCrateOnly
2298+
} else {
2299+
return;
2300+
}
2301+
}
22742302
} else if attr.name_or_empty() == sym::default_method_body_is_const {
22752303
errors::UnusedNote::DefaultMethodBodyConst
22762304
} else {

compiler/rustc_passes/src/errors.rs

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

767769
#[derive(LintDiagnostic)]

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,6 +1153,7 @@ symbols! {
11531153
link_section,
11541154
linkage,
11551155
linker,
1156+
linker_messages,
11561157
lint_reasons,
11571158
literal,
11581159
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)