dotnet / docs

This repository contains .NET Documentation.
https://learn.microsoft.com/dotnet
Creative Commons Attribution 4.0 International
4.24k stars 5.88k forks source link

CS0809 should mention that the ObsoleteAttribute will not work if applied to an overidden member only #41621

Closed rhys-vdw closed 1 month ago

rhys-vdw commented 3 months ago

Type of issue

Missing information

Description

Seems that the following will not trigger an obsolete warning.

class A {
  public virtual void Foo() {}
}

class B : A {
  [Obsolete("doesn't work")]
  public override void Foo() {}
}

static class C {
  public static void Test() {
    B b = new();
    b.Foo(); // <-- no warning raised
  }
}

The current docs say:

Typically, a member that is marked as obsolete should not override a member that is not marked as obsolete.

Which suggests this is a code smell or unidiomatic. More relevant (imo) is that the attribute will be ignored by compiler.

In this case I visited the info page to find out whether it would actually be a problem to ignore the warning. I don't fully understand the reasons why it's ignored, but I feel an explanation here would be helpful.

Thanks!

Page URL

https://learn.microsoft.com/en-us/dotnet/csharp/misc/cs0809

Content source URL

https://github.com/dotnet/docs/blob/main/docs/csharp/misc/cs0809.md

Document Version Independent Id

d83911ea-0426-fc82-622a-3c607d7b787a

Article author

@BillWagner

Metadata

BillWagner commented 3 months ago

I've added this to our backlog, and I've added the "help wanted" label. We should mention this. It's because the declaration is in the base class, not the derived class.

BartoszKlonowski commented 2 months ago

@BillWagner I will work on this!

BillWagner commented 2 months ago

Hi @BartoszKlonowski

I've assigned it to you.

YoshiRulz commented 2 months ago

Can I instead request that the attribute is recognised? (ObsoleteAttribute is [AttributeUsage(..., Inherited = false)] after all...) My use-case is to indicate that the "generic" object.Equals(object?) overload shouldn't be used because, to preserve commutativity, it only mimics the IEquatable<TSelf>.Equals overload, and not the additional IEquatable<int>.Equals overload—that is, instance.Equals(1) may resolve to the wrong overload and always return false.

rhys-vdw commented 2 months ago

Can I instead request that the attribute is recognised?

Not an expert on how this all works, but I feel like the docs repo is the place for documenting what is, and this is a proposal to change the language.