Skip to content

Commit 8ecabf8

Browse files
committed
Split up the unknown_or_malformed_diagnostic_attributes lint
1 parent 3de4f1c commit 8ecabf8

21 files changed

+136
-41
lines changed

compiler/rustc_lint/src/lib.rs

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

337337
add_lint_group!("deprecated_safe", DEPRECATED_SAFE_2024);
338338

339+
add_lint_group!(
340+
"unknown_or_malformed_diagnostic_attributes",
341+
MALFORMED_DIAGNOSTIC_ATTRIBUTES,
342+
MALFORMED_DIAGNOSTIC_FORMAT_LITERALS,
343+
MISPLACED_DIAGNOSTIC_ATTRIBUTES,
344+
UNKNOWN_DIAGNOSTIC_ATTRIBUTES
345+
);
346+
339347
// Register renamed and removed lints.
340348
store.register_renamed("single_use_lifetime", "single_use_lifetimes");
341349
store.register_renamed("elided_lifetime_in_path", "elided_lifetimes_in_paths");

compiler/rustc_lint_defs/src/builtin.rs

Lines changed: 85 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ declare_lint_pass! {
6464
LOSSY_PROVENANCE_CASTS,
6565
MACRO_EXPANDED_MACRO_EXPORTS_ACCESSED_BY_ABSOLUTE_PATHS,
6666
MACRO_USE_EXTERN_CRATE,
67+
MALFORMED_DIAGNOSTIC_ATTRIBUTES,
68+
MALFORMED_DIAGNOSTIC_FORMAT_LITERALS,
6769
META_VARIABLE_MISUSE,
70+
MISPLACED_DIAGNOSTIC_ATTRIBUTES,
6871
MISSING_ABI,
6972
MISSING_FRAGMENT_SPECIFIER,
7073
MISSING_UNSAFE_ON_EXTERN,
@@ -114,8 +117,8 @@ declare_lint_pass! {
114117
UNFULFILLED_LINT_EXPECTATIONS,
115118
UNINHABITED_STATIC,
116119
UNKNOWN_CRATE_TYPES,
120+
UNKNOWN_DIAGNOSTIC_ATTRIBUTES,
117121
UNKNOWN_LINTS,
118-
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
119122
UNNAMEABLE_TEST_ITEMS,
120123
UNNAMEABLE_TYPES,
121124
UNNECESSARY_TRANSMUTES,
@@ -4272,31 +4275,104 @@ declare_lint! {
42724275
}
42734276

42744277
declare_lint! {
4275-
/// The `unknown_or_malformed_diagnostic_attributes` lint detects unrecognized or otherwise malformed
4276-
/// diagnostic attributes.
4278+
/// The `malformed_diagnostic_attributes` lint detects malformed diagnostic attributes.
42774279
///
42784280
/// ### Example
42794281
///
42804282
/// ```rust
4281-
/// #![feature(diagnostic_namespace)]
4282-
/// #[diagnostic::does_not_exist]
4283-
/// struct Foo;
4283+
/// #[diagnostic::do_not_recommend(message = "message")]
4284+
/// trait Trait {}
4285+
/// ```
4286+
///
4287+
/// {{produces}}
4288+
///
4289+
/// ### Explanation
4290+
///
4291+
/// It is usually a mistake to use options or syntax that is not supported. Check the spelling,
4292+
/// and check the diagnostic attribute listing for the correct name and syntax. Also
4293+
/// consider if you are using an old version of the compiler; perhaps the option or syntax
4294+
/// is only available in a newer version. See the [reference] for a list of diagnostic attributes
4295+
/// and their syntax.
4296+
///
4297+
/// [reference]: https://doc.rust-lang.org/nightly/reference/attributes/diagnostics.html#the-diagnostic-tool-attribute-namespace
4298+
pub MALFORMED_DIAGNOSTIC_ATTRIBUTES,
4299+
Warn,
4300+
"detects malformed diagnostic attributes",
4301+
}
4302+
4303+
declare_lint! {
4304+
/// The `misplaced_diagnostic_attributes` lint detects wrongly placed diagnostic attributes.
4305+
///
4306+
/// ### Example
4307+
///
4308+
/// ```rust
4309+
/// #[diagnostic::do_not_recommend]
4310+
/// struct NotUserFacing;
42844311
/// ```
42854312
///
42864313
/// {{produces}}
42874314
///
4315+
/// ### Explanation
4316+
///
4317+
/// It is usually a mistake to specify a diagnostic attribute on an item it is not meant for. For example,
4318+
/// `#[diagnostic::do_not_recommend]` can only be placed on trait implementations, and does nothing if placed
4319+
/// elsewhere. See the [reference] for a list of diagnostic attributes and their correct positions.
4320+
///
4321+
/// [reference]: https://doc.rust-lang.org/nightly/reference/attributes/diagnostics.html#the-diagnostic-tool-attribute-namespace
4322+
pub MISPLACED_DIAGNOSTIC_ATTRIBUTES,
4323+
Warn,
4324+
"detects diagnostic attributes that are placed on the wrong item",
4325+
}
4326+
4327+
declare_lint! {
4328+
/// The `unknown_diagnostic_attributes` lint detects unknown diagnostic attributes.
4329+
///
4330+
/// ### Example
4331+
///
4332+
/// ```rust
4333+
/// #[diagnostic::does_not_exist]
4334+
/// struct Thing;
4335+
/// ```
4336+
///
4337+
/// {{produces}}
42884338
///
42894339
/// ### Explanation
42904340
///
42914341
/// It is usually a mistake to specify a diagnostic attribute that does not exist. Check
42924342
/// the spelling, and check the diagnostic attribute listing for the correct name. Also
42934343
/// consider if you are using an old version of the compiler, and the attribute
4294-
/// is only available in a newer version.
4295-
pub UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
4344+
/// is only available in a newer version. See the [reference] for the list of diagnostic attributes.
4345+
///
4346+
/// [reference]: https://doc.rust-lang.org/nightly/reference/attributes/diagnostics.html#the-diagnostic-tool-attribute-namespace
4347+
pub UNKNOWN_DIAGNOSTIC_ATTRIBUTES,
42964348
Warn,
4297-
"unrecognized or malformed diagnostic attribute",
4349+
"detects unknown diagnostic attributes",
42984350
}
42994351

4352+
declare_lint! {
4353+
/// The `malformed_diagnostic_format_literals` lint detects malformed
4354+
/// diagnostic format literals.
4355+
///
4356+
/// ### Example
4357+
///
4358+
/// ```rust
4359+
/// #[diagnostic::on_unimplemented(message = "{Self}} does not implement `Trait`")]
4360+
/// trait Trait {}
4361+
/// ```
4362+
///
4363+
/// {{produces}}
4364+
///
4365+
/// ### Explanation
4366+
///
4367+
/// The `#[diagnostic::on_unimplemented]` attribute accepts string literal values that
4368+
/// are similar to `format!`'s string literal. See the [reference] for details on
4369+
/// what is permitted in this string literal.
4370+
///
4371+
/// [reference]: https://doc.rust-lang.org/nightly/reference/attributes/diagnostics.html#the-diagnostic-tool-attribute-namespace
4372+
pub MALFORMED_DIAGNOSTIC_FORMAT_LITERALS,
4373+
Warn,
4374+
"detects diagnostic attribute with malformed diagnostic format literals",
4375+
}
43004376
declare_lint! {
43014377
/// The `ambiguous_glob_imports` lint detects glob imports that should report ambiguity
43024378
/// errors, but previously didn't do that due to rustc bugs.

compiler/rustc_passes/src/check_attr.rs

Lines changed: 5 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};
@@ -375,7 +375,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
375375
);
376376
}
377377

