dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
19.02k stars 4.03k forks source link

CS0612 warning about obsolete member not suppressed when it is referenced by a ObsoleteAttribute placed on a method #23015

Open stakx opened 6 years ago

stakx commented 6 years ago

Version Used: Visual Studio 15.4.0

Steps to Reproduce:

  1. Compile the following C# code:
using System;

[Obsolete] partial class Old { }

[Obsolete("This fixture contains tests related to " + nameof(Old) + ", which is obsolete.")]
public class A
{
    public void Test()
    {
        var old = new Old();
    }
}

public class B
{
    [Obsolete("This test is related to " + nameof(Old) + ", which is obsolete.")]  // (*)
    public void Test()
    {
        var old = new Old();
    }
}
  1. Observe emitted build warnings.

Expected Behavior: No compiler warnings are emitted about Old being obsolete. (The two methods that reference Old are themselves marked [Obsolete]. The [Obsolete] attributes that themselves refer to Old don't trigger warnings, either.)

Actual Behavior: One warning CS0612: 'Old' is obsolete is emitted for the line marked with (*) above. Note that the warning is only triggered for the [Obsolete] attribute being placed on a method. When placed on a class, no warning is emitted. That doesn't seem right to me.

obsolete-warning

(This isn't specific to Visual Studio IntelliSense. The warning is also emitted when compiling on the command line.)

MaceWindu commented 5 years ago

should this issue also cover other attributes, that target obsolete member?

[Obsolete]
    class A
    {
    }

    class SomeAttribute : Attribute
    {
        public SomeAttribute(string _)
        {
        }
    }

    class B
    {
        [Obsolete]
        [Some(nameof(A))]
        public string Prop { get; }

        [field: Obsolete]
        [field: Some(nameof(A))]
        public string Prop2 { get; }
    }