Fix MA0163 false positive when the ProcessStartInfo is assigned to Process.StartInfo

Merged
ME
2mo ago
+155-20

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, so process.StartInfo.UseShellExecute = false is matched with process.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 like this, so the assignment was only matched with this.StartInfo.
  • process.Start() is now recognized as the start of the process configured by process.StartInfo, so the assignments made after it are ignored, the same way as for Process.Start(psi).

The documentation has a compliant example for process.StartInfo.

Tests

  • Added ..._assigned_to_the_start_info_property and ..._assigned_in_the_process_initializer (both failed before the fix), ..._set_on_the_start_info_property_after_the_process_is_started and ..._set_on_the_start_info_property_of_another_process (still reported).
  • ProcessStartAnalyzerTests passes on roslyn5.9 and roslyn4.8 (40 tests each).