1
Fork 0

Also warn against #[diagnostic::do_not_recommend] on plain impls

This commit is contained in:
Georg Semmler 2024-10-25 08:54:05 +02:00
parent dd31713c53
commit ad29947f02
No known key found for this signature in database
GPG key ID: A87BCEE5205CE489
4 changed files with 27 additions and 4 deletions

View file

@ -115,7 +115,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
for attr in attrs { for attr in attrs {
match attr.path().as_slice() { match attr.path().as_slice() {
[sym::diagnostic, sym::do_not_recommend, ..] => { [sym::diagnostic, sym::do_not_recommend, ..] => {
self.check_do_not_recommend(attr.span, hir_id, target, attr) self.check_do_not_recommend(attr.span, hir_id, target, attr, item)
} }
[sym::diagnostic, sym::on_unimplemented, ..] => { [sym::diagnostic, sym::on_unimplemented, ..] => {
self.check_diagnostic_on_unimplemented(attr.span, hir_id, target) self.check_diagnostic_on_unimplemented(attr.span, hir_id, target)
@ -354,8 +354,15 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
hir_id: HirId, hir_id: HirId,
target: Target, target: Target,
attr: &Attribute, attr: &Attribute,
item: Option<ItemLike<'_>>,
) { ) {
if !matches!(target, Target::Impl) { if !matches!(target, Target::Impl)
|| matches!(
item,
Some(ItemLike::Item(hir::Item { kind: hir::ItemKind::Impl(_impl),.. }))
if _impl.of_trait.is_none()
)
{
self.tcx.emit_node_span_lint( self.tcx.emit_node_span_lint(
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES, UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES,
hir_id, hir_id,

View file

@ -48,5 +48,11 @@ warning: `#[diagnostic::do_not_recommend]` can only be placed on trait implement
LL | #[diagnostic::do_not_recommend] LL | #[diagnostic::do_not_recommend]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
warning: 8 warnings emitted warning: `#[diagnostic::do_not_recommend]` can only be placed on trait implementations
--> $DIR/incorrect-locations.rs:38:1
|
LL | #[diagnostic::do_not_recommend]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
warning: 9 warnings emitted

View file

@ -48,5 +48,11 @@ warning: `#[diagnostic::do_not_recommend]` can only be placed on trait implement
LL | #[diagnostic::do_not_recommend] LL | #[diagnostic::do_not_recommend]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
warning: 8 warnings emitted warning: `#[diagnostic::do_not_recommend]` can only be placed on trait implementations
--> $DIR/incorrect-locations.rs:38:1
|
LL | #[diagnostic::do_not_recommend]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
warning: 9 warnings emitted

View file

@ -19,6 +19,10 @@ type Type = ();
//~^WARN `#[diagnostic::do_not_recommend]` can only be placed //~^WARN `#[diagnostic::do_not_recommend]` can only be placed
enum Enum {} enum Enum {}
#[diagnostic::do_not_recommend]
//~^WARN `#[diagnostic::do_not_recommend]` can only be placed
impl Enum {}
#[diagnostic::do_not_recommend] #[diagnostic::do_not_recommend]
//~^WARN `#[diagnostic::do_not_recommend]` can only be placed //~^WARN `#[diagnostic::do_not_recommend]` can only be placed
extern "C" {} extern "C" {}