Skip to content

Commit ecb6fd8

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 bfd02d8 commit ecb6fd8

File tree

6 files changed

+91
-2
lines changed

6 files changed

+91
-2
lines changed

Diff for: compiler/rustc_passes/messages.ftl

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

Diff for: compiler/rustc_passes/src/check_attr.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
115115
for attr in attrs {
116116
match attr.path().as_slice() {
117117
[sym::diagnostic, sym::do_not_recommend, ..] => {
118-
self.check_do_not_recommend(attr.span, hir_id, target)
118+
self.check_do_not_recommend(attr.span, hir_id, target, attr)
119119
}
120120
[sym::diagnostic, sym::on_unimplemented, ..] => {
121121
self.check_diagnostic_on_unimplemented(attr.span, hir_id, target)
@@ -348,7 +348,13 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
348348
}
349349

350350
/// Checks if `#[diagnostic::do_not_recommend]` is applied on a trait impl.
351-
fn check_do_not_recommend(&self, attr_span: Span, hir_id: HirId, target: Target) {
351+
fn check_do_not_recommend(
352+
&self,
353+
attr_span: Span,
354+
hir_id: HirId,
355+
target: Target,
356+
attr: &Attribute,
357+
) {
352358
if !matches!(target, Target::Impl) {
353359
self.tcx.emit_node_span_lint(
354360
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
@@ -357,6 +363,14 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
357363
errors::IncorrectDoNotRecommendLocation,
358364
);
359365
}
366+
if !attr.is_word() {
367+
self.tcx.emit_node_span_lint(
368+
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
369+
hir_id,
370+
attr_span,
371+
errors::DoNotRecommendDoesNotExpectArgs,
372+
);
373+
}
360374
}
361375

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

Diff for: 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)