Skip to content

Add code fixer for MA0173 (Use LazyInitializer.EnsureInitialized)#1071

Merged
meziantou merged 2 commits intomainfrom
copilot/fixer-for-ma0173
Mar 30, 2026
Merged

Add code fixer for MA0173 (Use LazyInitializer.EnsureInitialized)#1071
meziantou merged 2 commits intomainfrom
copilot/fixer-for-ma0173

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 30, 2026

MA0173 flagged Interlocked.CompareExchange(ref _field, new T(), null) but provided no automated fix.

Changes

  • New fixer UseLazyInitializerEnsureInitializeFixer: rewrites the flagged call to LazyInitializer.EnsureInitialized, wrapping the factory expression in a lambda and dropping the null comparand argument. Supports fix-all via BatchFixer.
  • Tests: wired up the fixer in the existing test builder and added ShouldFixCodeWith assertions for the four diagnostic cases.
  • Docs: README code-fix column for MA0173 updated from ❌ to ✔️ (auto-generated).

Example

// Before
Interlocked.CompareExchange(ref _field, new Sample(), null);

// After
System.Threading.LazyInitializer.EnsureInitialized(ref _field, () => new Sample());

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • 1s1vsblobprodcus386.vsblob.vsassets.io
    • Triggering command: /usr/share/dotnet/dotnet dotnet restore --no-dependencies /home/REDACTED/work/Meziantou.Analyzer/Meziantou.Analyzer/Meziantou.Analyzer.slnx --packages /tmp/codeql-scratch-8ad8dc5536a632e8/dbs/csharp/working/packages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal /p:TargetFrameworkRootPath=/tmp/codeql-scratch-8ad8dc5536a632e8/dbs/csharp/working/emptyFakeDotnetRoot /p:NetCoreTargetingPackRoot=/tmp/codeql-scratch-8ad8dc5536a632e8/dbs/csharp/working/emptyFakeDotnetRoot /p:AllowMissingPrunePackageData=true (dns block)
  • pdfvsblobprodcus380.vsblob.vsassets.io
    • Triggering command: /usr/share/dotnet/dotnet dotnet restore --no-dependencies /home/REDACTED/work/Meziantou.Analyzer/Meziantou.Analyzer/Meziantou.Analyzer.slnx --packages /tmp/codeql-scratch-8ad8dc5536a632e8/dbs/csharp/working/packages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal /p:TargetFrameworkRootPath=/tmp/codeql-scratch-8ad8dc5536a632e8/dbs/csharp/working/emptyFakeDotnetRoot /p:NetCoreTargetingPackRoot=/tmp/codeql-scratch-8ad8dc5536a632e8/dbs/csharp/working/emptyFakeDotnetRoot /p:AllowMissingPrunePackageData=true (dns block)
  • st8vsblobprodcus339.vsblob.vsassets.io
    • Triggering command: /usr/share/dotnet/dotnet dotnet restore --no-dependencies /home/REDACTED/work/Meziantou.Analyzer/Meziantou.Analyzer/Meziantou.Analyzer.slnx --packages /tmp/codeql-scratch-8ad8dc5536a632e8/dbs/csharp/working/packages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal /p:TargetFrameworkRootPath=/tmp/codeql-scratch-8ad8dc5536a632e8/dbs/csharp/working/emptyFakeDotnetRoot /p:NetCoreTargetingPackRoot=/tmp/codeql-scratch-8ad8dc5536a632e8/dbs/csharp/working/emptyFakeDotnetRoot /p:AllowMissingPrunePackageData=true (dns block)
    • Triggering command: /usr/share/dotnet/dotnet dotnet restore --no-dependencies /home/REDACTED/work/Meziantou.Analyzer/Meziantou.Analyzer/tests/Meziantou.Analyzer.Test/Meziantou.Analyzer.Test.csproj --packages /tmp/codeql-scratch-8ad8dc5536a632e8/dbs/csharp/working/packages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal /p:TargetFrameworkRootPath=/tmp/codeql-scratch-8ad8dc5536a632e8/dbs/csharp/working/emptyFakeDotnetRoot /p:NetCoreTargetingPackRoot=/tmp/codeql-scratch-8ad8dc5536a632e8/dbs/csharp/working/emptyFakeDotnetRoot /p:AllowMissingPrunePackageData=true (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI changed the title [WIP] Add fixer for MA0173 Add code fixer for MA0173 (Use LazyInitializer.EnsureInitialized) Mar 30, 2026
Copilot AI requested a review from meziantou March 30, 2026 15:37
@meziantou meziantou marked this pull request as ready for review March 30, 2026 16:25
@meziantou meziantou merged commit fb85fc4 into main Mar 30, 2026
13 checks passed
@meziantou meziantou deleted the copilot/fixer-for-ma0173 branch March 30, 2026 16:48
This was referenced Mar 30, 2026
This was referenced May 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants