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
18.97k stars 4.03k forks source link

BUG: IDE0051 not working when file contains `nameof()` #54972

Open LukeOwlclaw opened 3 years ago

LukeOwlclaw commented 3 years ago

Version Used: VS2019 16.10.3 C:\Program Files\dotnet\sdk\5.0.302\Sdks\Microsoft.NET.Sdk\targets..\analyzers\Microsoft.CodeAnalysis.CSharp.NetAnalyzers.dll

Steps to Reproduce:

  1. Create a new project (e.g. .NET Core 5 Console app)
  2. Add private int mTest = 5; to Program.cs

You'll see immediately: Message IDE0051 Private member 'Program.mTest' is unused

  1. Add to Main(): Console.WriteLine(nameof(Class1));

Actual Behavior:

Message IDE0051 is gone!

Expected Behavior:

Message IDE0051 should stay independent of nameof being used in file.

Screenshots: (for visualizing I set IDE0051 to "error" and used extension "Solution Error Visualizer"):

image

image

Same problem applies to IDE0052

LukeOwlclaw commented 3 years ago

The same problem happens when using the range operator ... For example write:

Console.WriteLine("Hello World!"[0..]);

And the IDE0051 error will disappear.