Skip to content

Commit 15c81ec

Browse files
committedNov 18, 2024
Check #[diagnostic::do_not_recommend] for arguments
This commit adds a check that verifies that no arguments are passed to `#[diagnostic::do_not_recommend]`. If we detect arguments we emit a warning.
1 parent fda91cd commit 15c81ec

File tree

6 files changed

+91
-2
lines changed

6 files changed

+91
-2
lines changed
 

‎compiler/rustc_passes/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,9 @@ passes_ignored_derived_impls =
356356
passes_implied_feature_not_exist =
357357
feature `{$implied_by}` implying `{$feature}` does not exist
358358
359+
passes_incorrect_do_not_recommend_args =
360+
`#[diagnostic::do_not_recommend]` does not expect any arguments
361+
359362
passes_incorrect_do_not_recommend_location =
360363
`#[diagnostic::do_not_recommend]` can only be placed on trait implementations
361364

‎compiler/rustc_passes/src/check_attr.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
118118
for attr in attrs {
119119
match attr.path().as_slice() {
120120
[sym::diagnostic, sym::do_not_recommend, ..] => {
121-
self.check_do_not_recommend(attr.span, hir_id, target)
121+
self.check_do_not_recommend(attr.span, hir_id, target, attr)
122122
}
123123
[sym::diagnostic, sym::on_unimplemented, ..] => {
124124
self.check_diagnostic_on_unimplemented(attr.span, hir_id, target)
@@ -350,7 +350,13 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
350350
}
351351

352352
/// Checks if `#[diagnostic::do_not_recommend]` is applied on a trait impl.
353-
fn check_do_not_recommend(&self, attr_span: Span, hir_id: HirId, target: Target) {
353+
fn check_do_not_recommend(
354+
&self,
355+
attr_span: Span,
356+
hir_id: HirId,
357+
target: Target,
358+
attr: &Attribute,
359+
) {
354360
if !matches!(target, Target::Impl) {
355361
self.tcx.emit_node_span_lint(
356362
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
@@ -359,6 +365,14 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
359365
errors::IncorrectDoNotRecommendLocation,
360366
);
361367
}
368+
if !matches!(attr.meta_kind(), Some(MetaItemKind::Word)) {
369+
self.tcx.emit_node_span_lint(
370+
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
371+
hir_id,
372+
attr_span,
373+
errors::DoNotRecommendDoesNotExpectArgs,
374+
);
375+
}
362376
}
363377

364378
/// Checks if `#[diagnostic::on_unimplemented]` is applied to a trait definition

‎compiler/rustc_passes/src/errors.rs

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ use crate::lang_items::Duplicate;
2020
#[diag(passes_incorrect_do_not_recommend_location)]
2121
pub(crate) struct IncorrectDoNotRecommendLocation;
2222

23+
#[derive(LintDiagnostic)]
24+
#[diag(passes_incorrect_do_not_recommend_args)]
25+
pub(crate) struct DoNotRecommendDoesNotExpectArgs;
26+
2327
#[derive(Diagnostic)]
2428
#[diag(passes_autodiff_attr)]
2529
pub(crate) struct AutoDiffAttr {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
warning: `#[diagnostic::do_not_recommend]` does not expect any arguments
2+
--> $DIR/does_not_acccept_args.rs:12:1
3+
|
4+
LL | #[diagnostic::do_not_recommend(not_accepted)]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `#[warn(unknown_or_malformed_diagnostic_attributes)]` on by default
8+
9+
warning: `#[diagnostic::do_not_recommend]` does not expect any arguments
10+
--> $DIR/does_not_acccept_args.rs:16:1
11+
|
12+
LL | #[diagnostic::do_not_recommend(not_accepted = "foo")]
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
15+
warning: `#[diagnostic::do_not_recommend]` does not expect any arguments
16+
--> $DIR/does_not_acccept_args.rs:20:1
17+
|
18+
LL | #[diagnostic::do_not_recommend(not_accepted(42))]
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
20+
21+
warning: 3 warnings emitted
22+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
warning: `#[diagnostic::do_not_recommend]` does not expect any arguments
2+
--> $DIR/does_not_acccept_args.rs:12:1
3+
|
4+
LL | #[diagnostic::do_not_recommend(not_accepted)]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `#[warn(unknown_or_malformed_diagnostic_attributes)]` on by default
8+
9+
warning: `#[diagnostic::do_not_recommend]` does not expect any arguments
10+
--> $DIR/does_not_acccept_args.rs:16:1
11+
|
12+
LL | #[diagnostic::do_not_recommend(not_accepted = "foo")]
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
15+
warning: `#[diagnostic::do_not_recommend]` does not expect any arguments
16+
--> $DIR/does_not_acccept_args.rs:20:1
17+
|
18+
LL | #[diagnostic::do_not_recommend(not_accepted(42))]
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
20+
21+
warning: 3 warnings emitted
22+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//@ check-pass
2+
//@ revisions: current next
3+
//@ ignore-compare-mode-next-solver (explicit revisions)
4+
//@[next] compile-flags: -Znext-solver
5+
6+
#![feature(do_not_recommend)]
7+
8+
trait Foo {}
9+
trait Bar {}
10+
trait Baz {}
11+
12+
#[diagnostic::do_not_recommend(not_accepted)]
13+
//~^ WARNING `#[diagnostic::do_not_recommend]` does not expect any arguments
14+
impl<T> Foo for T where T: Send {}
15+
16+
#[diagnostic::do_not_recommend(not_accepted = "foo")]
17+
//~^ WARNING `#[diagnostic::do_not_recommend]` does not expect any arguments
18+
impl<T> Bar for T where T: Send {}
19+
20+
#[diagnostic::do_not_recommend(not_accepted(42))]
21+
//~^ WARNING `#[diagnostic::do_not_recommend]` does not expect any arguments
22+
impl<T> Baz for T where T: Send {}
23+
24+
fn main() {}

0 commit comments

Comments
 (0)