Description
This issue has been moved from a ticket on Developer Community.
[severity:It's more difficult to complete my work]
When using the collection expression to initialize a list, it somehow suppresses IDE0052 on all fields in the class. Copying the example directly from the IDE0052 page, adding a list initialized with []
causes the warning to disappear for all unused fields. Even if the list initializer is just used on a private variable inside a method, all the unrelated, class-level fields lose their warnings.
I noticed this problem shortly after removing ReSharper, but it might be unrelated.
public class C
{
// this is the only new line
private static readonly List<string> _someStringList = [];
// IDE0052 is no longer reported for any of these fields
private readonly int _field1;
private int _field2;
private int Property { get; set; }
public C()
{
_field1 = 0;
}
public void SetMethod()
{
_field2 = 0;
Property = 0;
}
}
public class CLocal
{
// IDE0052 is no longer reported for any of these fields
private readonly int _field1;
private int _field2;
private int Property { get; set; }
public CLocal()
{
_field1 = 0;
}
public void SetMethod()
{
// this is the only new line
List<string> someStringList = [];
_field2 = 0;
Property = 0;
}
}
public class COld
{
// With the old list initializer, the warning is still reported.
private static readonly List<string> _someStringList = new List<string>
{
"a",
"b",
};
// IDE0052: Remove unread private members
private readonly int _field1;
private int _field2;
private int Property { get; set; }
public COld()
{
_field1 = 0;
}
public void SetMethod()
{
_field2 = 0;
Property = 0;
}
}
Original Comments
Feedback Bot on 10/22/2024, 06:52 PM:
We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.
Runtong Liu (Centific Technologies Inc) [MSFT] on 10/22/2024, 10:55 PM:
Thank you for taking the time to log this issue!
Do you still repro this issue now? I can’t repro this issue in VS2022 Enterprise(17.11.5). Details see below gif. Could you try to update to the latest version and check if this issue still exists?
We look forward to hearing from you!
Andrew Ramsay on 10/24/2024, 06:35 AM:
I am a few versions behind (though well into the C#12 era). I was also reproducing my example at the bottom of the file where I first noticed the issue. I will update and see if that fixes it. If not, I will try to reproduce in a brand new project. I will report back what I find when that is complete.
Andrew Ramsay on 10/24/2024, 09:02 AM:
Ok, I updated visual studio, and my example now behaves like yours. However, the actual real code I was working on is still failing to show the IDE warning. I was able to reproduce this in a new class library project with a single class. It seems using nameof() with certain elements is also suppressing IDE0052. I’m not sure if the collection initializer was a red herring all along or if it was an unrelated bug fixed in the VS upgrade. But the issue is now present with no collection initializer at all, just the nameof.
I am now on this version:
Microsoft Visual Studio Enterprise 2022 (64-bit) - Current
Version 17.11.5
nameof with a property or a field doesn’t have any negative impact.
nameof with a Type suppresses IDE0052 across the entire class (I tried Console and Int32 to ensure it applied to both value and ref types)
nameof with a namespace component suppresses the warning
nameof with a method suppresses the warning
namespace IdeBug;
// Code with violations
public class SampleClass
{
// IDE0052: Remove unread private members
private readonly int _field1;
private int _field2;
private int Property { get; set; }public SampleClass(
)
{
_field1 = 0;// Uncommenting any one of these lines will suppress IDE0052 for all fields and properties in the class
//Console.WriteLine(nameof(Console));
//Console.WriteLine(nameof(Int32));
//Console.WriteLine(nameof(SetMethod));
//Console.WriteLine(nameof(Console.WriteLine));
//Console.WriteLine(nameof(IdeBug));// Uncommenting any one of these lines suppress IDE0052 for _field1 only. This is not a true usage of the field, so is probably a bug to suppress it. But doesn't break the warning for the entire class, so likely is unrelated.
//Console.WriteLine(nameof(_field1));
//Console.WriteLine(nameof(SampleClass._field1));
}public void SetMethod()
{
_field2 = 0;
Property = 0;
}
}
Runtong Liu (Centific Technologies Inc) [MSFT] on 10/22/2024, 10:56 PM:
Runtong Liu (Centific Technologies Inc) [MSFT] on 10/28/2024, 10:40 PM:
Thanks for your sharing the detail steps, I can reproduce this issue through your shared steps and this issue has been escalated for further investigation, if there is any process, I will inform you immediately.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status