dotnet format reports false positive for CS1503
Describe the bug
In a project where I use F# projects and C# projects I have the following code:
public class Test
{
public Guid Id { get; }
public string Text { get; } = "";
}
var test = Array.Empty<Test>();
var testById = test.ToDictionary(x => x.Id);
_ = testById[Guid.Empty].Text; // <-- CS1503 with dotnet format
When I run dotnet format --verify-no-changes I get the following error when Test is specified in an F# project:
dotnetformatissue\issue\Program.cs(7,31): error CS1503: Argument 1: cannot convert from 'System.Guid' to '?'
The compiler doesn't give this error and when Test is declared in a C# project the error doesn't appear either.
A workaround is to not use var testById but Dictionary<Guid, Test> testById.
(I also reported this in dotnet/format#1516, but I believe this is still the right repo right?)
To Reproduce
A full working version of the code can be found at: https://github.com/dnperfors/dotnetformatissue
Exceptions (if any)
Further technical details
dotnet --info
.NET SDK (reflecting any global.json):
Version: 6.0.100
Commit: 9e8b04bbff
Runtime Environment:
OS Name: Windows
OS Version: 10.0.22000
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\6.0.100\
Host (useful for support):
Version: 6.0.0
Commit: 4822e3c3aa
.NET SDKs installed:
5.0.401 [C:\Program Files\dotnet\sdk]
5.0.403 [C:\Program Files\dotnet\sdk]
6.0.100 [C:\Program Files\dotnet\sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 5.0.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 5.0.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 5.0.12 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
We have a simmilar issue in our project....
public class Pool < T > {
public IEnumerable < InstanceValuePair < U >> ForEach < U > (IList < U > items) {
// some other code
yield
return new InstanceValuePair < U > {
Instance = _instances[i],
Value = items[i]
};
// some more other code
}
public struct InstanceValuePair < U > {
public T Instance;
public U Value;
}
}
And later
foreach(var job in _hireButtonPool.ForEach(jobs.ToList())) {
string item = job.Value; // error CS1503: Argument 2: cannot convert from 'U' to 'string'
}
However when doing the exact same BUT expliciting the foreach method parameter, it works fine.
foreach(var job in _hireButtonPool.ForEach<string>(jobs.ToList())) {
string item = job.Value; // no error
}
Also in the same way, this only appear using dotnet-format. Compilation (In unity) and runtime work fine.
I have a simular issue but it only happens if I use --no-restore on dotnet format.
I have seen issues related to this due to how output directory or paths were defined in props or targets, so if you have issues with false positives consider whether it could be due to that. That is you try to get common build output directory you might see issues, as far as I can tell the approach described in https://nietras.com/2022/01/24/bendingdotnet-improved-common-flat-build-output/ and related works.