Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@
<!-- Analyzers -->
<PackageVersion Include="AsyncFixer" Version="2.1.0" />
<PackageVersion Include="IDisposableAnalyzers" Version="4.0.8" />
<PackageVersion Include="Meziantou.Analyzer" Version="3.0.339" />
<PackageVersion Include="Meziantou.Analyzer" Version="3.0.341" />
<PackageVersion Include="Microsoft.VisualStudio.Threading.Analyzers" Version="18.12.1" />
<PackageVersion Include="Roslynator.Analyzers" Version="5.0.1" />
<PackageVersion Include="Roslynator.CodeAnalysis.Analyzers" Version="5.0.1" />
<PackageVersion Include="Roslynator.Formatting.Analyzers" Version="5.0.1" />
<PackageVersion Include="SharpSource" Version="1.33.4" />
<PackageVersion Include="SonarAnalyzer.CSharp" Version="10.36.0.15337" />
<!-- Roslyn (analyzer authoring + code style) -->
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="5.9.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="5.12.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.PublicApiAnalyzers" Version="5.6.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.CodeStyle" Version="5.9.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.CodeStyle" Version="5.12.0" />
<PackageVersion Include="PolySharp" Version="1.17.1" />
<!-- Architecture Testing -->
<PackageVersion Include="TngTech.ArchUnitNET.xUnitV3" Version="0.13.5" />
Expand All @@ -38,7 +38,7 @@
<PackageVersion Include="System.CommandLine" Version="3.0.0" />
<!-- Transitive pins -->
<!-- CSharp.Workspaces: direct dependency of E128.Analyzers.CodeFixers -->
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="5.9.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="5.12.0" />
<!-- Transitive pins: deprecated or major-version-behind packages -->
<PackageVersion Include="System.Security.Cryptography.Pkcs" Version="11.0.0" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion lode/dotnet/analyzers/authoring.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Analyzer Authoring
*Updated: 2026-11-12T15:07:30Z*
*Updated: 2026-11-16T14:10:41Z*

Rules for a new or changed rule in `src/E128.Analyzers`. Read
[new-analyzer-checklist.md](new-analyzer-checklist.md) before you write the
Expand Down
9 changes: 6 additions & 3 deletions lode/dotnet/analyzers/rule-details-union.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Rule Detail Notes: Union and Hierarchy Rules
*Updated: 2026-11-14T01:26:59Z*
*Updated: 2026-11-16T14:14:54Z*

Detection shape for the union rule family E128219 to E128221. The complete
rule table lives in `src/E128.Analyzers/README.md`. The union language surface
Expand Down Expand Up @@ -71,8 +71,11 @@ Registers on `SimpleMemberAccessExpression` and `CastExpression`.
union, because a dictionary or a task carries a `Value`-shaped member too.
- A cast whose operand is a union and whose target type differs from the
operand type.
- No code fix. Converting a read or a cast into a pattern or a `TryGetValue`
call changes control flow.
- No code fix. Converting a read or a cast into a pattern changes control flow.
- A compiler-declared union exposes no `TryGetValue` member and no `HasValue`
property. Probed on net11.0 at `LangVersion` `latest` and `preview`, with
Roslyn 5.12.0. `Value` and a case pattern are the whole member surface, so
the rule text names the pattern only.

### Test fixtures

Expand Down
9 changes: 9 additions & 0 deletions src/E128.Analyzers/AnalyzerReleases.Shipped.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## Release 3.1.1

### Changed Rules

Rule ID | New Category | New Severity | Old Category | Old Severity | Notes
--------|--------------|--------------|--------------|--------------|-------
E128220 | E128-Design | Warning | E128-Design | Warning | Also reports a when-guarded discard or var arm because a guard never closes the arm
E128221 | E128-Design | Warning | E128-Design | Warning | Message names a case pattern only, because a compiler-declared union exposes no TryGetValue member

## Release 3.1.0

### New Rules
Expand Down
1 change: 0 additions & 1 deletion src/E128.Analyzers/AnalyzerReleases.Unshipped.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ Rule ID | Category | Severity | Notes

