1
Fork 0

warn on unused linker_messages warning attributes

This commit is contained in:
jyn 2024-10-16 01:35:01 -04:00
parent 537218afb2
commit fda2384333
7 changed files with 71 additions and 0 deletions

View file

@ -817,6 +817,9 @@ passes_unused_duplicate =
passes_unused_empty_lints_note = passes_unused_empty_lints_note =
attribute `{$name}` with an empty list has no effect attribute `{$name}` with an empty list has no effect
passes_unused_linker_warnings_note =
the `linker_warnings` lint can only be controlled at the root of a crate that needs to be linked
passes_unused_multiple = passes_unused_multiple =
multiple `{$name}` attributes multiple `{$name}` attributes
.suggestion = remove this attribute .suggestion = remove this attribute

View file

@ -25,6 +25,7 @@ use rustc_middle::traits::ObligationCause;
use rustc_middle::ty::error::{ExpectedFound, TypeError}; use rustc_middle::ty::error::{ExpectedFound, TypeError};
use rustc_middle::ty::{self, TyCtxt, TypingMode}; use rustc_middle::ty::{self, TyCtxt, TypingMode};
use rustc_middle::{bug, span_bug}; use rustc_middle::{bug, span_bug};
use rustc_session::config::CrateType;
use rustc_session::lint::builtin::{ use rustc_session::lint::builtin::{
CONFLICTING_REPR_HINTS, INVALID_DOC_ATTRIBUTES, INVALID_MACRO_EXPORT_ARGUMENTS, CONFLICTING_REPR_HINTS, INVALID_DOC_ATTRIBUTES, INVALID_MACRO_EXPORT_ARGUMENTS,
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES, UNUSED_ATTRIBUTES, UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES, UNUSED_ATTRIBUTES,
@ -2328,6 +2329,33 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
&& item.path == sym::reason && item.path == sym::reason
{ {
errors::UnusedNote::NoLints { name: attr.name_or_empty() } errors::UnusedNote::NoLints { name: attr.name_or_empty() }
} else if matches!(
attr.name_or_empty(),
sym::allow | sym::warn | sym::deny | sym::forbid | sym::expect
) && let Some(meta) = attr.meta_item_list()
&& meta.iter().any(|meta| {
meta.meta_item().map_or(false, |item| item.path == sym::linker_messages)
})
{
if hir_id != CRATE_HIR_ID {
let err = match attr.style {
ast::AttrStyle::Outer => errors::OuterCrateLevelAttr,
ast::AttrStyle::Inner => errors::OuterCrateLevelAttr,
};
self.tcx.emit_node_span_lint(UNUSED_ATTRIBUTES, hir_id, attr.span, err);
return;
} else {
let never_needs_link = self
.tcx
.crate_types()
.iter()
.all(|kind| matches!(kind, CrateType::Rlib | CrateType::Staticlib));
if never_needs_link {
errors::UnusedNote::LinkerWarningsBinaryCrateOnly
} else {
return;
}
}
} else if attr.name_or_empty() == sym::default_method_body_is_const { } else if attr.name_or_empty() == sym::default_method_body_is_const {
errors::UnusedNote::DefaultMethodBodyConst errors::UnusedNote::DefaultMethodBodyConst
} else { } else {

View file

@ -802,6 +802,8 @@ pub(crate) enum UnusedNote {
NoLints { name: Symbol }, NoLints { name: Symbol },
#[note(passes_unused_default_method_body_const_note)] #[note(passes_unused_default_method_body_const_note)]
DefaultMethodBodyConst, DefaultMethodBodyConst,
#[note(passes_unused_linker_warnings_note)]
LinkerWarningsBinaryCrateOnly,
} }
#[derive(LintDiagnostic)] #[derive(LintDiagnostic)]

View file

@ -1189,6 +1189,7 @@ symbols! {
link_section, link_section,
linkage, linkage,
linker, linker,
linker_messages,
lint_reasons, lint_reasons,
literal, literal,
load, load,

View file

@ -0,0 +1,6 @@
//@ build-pass
#![crate_type = "bin"]
#![warn(unused_attributes)]
#![allow(linker_messages)]
fn main() {}

View file

@ -0,0 +1,9 @@
//@ check-pass
#![crate_type = "lib"]
#![warn(unused_attributes)]
#![allow(linker_messages)]
//~^ WARNING unused attribute
#[allow(linker_messages)]
//~^ WARNING should be an inner attribute
fn foo() {}

View file

@ -0,0 +1,22 @@
warning: crate-level attribute should be an inner attribute: add an exclamation mark: `#![foo]`
--> $DIR/linker-warning.rs:7:1
|
LL | #[allow(linker_messages)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: the lint level is defined here
--> $DIR/linker-warning.rs:3:9
|
LL | #![warn(unused_attributes)]
| ^^^^^^^^^^^^^^^^^
warning: unused attribute
--> $DIR/linker-warning.rs:4:1
|
LL | #![allow(linker_messages)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove this attribute
|
= note: the `linker_warnings` lint can only be controlled at the root of a crate that needs to be linked
warning: 2 warnings emitted