Fix MA0163 false positive when the ProcessStartInfo is assigned to Process.StartInfo
Merged
ME
2mo ago+155+155+012345678901234567890123456789-20-20-01234567890123456789
What
MA0163 tracks the assignments made after a ProcessStartInfo is created, but only when the instance is stored in a local, a parameter, or a member of this. When it is stored in a property of another instance, such as process.StartInfo, the later assignments are ignored:
var process = new Process();
process.StartInfo = new ProcessStartInfo("cmd") { RedirectStandardOutput = true }; // MA0163 reported
process.StartInfo.UseShellExecute = false;
process.Start();
The same happens with new Process { StartInfo = new ProcessStartInfo("cmd") { RedirectStandardOutput = true } } followed by process.StartInfo.UseShellExecute = false.
Expected: no diagnostic. Actual: MA0163 is reported.
Fix
- The instance is now identified by the chain of members that references it (e.g.
[process, StartInfo]) instead of a single symbol, soprocess.StartInfo.UseShellExecute = falseis matched withprocess.StartInfo = new ProcessStartInfo(...). - In an object initializer (
new Process { StartInfo = ... }), the implicit receiver is resolved to the variable the created object is assigned to. Before, it was handled likethis, so the assignment was only matched withthis.StartInfo. process.Start()is now recognized as the start of the process configured byprocess.StartInfo, so the assignments made after it are ignored, the same way as forProcess.Start(psi).
The documentation has a compliant example for process.StartInfo.
Tests
- Added
..._assigned_to_the_start_info_propertyand..._assigned_in_the_process_initializer(both failed before the fix),..._set_on_the_start_info_property_after_the_process_is_startedand..._set_on_the_start_info_property_of_another_process(still reported). ProcessStartAnalyzerTestspasses on roslyn5.9 and roslyn4.8 (40 tests each).