Skip to content

[fix-finder] Remove redundant null-forgiving operator in MamJsonParser.ReadClassRewrites #11771

Description

@github-actions

Problem

MamJsonParser.ReadClassRewrites uses the ! null-forgiving operator on a value that is already proven non-null by an explicit if (classRewrite == null) continue; guard immediately above it. The dotnet/android coding rules state the ! null-forgiving operator must never be used in C# code; here it is also redundant. The sibling method ReadGlobalMethodCalls performs the exact same call without the operator, confirming it is unnecessary.

Location

  • File(s): src/Xamarin.Android.Build.Tasks/Utilities/MamJsonParser.cs
  • Line(s): ~73 (inside ReadClassRewrites)

Current Code

foreach (var classRewrite in classRewrites.AsArray ()) {
    if (classRewrite == null) {
        continue;
    }
    if (!TryReadClassFromTo (classRewrite!, out var from, out var to)) {
        Logger (TraceLevel.Verbose, $"No from or to! {classRewrite}");
        continue;
    }

For comparison, ReadGlobalMethodCalls (~line 183) makes the identical call with no !:

if (!TryReadClassFromTo (globalMethodCall, out var from, out var to)) {

Suggested Fix

Remove the ! operator. After the if (classRewrite == null) continue; guard, the C# compiler already narrows classRewrite to non-null, so the operator is redundant:

    if (!TryReadClassFromTo (classRewrite, out var from, out var to)) {

Guidelines

  • Repo rule: NEVER use the ! null-forgiving operator in C# code.
  • The owning project (Xamarin.Android.Build.Tasks.csproj, netstandard2.0) builds with <Nullable>enable</Nullable> and <WarningsAsErrors>Nullable</WarningsAsErrors>. The identical operator-free call in ReadGlobalMethodCalls already compiles cleanly, so this change will not introduce a nullable warning. The file is also compiled standalone by tools/remap-mam-json-to-xml.
  • C# tabs, Mono style; keep the diff minimal.
  • Out of scope: the separate JsonNode.Parse ("{}")! on ~line 63 in ReadJson is a genuine (non-redundant) non-null assertion guarding a nullable return value; do not modify it as part of this issue.

Acceptance Criteria

  • The ! operator is removed from the TryReadClassFromTo (classRewrite!, ...) call in ReadClassRewrites (becomes classRewrite).
  • No other lines are changed.
  • All tests pass.
  • No new warnings introduced.

Fix-finder metadata

  • Script: 02-null-forgiving-operator
  • Score: 29/30 (actionability: 10, safety: 9, scope: 10)

Generated by Nightly Fix Finder for issue #11769 · 384 AIC · ⌖ 48.1 AIC · ⊞ 40.8K ·

  • expires on Jul 4, 2026, 3:38 PM UTC

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions