When using the new new() syntax for target type newing things in C# 9, the analyzer throws S1172 thinking the ctor of that type is unused when it is (it's just called using target typed new)
namespace UnluacNET
{
using System;
public class LBoolean
{
public static readonly LBoolean LTRUE = new(true);
public static readonly LBoolean LFALSE = new(false);
private LBoolean(bool value)
{
this.Value = value;
}
private bool Value { get; set; }
public override bool Equals(object obj)
=> this == obj;
public override int GetHashCode()
=> throw new NotImplementedException();
public override string ToString()
=> this.Value.ToString();
}
}
I expect the analyzer to properly pick up that target typed new is used and that the specific ctor is actually called and not offer to remove it. Removing it would result in a compile error due to that fact. True I could replace the ctor with an initializer on the static readonly variables to initialize the property, but it's shorter to just use the ctor.
The analyzer flags the ctor as unused and offers to delete it.
A test case for this problem can be found here: https://github.com/SonarSource/sonar-dotnet/blob/0dfe4f9bd5a457684f97c6ae97cfdb360c533b70/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/ClassNotInstantiatable.CSharp9.cs#L78-L87
Ah sorry I might have identified the number on it wrong.
Ah sorry I might have identified the number on it wrong.
No worries, thanks a lot four your feedback!
I came here to report this. Is there a place to track progress on the planned fix? Also, can you change the title of this issue to show the correct rule so it's easier to find?
Is there a place to track progress on the planned fix?
Not yet.
You can track MMF-2225. This issue should get fixed as part of it.
To be clear, there's no ETA for the MMF, but C# 9 support is already in progress as many things needs to be done on the topic.
Most helpful comment
I came here to report this. Is there a place to track progress on the planned fix? Also, can you change the title of this issue to show the correct rule so it's easier to find?