In my recent work on the dotnet/roslyn repository, I addressed two issues related to the handling of nullability in the Extract Method refactoring. The Roslyn repository is the home of the .NET Compiler Platform, which provides open-source C# and Visual Basic compilers with rich code analysis APIs.
The problems were identified in issues #38127 and #65456. Both issues were related to the Extract Method refactoring not correctly handling nullability in certain scenarios.
Here's an example that illustrates the problem:
public class Test
{
public int M(Dictionary<string, string?> v)
{
return v.Count;
}
}
When running the Extract Method refactoring on the return statement line, the expected behavior was that the new method's parameter type should be Dictionary<string, string?>. However, the actual behavior was that the new method's parameter type was Dictionary<string, string>, missing the nullability annotation on the second generic parameter. This resulted in an immediate nullability error.
The solution involved modifying the Extract Method refactoring to correctly handle nullability in these scenarios. This was a complex task that required changes in several parts of the codebase. The changes ensured that nullability annotations were correctly preserved when extracting methods, and that nullability errors were correctly reported.