Skip to content

Commit efbdf08

Browse files
committed
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 18910b2 commit efbdf08

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
@@ -353,6 +353,9 @@ passes_ignored_derived_impls =
353353
passes_implied_feature_not_exist =
354354
feature `{$implied_by}` implying `{$feature}` does not exist
355355
356+
passes_incorrect_do_not_recommend_args =
357+
`#[diagnostic::do_not_recommend]` does not expect any arguments
358+
356359
passes_incorrect_do_not_recommend_location =
357360
`#[diagnostic::do_not_recommend]` can only be placed on trait implementations
358361

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)
@@ -353,7 +353,13 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
353353
}
354354

355355
/// Checks if `#[diagnostic::do_not_recommend]` is applied on a trait impl.
356-
fn check_do_not_recommend(&self, attr_span: Span, hir_id: HirId, target: Target) {
356+
fn check_do_not_recommend(
357+
&self,
358+
attr_span: Span,
359+
hir_id: HirId,
360+
target: Target,
361+
attr: &Attribute,
362+
) {
357363
if !matches!(target, Target::Impl) {
358364
self.tcx.emit_node_span_lint(
359365
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
@@ -362,6 +368,14 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
362368
errors::IncorrectDoNotRecommendLocation,
363369
);
364370
}
371+
if !matches!(attr.meta_kind(), Some(MetaItemKind::Word)) {
372+
self.tcx.emit_node_span_lint(
373+
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
374+
hir_id,
375+
attr_span,
376+
errors::DoNotRecommendDoesNotExpectArgs,
377+
);
378+
}
365379
}
366380

367381
/// 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)