Skip to content

Remove the dead Roslyn 4.4 workaround from MA0099 - #1412

Merged
meziantou merged 1 commit into
mainfrom
feature/roslyn-version-guard-typo-949f1f
Sep 6, 2026
Merged

meziantou merged 1 commit into
mainfrom
feature/roslyn-version-guard-typo-949f1f

Conversation

@meziantou

Copy link
Copy Markdown
Owner

What

Removes a #if-guarded block from DoNotUseZeroToInitializeAnEnumValue (MA0099) whose guard used a constant that is never defined.

#if !ROSLYN4_5_OR_GREATER
    if (operation.Syntax.IsKind(Microsoft.CodeAnalysis.CSharp.SyntaxKind.Attribute))
        return;
#endif

Why

The constants come from the Meziantou.Framework.Roslyn package and are named ROSLYN_<major>_<minor>_OR_GREATER. ROSLYN4_5_OR_GREATER is missing the underscore after ROSLYN — it is the only one of the nine ROSLYN*/CSHARP* identifiers used in a #if across src and tests that is misspelled.

An undefined constant is not an error for the C# compiler, it is simply false, so ! made the condition always true. The block was therefore compiled into all five builds rather than excluded from them — the typo inverted the guard rather than merely disabling it.

The workaround comes from #526 and only applies to Roslyn 4.4 and below, where the conversion of a default parameter value in an attribute had the Attribute node as its syntax. The minimum supported version is now 4.8, so spelling the constant correctly would exclude the block from every supported build. The code is dead either way, which is why this removes it rather than repairing the typo.

For reviewers

Because the block was live rather than skipped, removing it changes the compiled code, so this needed test evidence rather than inspection. ImplicitParameterInAttribute — the test added alongside the workaround — covers both the implicit default value the block was meant to ignore and the explicit 0 that must still be reported. It passes on every supported version, confirming the block never matched.

Verified:

  • dotnet build succeeds with 0 warnings across roslyn4.8, 4.14, 5.0, 5.6 and 5.9.
  • The 248 DoNotUseZeroToInitializeAnEnumValueTests cases pass on each of the five versions.
  • dotnet run --project src/DocumentationGenerator exits 0 with no markdown changes.

The guard was written as `#if !ROSLYN4_5_OR_GREATER`, but the constants the
Meziantou.Framework.Roslyn package defines are named ROSLYN_<major>_<minor>_OR_GREATER.
ROSLYN4_5_OR_GREATER, missing the underscore after ROSLYN, is never defined,
and an undefined constant is not an error for the C# compiler: it is simply
false. The negation therefore made the condition always true, so the
workaround was compiled into every supported Roslyn version instead of none
of them.

The workaround comes from #526 and only applies to Roslyn 4.4 and below,
where the conversion of a default parameter value in an attribute had the
Attribute node as its syntax. The minimum supported version is 4.8, so
spelling the constant correctly would exclude the block from all five builds:
the code is dead either way, and removing it is the fix rather than repairing
the typo.

The block was live, not skipped, so its removal is a change to the compiled
code. ImplicitParameterInAttribute, the test added with the workaround, covers
both the implicit default value it was meant to ignore and the explicit zero
that must still be reported; it passes on Roslyn 4.8 through 5.9, confirming
the block never matched on any supported version.
@meziantou
meziantou enabled auto-merge (squash) September 6, 2026 04:22
@meziantou
meziantou merged commit 9a30a7c into main Sep 6, 2026
13 checks passed
@meziantou
meziantou deleted the feature/roslyn-version-guard-typo-949f1f branch September 6, 2026 04:22
This was referenced Sep 6, 2026
This was referenced Sep 17, 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.

1 participant