diff --git a/Directory.Packages.props b/Directory.Packages.props index 6bbed1c..3d8237d 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -10,7 +10,7 @@ - + @@ -18,9 +18,9 @@ - + - + @@ -38,7 +38,7 @@ - + diff --git a/lode/dotnet/analyzers/authoring.md b/lode/dotnet/analyzers/authoring.md index 38b25e6..d7b90e9 100644 --- a/lode/dotnet/analyzers/authoring.md +++ b/lode/dotnet/analyzers/authoring.md @@ -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 diff --git a/lode/dotnet/analyzers/rule-details-union.md b/lode/dotnet/analyzers/rule-details-union.md index 689bc05..610eb91 100644 --- a/lode/dotnet/analyzers/rule-details-union.md +++ b/lode/dotnet/analyzers/rule-details-union.md @@ -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 @@ -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 diff --git a/src/E128.Analyzers/AnalyzerReleases.Shipped.md b/src/E128.Analyzers/AnalyzerReleases.Shipped.md index 5471be7..0525ce5 100644 --- a/src/E128.Analyzers/AnalyzerReleases.Shipped.md +++ b/src/E128.Analyzers/AnalyzerReleases.Shipped.md @@ -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 diff --git a/src/E128.Analyzers/AnalyzerReleases.Unshipped.md b/src/E128.Analyzers/AnalyzerReleases.Unshipped.md index 9a5bb29..c75fee0 100644 --- a/src/E128.Analyzers/AnalyzerReleases.Unshipped.md +++ b/src/E128.Analyzers/AnalyzerReleases.Unshipped.md @@ -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 diff --git a/src/E128.Analyzers/Design/UnionValueReadAnalyzer.cs b/src/E128.Analyzers/Design/UnionValueReadAnalyzer.cs index c6e3eac..0c21207 100644 --- a/src/E128.Analyzers/Design/UnionValueReadAnalyzer.cs +++ b/src/E128.Analyzers/Design/UnionValueReadAnalyzer.cs @@ -11,13 +11,12 @@ namespace E128.Analyzers.Design; /// E128221: Reports a Value read or a case-type cast on a union /// outside a pattern match. 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. +/// pattern reads the value without boxing and stays checked against the case list. A +/// compiler-declared union exposes no TryGetValue member. /// /// -/// No code fix is provided — converting a read or a cast into a pattern or a -/// TryGetValue 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. /// [DiagnosticAnalyzer(LanguageNames.CSharp)] public sealed class UnionValueReadAnalyzer : DiagnosticAnalyzer @@ -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); diff --git a/src/E128.Analyzers/E128.Analyzers.csproj b/src/E128.Analyzers/E128.Analyzers.csproj index 6dbd6df..c435bbe 100644 --- a/src/E128.Analyzers/E128.Analyzers.csproj +++ b/src/E128.Analyzers/E128.Analyzers.csproj @@ -23,7 +23,7 @@ - 3.1.0 + 3.1.1 true false true diff --git a/src/E128.Analyzers/README.md b/src/E128.Analyzers/README.md index 4559e0e..67bdff1 100644 --- a/src/E128.Analyzers/README.md +++ b/src/E128.Analyzers/README.md @@ -5,7 +5,7 @@ Roslyn analyzers and code fixes that enforce opinionated .NET conventions at com ## Installation ```xml - + ``` > `PrivateAssets="all"` keeps the analyzers out of your consumers' dependency graph. @@ -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.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) @@ -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