Rule ID | New Category | New Severity | Old Category | Old Severity | Notes
--------|--------------|--------------|--------------|--------------|-------
E128220 | E128-Design | Warning | E128-Design | Warning | Also reports a when-guarded discard or var arm because a guard never closes the arm

### Removed Rules

Expand Down
11 changes: 5 additions & 6 deletions src/E128.Analyzers/Design/UnionValueReadAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ namespace E128.Analyzers.Design;
/// E128221: Reports a <c lang="csharp">Value</c> read or a case-type cast on a union
/// outside a pattern match. A <c lang="csharp">Value</c> read boxes the case and skips
/// the case set, and a cast trusts the runtime state instead of the type system. A case
/// pattern, or <c lang="csharp">TryGetValue</c>, reads the value without boxing and stays
/// checked against the case list.
/// pattern reads the value without boxing and stays checked against the case list. A
/// compiler-declared union exposes no <c lang="csharp">TryGetValue</c> member.
/// </summary>
/// <remarks>
/// No code fix is provided — converting a read or a cast into a pattern or a
/// <c lang="csharp">TryGetValue</c> call changes control flow, which cannot be automated
/// safely.
/// No code fix is provided — converting a read or a cast into a pattern changes control
/// flow, which cannot be automated safely.
/// </remarks>
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class UnionValueReadAnalyzer : DiagnosticAnalyzer
Expand All @@ -27,7 +26,7 @@ public sealed class UnionValueReadAnalyzer : DiagnosticAnalyzer
private static readonly DiagnosticDescriptor Rule = new(
DiagnosticId,
"Union case read outside a pattern match",
"Read the union case through a pattern or TryGetValue instead of a Value read or cast — the read skips the case set and boxes the value",
"Read the union case through a pattern instead of a Value read or cast. The read skips the case set and boxes the value.",
"E128-Design",
DiagnosticSeverity.Warning,
true);
Expand Down
2 changes: 1 addition & 1 deletion src/E128.Analyzers/E128.Analyzers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

<!-- NuGet Packaging -->
<PropertyGroup>
<Version>3.1.0</Version>
<Version>3.1.1</Version>
<IsPackable>true</IsPackable>
<IncludeBuildOutput>false</IncludeBuildOutput>
<DevelopmentDependency>true</DevelopmentDependency>
Expand Down
13 changes: 7 additions & 6 deletions src/E128.Analyzers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Roslyn analyzers and code fixes that enforce opinionated .NET conventions at com
## Installation

```xml
<PackageReference Include="E128.Analyzers" Version="3.1.0" PrivateAssets="all" />
<PackageReference Include="E128.Analyzers" Version="3.1.1" PrivateAssets="all" />
```

> `PrivateAssets="all"` keeps the analyzers out of your consumers' dependency graph.
Expand Down Expand Up @@ -3145,15 +3145,16 @@ var text = result switch

Flags a `Value` read on a union expression and a cast of a union expression to
a case type. A `Value` read boxes the case and skips the case set, and a cast
trusts the runtime state instead of the type system. A case pattern, or
`TryGetValue`, reads the value without boxing and stays checked against the
case list.
trusts the runtime state instead of the type system. A case pattern reads the
value without boxing and stays checked against the case list. A
compiler-declared union exposes no `TryGetValue` member, so the pattern is the
only checked read.

A member access whose receiver is not a union stays silent, so a
`Dictionary<K,V>.Values` read or a `Task` result never reports.

No code fix. The rewrite changes control flow, so the developer writes the
pattern or the `TryGetValue` call.
pattern.

```csharp
// Before (warns)
Expand All @@ -3162,7 +3163,7 @@ var cast = (GreetingError)result;

// After
if (result is Greeting greeting) { }
if (result.TryGetValue(out var value) && value is GreetingError error) { }
if (result is GreetingError error) { }
```

### E128222: Path.Combine of a root and a run-time leaf
Expand Down