Skip to content

Repository files navigation

Validation - Analyzer diagnostics for loop iterators captured in closures

Scenario: dotnet/aspnetcore #68490

Manual: dotnet/aspnetcore #68479

Build tested

.NET SDK 11.0.100-preview.7.26381.103

The scenario requires .NET 11 Preview 7 or later.

Sample application

BlazorLoopClosureDemo/ and BlazorLoopClosureDemo.Client/ contain a Blazor Web App using Interactive Server rendering for the validation pages.

Start at /samples/bl0014 to access the complete sample set.

Expected BL0014 warnings

Page Route Purpose
@onclick lambda /samples/bl0014/flagged-onclick Captures the for iterator in a click handler
Lambda with event args /samples/bl0014/flagged-args Captures the iterator alongside event arguments
@bind indexer /samples/bl0014/flagged-bind Uses the iterator as a two-way binding index
Child EventCallback /samples/bl0014/flagged-eventcallback Captures the iterator in a child callback
Child content /samples/bl0014/flagged-childcontent Renders the iterator inside ChildContent

Expected no BL0014 warning

Page Route Purpose
Copied local /samples/bl0014/safe-copied-local Copies the iterator before capturing it
foreach over a list /samples/bl0014/safe-foreach Uses a foreach iteration variable
foreach over Enumerable.Range /samples/bl0014/safe-range Uses a range iteration variable
Iterator outside a closure /samples/bl0014/safe-loop-body-only Reads the iterator only in the loop body

How to run

From the repository root:

dotnet run --project BlazorLoopClosureDemo/BlazorLoopClosureDemo.csproj

Open the HTTPS URL printed in the console, then navigate to /samples/bl0014.

How to verify the build diagnostic

dotnet clean BlazorLoopClosureDemo.sln -nologo -v:minimal
dotnet build BlazorLoopClosureDemo.sln -t:Rebuild -nologo -v:minimal

Expected result: the five flagged pages produce BL0014 warnings naming the captured iterator. The copied-local, foreach, Enumerable.Range, and loop-body-only pages produce no BL0014 warning.

Configuration tested

  • Blazor Web App - Interactive Server
  • Visual Studio 2022 and command-line builds
  • Published Release output (dotnet publish -c Release)

The diagnostic is produced at compile time and does not vary by render mode.

Evidence

About

Application to test Analyzer diagnostics for loop iterators captured in closures

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages