Skip to content

Commit 3d814b3

Browse files
committed
Also warn against #[diagnostic::do_not_recommend] on plain impls
1 parent a8f82b1 commit 3d814b3

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

compiler/rustc_passes/src/check_attr.rs

+9-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, attr)
120+
self.check_do_not_recommend(attr.span, hir_id, target, attr, item)
121121
}
122122
[sym::diagnostic, sym::on_unimplemented, ..] => {
123123
self.check_diagnostic_on_unimplemented(attr.span, hir_id, target)
@@ -358,8 +358,15 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
358358
hir_id: HirId,
359359
target: Target,
360360
attr: &Attribute,
361+
item: Option<ItemLike<'_>>,
361362
) {
362-
if !matches!(target, Target::Impl) {
363+
if !matches!(target, Target::Impl)
364+
|| matches!(
365+
item,
366+
Some(ItemLike::Item(hir::Item { kind: hir::ItemKind::Impl(_impl),.. }))
367+
if _impl.of_trait.is_none()
368+
)
369+
{
363370
self.tcx.emit_node_span_lint(
364371
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
365372
hir_id,

tests/ui/diagnostic_namespace/do_not_recommend/incorrect-locations.current.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,11 @@ warning: `#[diagnostic::do_not_recommend]` can only be placed on trait implement
4848
LL | #[diagnostic::do_not_recommend]
4949
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5050

51-
warning: 8 warnings emitted
51+
warning: `#[diagnostic::do_not_recommend]` can only be placed on trait implementations
52+
--> $DIR/incorrect-locations.rs:38:1
53+
|
54+
LL | #[diagnostic::do_not_recommend]
55+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
56+
57+
warning: 9 warnings emitted
5258

tests/ui/diagnostic_namespace/do_not_recommend/incorrect-locations.next.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,11 @@ warning: `#[diagnostic::do_not_recommend]` can only be placed on trait implement
4848
LL | #[diagnostic::do_not_recommend]
4949
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5050

51-
warning: 8 warnings emitted
51+
warning: `#[diagnostic::do_not_recommend]` can only be placed on trait implementations
52+
--> $DIR/incorrect-locations.rs:38:1
53+
|
54+
LL | #[diagnostic::do_not_recommend]
55+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
56+
57+
warning: 9 warnings emitted
5258

tests/ui/diagnostic_namespace/do_not_recommend/incorrect-locations.rs

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ type Type = ();
1919
//~^WARN `#[diagnostic::do_not_recommend]` can only be placed
2020
enum Enum {}
2121

22+
#[diagnostic::do_not_recommend]
23+
//~^WARN `#[diagnostic::do_not_recommend]` can only be placed
24+
impl Enum {}
25+
2226
#[diagnostic::do_not_recommend]
2327
//~^WARN `#[diagnostic::do_not_recommend]` can only be placed
2428
extern "C" {}

0 commit comments

Comments
 (0)