Skip to content

Fix bare #pragma warning disable in AvoidUsingRedundantElseAnalyzer - #1339

Merged
meziantou merged 1 commit into
mainfrom
feature/meziantou-analyzer-1338-eafec4
Aug 26, 2026
Merged

Fix bare #pragma warning disable in AvoidUsingRedundantElseAnalyzer#1339
meziantou merged 1 commit into
mainfrom
feature/meziantou-analyzer-1338-eafec4

Conversation

@meziantou

Copy link
Copy Markdown
Owner

Fixes #1338

What changed

src/Meziantou.Analyzer/Rules/AvoidUsingRedundantElseAnalyzer.cs — the #pragma warning disable in FindLocalIdentifiersIn had no warning ID, so it disabled every warning from line 96 to the end of the file. The matching #pragma warning restore IDE0010 only lifted IDE0010, so the blanket disable was never actually restored. The ID was lost in f807685 ("Update .NET SDK 9.0", Nov 2024). Restored it to #pragma warning disable IDE0010 // Add missing cases so the suppression is scoped to the switch again (and dropped the trailing whitespace the bare pragma carried).

.github/workflows/ci.yml — new check_pragmas job that fails on any #pragma warning disable with an empty warning list, annotating the offending file and line. Added to deploy's needs so it blocks publishing. This is the cheap recurrence guard suggested in the issue; nothing in the repo linted for bare pragmas before.

Why it matters

This is analyzer source that runs inside every consumer's compiler. Nullable warnings, CA rules and the project's own dogfooded MA rules were all off for FindLocalIdentifiersIn — the helper deciding which identifiers MA0160 treats as captured. A regression there would have been flagged by neither the build nor CI.

Verification

  • All five analyzer projects (roslyn4.8roslyn5.9) build with 0 warnings — nothing was actually being hidden by the blanket disable, so no follow-up suppressions were needed.
  • AvoidUsingRedundantElseAnalyzerTests: 27/27 passed on both roslyn5.9 and roslyn4.8.
  • The CI script was run locally under pwsh in both directions: clean on this branch (exit 0), and it correctly flags AvoidUsingRedundantElseAnalyzer.cs:96 when the bare pragma is reintroduced (exit 1).
  • dotnet run --project src/DocumentationGenerator exits 0 — no markdown changes.

Note for the reviewer

The CI check is limited to bare disable, as the issue specified. A bare #pragma warning restore is also legal and can silently re-enable warnings suppressed by an enclosing region, but there are none in the repo today — happy to widen the check if you'd like it to cover those too.

…1338)

The `#pragma warning disable` in `FindLocalIdentifiersIn` lost its warning
ID in f807685, so it disabled every warning from that point to the end of
the file, while the matching `#pragma warning restore IDE0010` only lifted
IDE0010. Restore the ID so the suppression is scoped to the switch again.

Add a `check_pragmas` CI job that rejects any `#pragma warning disable`
with an empty warning list, so the ID cannot be silently dropped again.
@meziantou
meziantou merged commit 318da60 into main Aug 26, 2026
13 checks passed
@meziantou
meziantou deleted the feature/meziantou-analyzer-1338-eafec4 branch August 26, 2026 17:55
This was referenced Aug 26, 2026
This was referenced Aug 31, 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.

Bare #pragma warning disable in AvoidUsingRedundantElseAnalyzer silences every warning to end of file

1 participant