378-
/// Checks if `#[diagnostic::do_not_recommend]` is applied on a trait impl.
378+
/// Checks if `#[diagnostic::do_not_recommend]` is applied on a trait impl and that it is a word.
379379
fn check_do_not_recommend(
380380
&self,
381381
attr_span: Span,
@@ -392,15 +392,15 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
392392
)
393393
{
394394
self.tcx.emit_node_span_lint(
395-
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
395+
MISPLACED_DIAGNOSTIC_ATTRIBUTES,
396396
hir_id,
397397
attr_span,
398398
errors::IncorrectDoNotRecommendLocation,
399399
);
400400
}
401401
if !attr.is_word() {
402402
self.tcx.emit_node_span_lint(
403-
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
403+
MALFORMED_DIAGNOSTIC_ATTRIBUTES,
404404
hir_id,
405405
attr_span,
406406
errors::DoNotRecommendDoesNotExpectArgs,
@@ -412,7 +412,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
412412
fn check_diagnostic_on_unimplemented(&self, attr_span: Span, hir_id: HirId, target: Target) {
413413
if !matches!(target, Target::Trait) {
414414
self.tcx.emit_node_span_lint(
415-
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
415+
MISPLACED_DIAGNOSTIC_ATTRIBUTES,
416416
hir_id,
417417
attr_span,
418418
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
@@ -8,7 +8,7 @@ use rustc_parse_format::{
88
Alignment, Argument, Count, FormatSpec, ParseError, ParseMode, Parser, Piece as RpfPiece,
99
Position,
1010
};
11-
use rustc_session::lint::builtin::UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES;
11+
use rustc_session::lint::builtin::MALFORMED_DIAGNOSTIC_FORMAT_LITERALS;
1212
use rustc_span::def_id::DefId;
1313
use rustc_span::{BytePos, Pos, Span, Symbol, kw, sym};
1414

@@ -70,7 +70,7 @@ impl FormatWarning {
7070
let this = tcx.item_ident(item_def_id);
7171
if let Some(item_def_id) = item_def_id.as_local() {
7272
tcx.emit_node_span_lint(
73-
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
73+
MALFORMED_DIAGNOSTIC_FORMAT_LITERALS,
7474
tcx.local_def_id_to_hir_id(item_def_id),
7575
span,
7676
UnknownFormatParameterForOnUnimplementedAttr {
@@ -83,7 +83,7 @@ impl FormatWarning {
8383
FormatWarning::PositionalArgument { span, .. } => {
8484
if let Some(item_def_id) = item_def_id.as_local() {
8585
tcx.emit_node_span_lint(
86-
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
86+
MALFORMED_DIAGNOSTIC_FORMAT_LITERALS,
8787
tcx.local_def_id_to_hir_id(item_def_id),
8888
span,
8989
DisallowedPositionalArgument,
@@ -93,7 +93,7 @@ impl FormatWarning {
9393
FormatWarning::InvalidSpecifier { span, .. } => {
9494
if let Some(item_def_id) = item_def_id.as_local() {
9595
tcx.emit_node_span_lint(
96-
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
96+
MALFORMED_DIAGNOSTIC_FORMAT_LITERALS,
9797
tcx.local_def_id_to_hir_id(item_def_id),
9898
span,
9999
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

tests/ui/diagnostic_namespace/non_existing_attributes_accepted.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ warning: unknown diagnostic attribute
44
LL | #[diagnostic::non_existing_attribute]
55
| ^^^^^^^^^^^^^^^^^^^^^^
66
|
7-
= note: `#[warn(unknown_or_malformed_diagnostic_attributes)]` on by default
7+
= note: `#[warn(unknown_diagnostic_attributes)]` on by default
88

99
warning: unknown diagnostic attribute
1010
--> $DIR/non_existing_attributes_accepted.rs:8:15

tests/ui/diagnostic_namespace/on_unimplemented/broken_format.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ warning: unmatched `}` found
44
LL | #[diagnostic::on_unimplemented(message = "{{Test } thing")]
55
| ^^^^^^^^^^^^^^^^
66
|
7-
= note: `#[warn(unknown_or_malformed_diagnostic_attributes)]` on by default
7+
= note: `#[warn(malformed_diagnostic_format_literals)]` on by default
88

99
warning: positional format arguments are not allowed here
1010
--> $DIR/broken_format.rs:7:49

0 commit comments

Comments
 (0)