Skip to content

[fix-finder] Enable nullable reference types in RtxtWriter.cs #12240

Description

@github-actions

Problem

src/Xamarin.Android.Build.Tasks/Utilities/RtxtWriter.cs is shipped product code that has not yet opted in to nullable reference types (NRT). Enabling #nullable enable improves null-safety and moves this file toward the repo-wide NRT goal. This is a tiny, self-contained utility, making it a low-risk candidate.

Location

  • File(s): src/Xamarin.Android.Build.Tasks/Utilities/RtxtWriter.cs
  • Line(s): entire file (25 lines)

Current Code

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Linq;
using Microsoft.Android.Build.Tasks;
using Microsoft.Build.Utilities;

namespace Xamarin.Android.Tasks
{
	/// Write a list of Item to a file
	///
	public class RtxtWriter {
		public void Write (string file, IList<R> items)
		{
			using (var sw = MemoryStreamPool.Shared.CreateStreamWriter ()) {
				foreach (var item in items) {
					sw.WriteLine (item.ToString ());
				}
				sw.Flush ();
				Files.CopyIfStreamChanged (sw.BaseStream, file);
			}
		}
	}
}

Suggested Fix

Add #nullable enable as the very first line (no preceding blank lines) and add explicit null checks for the two reference-type parameters (file and items). The owning project (Xamarin.Android.Build.Tasks.csproj) targets netstandard2.0, so ArgumentNullException.ThrowIfNull is NOT available — use the explicit two-line form:

#nullable enable
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Linq;
using Microsoft.Android.Build.Tasks;
using Microsoft.Build.Utilities;

namespace Xamarin.Android.Tasks
{
	/// Write a list of Item to a file
	///
	public class RtxtWriter {
		public void Write (string file, IList<R> items)
		{
			if (file == null)
				throw new ArgumentNullException (nameof (file));
			if (items == null)
				throw new ArgumentNullException (nameof (items));

			using (var sw = MemoryStreamPool.Shared.CreateStreamWriter ()) {
				foreach (var item in items) {
					sw.WriteLine (item.ToString ());
				}
				sw.Flush ();
				Files.CopyIfStreamChanged (sw.BaseStream, file);
			}
		}
	}
}

Note: R is a struct (defined in RtxtParser.cs), so IList<R> needs no element-level null annotations and no ! operator is required anywhere.

Guidelines

  • Add #nullable enable at the top with no preceding blank lines.
  • Never use the ! (null-forgiving) operator — check for null explicitly.
  • The project is netstandard2.0; use if (x == null) throw new ArgumentNullException (nameof (x)); — do NOT use ArgumentNullException.ThrowIfNull (it does not exist in netstandard2.0 and broke the build in PR Enable nullable reference types in XAJavaTypeScanner.cs #11455).
  • Follow Mono code style: tabs, space before (.

Acceptance Criteria

  • #nullable enable is the first line of the file
  • No ! null-forgiving operator is used
  • The file compiles with zero nullable warnings
  • All tests pass
  • No new warnings introduced

Fix-finder metadata

  • Script: 01-nullable-reference-types
  • Score: 30/30 (actionability: 10, safety: 10, scope: 10)

Generated by Nightly Fix Finder · 59.7 AIC · ⌖ 17.7 AIC · ⊞ 9.4K ·

  • expires on Aug 3, 2026, 2:27 AM UTC

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions