Skip to content

Commit ea6eb85

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 ce37515 commit ea6eb85

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
@@ -349,6 +349,9 @@ passes_ignored_derived_impls =
349349
passes_implied_feature_not_exist =
350350
feature `{$implied_by}` implying `{$feature}` does not exist
351351
352+
passes_incorrect_do_not_recommend_args =
353+
`#[diagnostic::do_not_recommend]` does not expect any arguments
354+
352355
passes_incorrect_do_not_recommend_location =
353356
`#[diagnostic::do_not_recommend]` can only be placed on trait implementations
354357

compiler/rustc_passes/src/check_attr.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
117117
for attr in attrs {
118118
match attr.path().as_slice() {
119119
[sym::diagnostic, sym::do_not_recommend, ..] => {
120-
self.check_do_not_recommend(attr.span, hir_id, target)
120+
self.check_do_not_recommend(attr.span, hir_id, target, attr)
121121
}
122122
[sym::diagnostic, sym::on_unimplemented, ..] => {
123123
self.check_diagnostic_on_unimplemented(attr.span, hir_id, target)
@@ -352,7 +352,13 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
352352
}
353353

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

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