Skip to content

Commit d197dc0

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 4abe867 commit d197dc0

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
@@ -116,7 +116,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
116116
for attr in attrs {
117117
match attr.path().as_slice() {
118118
[sym::diagnostic, sym::do_not_recommend, ..] => {
119-
self.check_do_not_recommend(attr.span, hir_id, target)
119+
self.check_do_not_recommend(attr.span, hir_id, target, attr)
120120
}
121121
[sym::diagnostic, sym::on_unimplemented, ..] => {
122122
self.check_diagnostic_on_unimplemented(attr.span, hir_id, target)
@@ -349,7 +349,13 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
349349
}
350350

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

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