Skip to content

Commit f975708

Browse files
committed
Split up the unknown_or_malformed_diagnostic_attributes lint
1 parent 015c777 commit f975708

24 files changed

+156
-58
lines changed

compiler/rustc_lint/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,14 @@ fn register_builtins(store: &mut LintStore) {
339339

340340
add_lint_group!("deprecated_safe", DEPRECATED_SAFE_2024);
341341

342+
add_lint_group!(
343+
"unknown_or_malformed_diagnostic_attributes",
344+
MALFORMED_DIAGNOSTIC_ATTRIBUTES,
345+
MALFORMED_DIAGNOSTIC_FORMAT_LITERALS,
346+
MISPLACED_DIAGNOSTIC_ATTRIBUTES,
347+
UNKNOWN_DIAGNOSTIC_ATTRIBUTES
348+
);
349+
342350
// Register renamed and removed lints.
343351
store.register_renamed("single_use_lifetime", "single_use_lifetimes");
344352
store.register_renamed("elided_lifetime_in_path", "elided_lifetimes_in_paths");

compiler/rustc_lint_defs/src/builtin.rs

Lines changed: 89 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ declare_lint_pass! {
6363
LOSSY_PROVENANCE_CASTS,
6464
MACRO_EXPANDED_MACRO_EXPORTS_ACCESSED_BY_ABSOLUTE_PATHS,
6565
MACRO_USE_EXTERN_CRATE,
66+
MALFORMED_DIAGNOSTIC_ATTRIBUTES,
67+
MALFORMED_DIAGNOSTIC_FORMAT_LITERALS,
6668
META_VARIABLE_MISUSE,
69+
MISPLACED_DIAGNOSTIC_ATTRIBUTES,
6770
MISSING_ABI,
6871
MISSING_FRAGMENT_SPECIFIER,
6972
MISSING_UNSAFE_ON_EXTERN,
@@ -113,8 +116,8 @@ declare_lint_pass! {
113116
UNFULFILLED_LINT_EXPECTATIONS,
114117
UNINHABITED_STATIC,
115118
UNKNOWN_CRATE_TYPES,
119+
UNKNOWN_DIAGNOSTIC_ATTRIBUTES,
116120
UNKNOWN_LINTS,
117-
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
118121
UNNAMEABLE_TEST_ITEMS,
119122
UNNAMEABLE_TYPES,
120123
UNREACHABLE_CODE,
@@ -4330,31 +4333,105 @@ declare_lint! {
43304333
}
43314334

43324335
declare_lint! {
4333-
/// The `unknown_or_malformed_diagnostic_attributes` lint detects unrecognized or otherwise malformed
4334-
/// diagnostic attributes.
4336+
/// The `malformed_diagnostic_attributes` lint detects malformed diagnostic attributes.
43354337
///
43364338
/// ### Example
43374339
///
43384340
/// ```rust
4339-
/// #![feature(diagnostic_namespace)]
4340-
/// #[diagnostic::does_not_exist]
4341-
/// struct Foo;
4341+
/// #[diagnostic::do_not_recommend(message = "message")]
4342+
/// trait Trait {}
43424343
/// ```
43434344
///
43444345
/// {{produces}}
43454346
///
4347+
/// ### Explanation
4348+
///
4349+
/// It is usually a mistake to use options or syntax that is not supported. Check the spelling,
4350+
/// and check the diagnostic attribute listing for the correct name and syntax. Also consider if
4351+
/// you are using an old version of the compiler; perhaps the option or syntax is only available
4352+
/// in a newer version. See the [reference] for a list of diagnostic attributes and the syntax
4353+
/// of each.
4354+
///
4355+
/// [reference]: https://doc.rust-lang.org/nightly/reference/attributes/diagnostics.html#the-diagnostic-tool-attribute-namespace
4356+
pub MALFORMED_DIAGNOSTIC_ATTRIBUTES,
4357+
Warn,
4358+
"detects malformed diagnostic attributes",
4359+
}
4360+
4361+
declare_lint! {
4362+
/// The `misplaced_diagnostic_attributes` lint detects wrongly placed diagnostic attributes.
4363+
///
4364+
/// ### Example
4365+
///
4366+
/// ```rust
4367+
/// #[diagnostic::do_not_recommend]
4368+
/// struct NotUserFacing;
4369+
/// ```
4370+
///
4371+
/// {{produces}}
43464372
///
43474373
/// ### Explanation
43484374
///
4349-
/// It is usually a mistake to specify a diagnostic attribute that does not exist. Check
4350-
/// the spelling, and check the diagnostic attribute listing for the correct name. Also
4351-
/// consider if you are using an old version of the compiler, and the attribute
4352-
/// is only available in a newer version.
4353-
pub UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
4375+
/// It is usually a mistake to specify a diagnostic attribute on an item it is not meant for.
4376+
/// For example, `#[diagnostic::do_not_recommend]` can only be placed on trait implementations,
4377+
/// and does nothing if placed elsewhere. See the [reference] for a list of diagnostic
4378+
/// attributes and their correct positions.
4379+
///
4380+
/// [reference]: https://doc.rust-lang.org/nightly/reference/attributes/diagnostics.html#the-diagnostic-tool-attribute-namespace
4381+
pub MISPLACED_DIAGNOSTIC_ATTRIBUTES,
43544382
Warn,
4355-
"unrecognized or malformed diagnostic attribute",
4383+
"detects diagnostic attributes that are placed on the wrong item",
43564384
}
43574385

4386+
declare_lint! {
4387+
/// The `unknown_diagnostic_attributes` lint detects unknown diagnostic attributes.
4388+
///
4389+
/// ### Example
4390+
///
4391+
/// ```rust
4392+
/// #[diagnostic::does_not_exist]
4393+
/// struct Thing;
4394+
/// ```
4395+
///
4396+
/// {{produces}}
4397+
///
4398+
/// ### Explanation
4399+
///
4400+
/// It is usually a mistake to specify a diagnostic attribute that does not exist. Check the
4401+
/// spelling, and check the diagnostic attribute listing for the correct name. Also consider if
4402+
/// you are using an old version of the compiler and the attribute is only available in a newer
4403+
/// version. See the [reference] for the list of diagnostic attributes.
4404+
///
4405+
/// [reference]: https://doc.rust-lang.org/nightly/reference/attributes/diagnostics.html#the-diagnostic-tool-attribute-namespace
4406+
pub UNKNOWN_DIAGNOSTIC_ATTRIBUTES,
4407+
Warn,
4408+
"detects unknown diagnostic attributes",
4409+
}
4410+
4411+
declare_lint! {
4412+
/// The `malformed_diagnostic_format_literals` lint detects malformed diagnostic format
4413+
/// literals.
4414+
///
4415+
/// ### Example
4416+
///
4417+
/// ```rust
4418+
/// #[diagnostic::on_unimplemented(message = "{Self}} does not implement `Trait`")]
4419+
/// trait Trait {}
4420+
/// ```
4421+
///
4422+
/// {{produces}}
4423+
///
4424+
/// ### Explanation
4425+
///
4426+
/// The `#[diagnostic::on_unimplemented]` attribute accepts string literal values that are
4427+
/// similar to `format!`'s string literal. See the [reference] for details on what is permitted
4428+
/// in this string literal.
4429+
///
4430+
/// [reference]: https://doc.rust-lang.org/nightly/reference/attributes/diagnostics.html#the-diagnostic-tool-attribute-namespace
4431+
pub MALFORMED_DIAGNOSTIC_FORMAT_LITERALS,
4432+
Warn,
4433+
"detects diagnostic attribute with malformed diagnostic format literals",
4434+
}
43584435
declare_lint! {
43594436
/// The `ambiguous_glob_imports` lint detects glob imports that should report ambiguity
43604437
/// errors, but previously didn't do that due to rustc bugs.

compiler/rustc_passes/src/check_attr.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use rustc_middle::{bug, span_bug};
3232
use rustc_session::config::CrateType;
3333
use rustc_session::lint::builtin::{
3434
CONFLICTING_REPR_HINTS, INVALID_DOC_ATTRIBUTES, INVALID_MACRO_EXPORT_ARGUMENTS,
35-
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES, UNUSED_ATTRIBUTES,
35+
MALFORMED_DIAGNOSTIC_ATTRIBUTES, MISPLACED_DIAGNOSTIC_ATTRIBUTES, UNUSED_ATTRIBUTES,
3636
};
3737
use rustc_session::parse::feature_err;
3838
use rustc_span::{BytePos, DUMMY_SP, Span, Symbol, edition, sym};
@@ -385,7 +385,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
385385
);
386386
}
387387

388-
/// Checks if `#[diagnostic::do_not_recommend]` is applied on a trait impl.
388+
/// Checks if `#[diagnostic::do_not_recommend]` is applied on a trait impl and that it has no
389+
/// arguments.
389390
fn check_do_not_recommend(
390391
&self,
391392
attr_span: Span,
@@ -402,15 +403,15 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
402403
)
403404
{
404405
self.tcx.emit_node_span_lint(
405-
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
406+
MISPLACED_DIAGNOSTIC_ATTRIBUTES,
406407
hir_id,
407408
attr_span,
408409
errors::IncorrectDoNotRecommendLocation,
409410
);
410411
}
411412
if !attr.is_word() {
412413
self.tcx.emit_node_span_lint(
413-
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
414+
MALFORMED_DIAGNOSTIC_ATTRIBUTES,
414415
hir_id,
415416
attr_span,
416417
errors::DoNotRecommendDoesNotExpectArgs,
@@ -422,7 +423,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
422423
fn check_diagnostic_on_unimplemented(&self, attr_span: Span, hir_id: HirId, target: Target) {
423424
if !matches!(target, Target::Trait) {
424425
self.tcx.emit_node_span_lint(
425-
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
426+
MISPLACED_DIAGNOSTIC_ATTRIBUTES,
426427
hir_id,
427428
attr_span,
428429
DiagnosticOnUnimplementedOnlyForTraits,

compiler/rustc_resolve/src/macros.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use rustc_middle::middle::stability;
2424
use rustc_middle::ty::{RegisteredTools, TyCtxt, Visibility};
2525
use rustc_session::lint::BuiltinLintDiag;
2626
use rustc_session::lint::builtin::{
27-
LEGACY_DERIVE_HELPERS, OUT_OF_SCOPE_MACRO_CALLS, UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
27+
LEGACY_DERIVE_HELPERS, OUT_OF_SCOPE_MACRO_CALLS, UNKNOWN_DIAGNOSTIC_ATTRIBUTES,
2828
UNUSED_MACRO_RULES, UNUSED_MACROS,
2929
};
3030
use rustc_session::parse::feature_err;
@@ -663,7 +663,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
663663
);
664664

665665
self.tcx.sess.psess.buffer_lint(
666-
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
666+
UNKNOWN_DIAGNOSTIC_ATTRIBUTES,
667667
attribute.span(),
668668
node_id,
669669
BuiltinLintDiag::UnknownDiagnosticAttribute { span: attribute.span(), typo_name },

compiler/rustc_trait_selection/src/error_reporting/traits/on_unimplemented.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ use rustc_macros::LintDiagnostic;
1111
use rustc_middle::bug;
1212
use rustc_middle::ty::print::PrintTraitRefExt;
1313
use rustc_middle::ty::{self, GenericArgsRef, GenericParamDef, GenericParamDefKind, TyCtxt};
14-
use rustc_session::lint::builtin::UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES;
14+
use rustc_session::lint::builtin::{
15+
MALFORMED_DIAGNOSTIC_ATTRIBUTES, MALFORMED_DIAGNOSTIC_FORMAT_LITERALS,
16+
};
1517
use rustc_span::{Span, Symbol, sym};
1618
use tracing::{debug, info};
1719

@@ -382,7 +384,7 @@ impl IgnoredDiagnosticOption {
382384
if let (Some(new_item), Some(old_item)) = (new, old) {
383385
if let Some(item_def_id) = item_def_id.as_local() {
384386
tcx.emit_node_span_lint(
385-
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
387+
MALFORMED_DIAGNOSTIC_ATTRIBUTES,
386388
tcx.local_def_id_to_hir_id(item_def_id),
387389
new_item,
388390
IgnoredDiagnosticOption { span: new_item, prev_span: old_item, option_name },
@@ -533,7 +535,7 @@ impl<'tcx> OnUnimplementedDirective {
533535
if is_diagnostic_namespace_variant {
534536
if let Some(def_id) = item_def_id.as_local() {
535537
tcx.emit_node_span_lint(
536-
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
538+
MALFORMED_DIAGNOSTIC_ATTRIBUTES,
537539
tcx.local_def_id_to_hir_id(def_id),
538540
vec![item.span()],
539541
MalformedOnUnimplementedAttrLint::new(item.span()),
@@ -689,7 +691,7 @@ impl<'tcx> OnUnimplementedDirective {
689691

690692
if let Some(item_def_id) = item_def_id.as_local() {
691693
tcx.emit_node_span_lint(
692-
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
694+
MALFORMED_DIAGNOSTIC_ATTRIBUTES,
693695
tcx.local_def_id_to_hir_id(item_def_id),
694696
report_span,
695697
MalformedOnUnimplementedAttrLint::new(report_span),
@@ -702,7 +704,7 @@ impl<'tcx> OnUnimplementedDirective {
702704
Attribute::Unparsed(p) if !matches!(p.args, AttrArgs::Empty) => {
703705
if let Some(item_def_id) = item_def_id.as_local() {
704706
tcx.emit_node_span_lint(
705-
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
707+
MALFORMED_DIAGNOSTIC_ATTRIBUTES,
706708
tcx.local_def_id_to_hir_id(item_def_id),
707709
attr.span(),
708710
MalformedOnUnimplementedAttrLint::new(attr.span()),
@@ -712,7 +714,7 @@ impl<'tcx> OnUnimplementedDirective {
712714
_ => {
713715
if let Some(item_def_id) = item_def_id.as_local() {
714716
tcx.emit_node_span_lint(
715-
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
717+
MALFORMED_DIAGNOSTIC_ATTRIBUTES,
716718
tcx.local_def_id_to_hir_id(item_def_id),
717719
attr.span(),
718720
MissingOptionsForOnUnimplementedAttr,
@@ -859,7 +861,7 @@ impl<'tcx> OnUnimplementedFormatString {
859861
if self.is_diagnostic_namespace_variant {
860862
if let Some(trait_def_id) = trait_def_id.as_local() {
861863
tcx.emit_node_span_lint(
862-
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
864+
MALFORMED_DIAGNOSTIC_FORMAT_LITERALS,
863865
tcx.local_def_id_to_hir_id(trait_def_id),
864866
self.span,
865867
WrappedParserError { description: e.description, label: e.label },

compiler/rustc_trait_selection/src/error_reporting/traits/on_unimplemented_format.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_middle::ty::{GenericParamDefKind, TyCtxt};
77
use rustc_parse_format::{
88
Argument, FormatSpec, ParseError, ParseMode, Parser, Piece as RpfPiece, Position,
99
};
10-
use rustc_session::lint::builtin::UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES;
10+
use rustc_session::lint::builtin::MALFORMED_DIAGNOSTIC_FORMAT_LITERALS;
1111
use rustc_span::def_id::DefId;
1212
use rustc_span::{InnerSpan, Span, Symbol, kw, sym};
1313

@@ -69,7 +69,7 @@ impl FormatWarning {
6969
let this = tcx.item_ident(item_def_id);
7070
if let Some(item_def_id) = item_def_id.as_local() {
7171
tcx.emit_node_span_lint(
72-
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
72+
MALFORMED_DIAGNOSTIC_FORMAT_LITERALS,
7373
tcx.local_def_id_to_hir_id(item_def_id),
7474
span,
7575
UnknownFormatParameterForOnUnimplementedAttr {
@@ -82,7 +82,7 @@ impl FormatWarning {
8282
FormatWarning::PositionalArgument { span, .. } => {
8383
if let Some(item_def_id) = item_def_id.as_local() {
8484
tcx.emit_node_span_lint(
85-
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
85+
MALFORMED_DIAGNOSTIC_FORMAT_LITERALS,
8686
tcx.local_def_id_to_hir_id(item_def_id),
8787
span,
8888
DisallowedPositionalArgument,
@@ -92,7 +92,7 @@ impl FormatWarning {
9292
FormatWarning::InvalidSpecifier { span, .. } => {
9393
if let Some(item_def_id) = item_def_id.as_local() {
9494
tcx.emit_node_span_lint(
95-
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
95+
MALFORMED_DIAGNOSTIC_FORMAT_LITERALS,
9696
tcx.local_def_id_to_hir_id(item_def_id),
9797
span,
9898
InvalidFormatSpecifier,

src/tools/lint-docs/src/groups.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ static GROUP_DESCRIPTIONS: &[(&str, &str)] = &[
2626
"Lints that detect identifiers which will be come keywords in later editions",
2727
),
2828
("deprecated-safe", "Lints for functions which were erroneously marked as safe in the past"),
29+
(
30+
"unknown-or-malformed-diagnostic-attributes",
31+
"detects unknown or malformed diagnostic attributes",
32+
),
2933
];
3034

3135
type LintGroups = BTreeMap<String, BTreeSet<String>>;

tests/ui/diagnostic_namespace/deny_malformed_attribute.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ note: the lint level is defined here
99
|
1010
LL | #![deny(unknown_or_malformed_diagnostic_attributes)]
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
= note: `#[deny(unknown_diagnostic_attributes)]` implied by `#[deny(unknown_or_malformed_diagnostic_attributes)]`
1213

1314
error: aborting due to 1 previous error
1415

tests/ui/diagnostic_namespace/do_not_recommend/does_not_acccept_args.current.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ warning: `#[diagnostic::do_not_recommend]` does not expect any arguments
44
LL | #[diagnostic::do_not_recommend(not_accepted)]
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
7-
= note: `#[warn(unknown_or_malformed_diagnostic_attributes)]` on by default
7+
= note: `#[warn(malformed_diagnostic_attributes)]` on by default
88

99
warning: `#[diagnostic::do_not_recommend]` does not expect any arguments
1010
--> $DIR/does_not_acccept_args.rs:15:1

tests/ui/diagnostic_namespace/do_not_recommend/does_not_acccept_args.next.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ warning: `#[diagnostic::do_not_recommend]` does not expect any arguments
44
LL | #[diagnostic::do_not_recommend(not_accepted)]
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
7-
= note: `#[warn(unknown_or_malformed_diagnostic_attributes)]` on by default
7+
= note: `#[warn(malformed_diagnostic_attributes)]` on by default
88

99
warning: `#[diagnostic::do_not_recommend]` does not expect any arguments
1010
--> $DIR/does_not_acccept_args.rs:15:1

tests/ui/diagnostic_namespace/do_not_recommend/incorrect-locations.current.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ warning: `#[diagnostic::do_not_recommend]` can only be placed on trait implement
44
LL | #[diagnostic::do_not_recommend]
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
7-
= note: `#[warn(unknown_or_malformed_diagnostic_attributes)]` on by default
7+
= note: `#[warn(misplaced_diagnostic_attributes)]` on by default
88

99
warning: `#[diagnostic::do_not_recommend]` can only be placed on trait implementations
1010
--> $DIR/incorrect-locations.rs:11:1

tests/ui/diagnostic_namespace/do_not_recommend/incorrect-locations.next.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ warning: `#[diagnostic::do_not_recommend]` can only be placed on trait implement
44
LL | #[diagnostic::do_not_recommend]
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
7-
= note: `#[warn(unknown_or_malformed_diagnostic_attributes)]` on by default
7+
= note: `#[warn(misplaced_diagnostic_attributes)]` on by default
88

99
warning: `#[diagnostic::do_not_recommend]` can only be placed on trait implementations
1010
--> $DIR/incorrect-locations.rs:11:1

0 commit comments

Comments
 (0)