Skip to content

Fix MA0027 not firing for derived exception types#1026

Merged
meziantou merged 2 commits intomainfrom
copilot/fix-ma0027-reporting-issue
Feb 21, 2026
Merged

Fix MA0027 not firing for derived exception types#1026
meziantou merged 2 commits intomainfrom
copilot/fix-ma0027-reporting-issue

Conversation

Copy link
Contributor

Copilot AI commented Feb 21, 2026

MA0027 failed to report throw ex when ex was a derived exception type (e.g. InvalidOperationException). It only triggered for exactly System.Exception.

Root cause: Roslyn wraps the ILocalReferenceOperation in an implicit IConversionOperation (upcasting to Exception) for derived types. The analyzer's early-exit check operation.Exception is not ILocalReferenceOperation never unwrapped that conversion, so derived types were silently skipped.

Fix:

  • Call UnwrapImplicitConversionOperations() before the pattern match in DoNotRemoveOriginalExceptionFromThrowStatementAnalyzer
// Before — missed derived types due to implicit upcast wrapping
if (operation.Exception is not ILocalReferenceOperation localReferenceOperation)

// After
if (operation.Exception.UnwrapImplicitConversionOperations() is not ILocalReferenceOperation localReferenceOperation)
  • Added test ShouldReportDiagnostic_DerivedException covering the catch (InvalidOperationException ex) { throw ex; } case
Original prompt

This section details on the original issue you should resolve

<issue_title>MA0027 is only reported when Exception is caught</issue_title>
<issue_description>### Version of the Meziantou.Analyzer NuGet package

3.0.11

Rule Identifier

MA0027

Target Framework

.NET 10.0

C# Language version

C# 14

Description

MA0027 appears to only be reported if the caught exception is exact of the exact type System.Exception and not for a derived type.

Reproduction Steps

Minimal code:

try
{
}
catch (InvalidOperationException ex)
{
    throw ex; // Only CA2200 and S3445 are reported
}

Other information

I see the following options:

  • Fix MA0027
  • Deprecate it in favor of CA2200</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

…plicit conversions

Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix reporting issue for MA0027 rule Fix MA0027 not firing for derived exception types Feb 21, 2026
Copilot AI requested a review from meziantou February 21, 2026 18:21
@meziantou meziantou marked this pull request as ready for review February 21, 2026 18:31
@meziantou meziantou merged commit 7a0e109 into main Feb 21, 2026
12 checks passed
@meziantou meziantou deleted the copilot/fix-ma0027-reporting-issue branch February 21, 2026 18:31
This was referenced Feb 21, 2026
This was referenced Feb 27, 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.

MA0027 is only reported when Exception is caught

2 participants