Skip to content

Fix MA0071 false positive for else if chains with reachable prior branches#1166

Merged
meziantou merged 3 commits into
mainfrom
copilot/fix-redundant-else-check
May 20, 2026
Merged

Fix MA0071 false positive for else if chains with reachable prior branches#1166
meziantou merged 3 commits into
mainfrom
copilot/fix-redundant-else-check

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 20, 2026

MA0071 incorrectly flagged the final else in some else if chains, especially when conditions used method calls, leading to unsafe flattening suggestions. The rule now avoids diagnostics unless earlier branches in the same chain are proven to terminate unconditionally.

  • Analyzer logic: constrain else if flattening

    • Updated AvoidUsingRedundantElseAnalyzer to require that every preceding branch in the enclosing else if chain has an unreachable endpoint before considering the current else redundant.
    • This prevents reporting when control flow can still continue past an earlier if branch.
  • Regression coverage

    • Added a focused test reproducing the reported pattern:
      • first branch with method-call condition and reachable endpoint
      • second else if branch with continue
      • final else that must remain valid
    • Expected behavior: no MA0071 diagnostic.
  • Rule documentation

    • Clarified MA0071 behavior for else if chains: simplification applies only when previous branches also jump unconditionally.
if (sc.Verify("first", cmp) is not null)
{
    Console.WriteLine("Handled"); // reachable endpoint
}
else if (sc.Verify("second", cmp) is not null)
{
    Console.WriteLine("Handled");
    continue;
}
else
{
    Console.WriteLine("Not handled"); // must not be flagged as redundant
}

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • 1s1vsblobprodcus386.vsblob.vsassets.io
    • Triggering command: /usr/share/dotnet/dotnet dotnet restore --no-dependencies /home/REDACTED/work/Meziantou.Analyzer/Meziantou.Analyzer/Meziantou.Analyzer.slnx --packages /tmp/codeql-scratch-8ad8dc5536a632e8/dbs/csharp/working/packages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal /p:TargetFrameworkRootPath=/tmp/codeql-scratch-8ad8dc5536a632e8/dbs/csharp/working/emptyFakeDotnetRoot /p:NetCoreTargetingPackRoot=/tmp/codeql-scratch-8ad8dc5536a632e8/dbs/csharp/working/emptyFakeDotnetRoot /p:AllowMissingPrunePackageData=true -s REDACTED -s REDACTED (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI linked an issue May 20, 2026 that may be closed by this pull request
Copilot AI and others added 2 commits May 20, 2026 13:57
Copilot AI changed the title [WIP] Fix false positive for redundant else check Fix MA0071 false positive for else if chains with reachable prior branches May 20, 2026
Copilot AI requested a review from meziantou May 20, 2026 14:04
@meziantou meziantou marked this pull request as ready for review May 20, 2026 14:16
@meziantou meziantou enabled auto-merge (squash) May 20, 2026 14:17
@meziantou meziantou merged commit ea502f8 into main May 20, 2026
13 checks passed
@meziantou meziantou deleted the copilot/fix-redundant-else-check branch May 20, 2026 14:19
IhateTrains pushed a commit to ParadoxGameConverters/ImperatorToCK3 that referenced this pull request May 21, 2026
Updated
[Meziantou.Analyzer](https://github.com/meziantou/Meziantou.Analyzer)
from 3.0.89 to 3.0.90.

<details>
<summary>Release notes</summary>

_Sourced from [Meziantou.Analyzer's
releases](https://github.com/meziantou/Meziantou.Analyzer/releases)._

## 3.0.90

NuGet package:
<https://www.nuget.org/packages/Meziantou.Analyzer/3.0.90>

## What's Changed
* Fix MA0071 false positive for `else if` chains with reachable prior
branches by @​Copilot in
meziantou/Meziantou.Analyzer#1166


**Full Changelog**:
meziantou/Meziantou.Analyzer@3.0.89...3.0.90

Commits viewable in [compare
view](meziantou/Meziantou.Analyzer@3.0.89...3.0.90).
</details>

[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=Meziantou.Analyzer&package-manager=nuget&previous-version=3.0.89&new-version=3.0.90)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop
Dependabot creating any more for this major version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop
Dependabot creating any more for this minor version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop
Dependabot creating any more for this dependency (unless you reopen the
PR or upgrade to it yourself)


</details>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
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.

False positive avoid using redundant else

2 participants