Problem
src/Xamarin.Android.Tools.Aidl/AidlCompiler.cs does not have nullable reference types enabled. The file has several fields and parameters that can be null but are typed as non-nullable, which hides potential null reference issues at compile time.
Location
- File:
src/Xamarin.Android.Tools.Aidl/AidlCompiler.cs
Current Code
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Irony.Parsing;
using Mono.Cecil;
namespace Xamarin.Android.Tools.Aidl
{
public class AidlCompiler
{
// ...
Dictionary<string,CompilationUnit> units = new Dictionary<string,CompilationUnit> ();
string output_path; // can be null before Run() is called
string output_ns; // can be null, checked on line 71
public event Action<string,string> FileWritten; // can be null if no subscriber
// ...
// Line 75: FileWritten invoked without null check
FileWritten (file, csharp);
// Line 82: out parameter set to null without nullable annotation
public Result Run (string input, out string output, AssemblyDefinition[] references = null, ...)
{
// ...
output = null; // assigned null but parameter is non-nullable
}
}
}
Suggested Fix
- Add
#nullable enable at the top of the file (no preceding blank lines)
- Make nullable fields explicit:
string output_path; → string? output_path;
string output_ns; → string? output_ns;
- Make the event nullable and use null-conditional invoke:
public event Action<string,string> FileWritten; → public event Action<string,string>? FileWritten;
FileWritten (file, csharp); → FileWritten?.Invoke (file, csharp);
- Fix method signatures:
out string output → out string? output
AssemblyDefinition[] references = null → AssemblyDefinition[]? references = null
- Do NOT use the
! (null-forgiving) operator anywhere — always handle nulls explicitly
Guidelines
- This project targets
netstandard2.0, so use throw new ArgumentNullException (nameof(parameter)) for null argument checks (not ArgumentNullException.ThrowIfNull)
- Follow Mono formatting style: space before
( in method calls (e.g., FileWritten?.Invoke (file, csharp))
- Use
"" not string.Empty
- Do not add
<Nullable>enable</Nullable> to the .csproj — only add the per-file #nullable enable directive
- Use the
IsNullOrEmpty() extension method instead of string.IsNullOrEmpty() if applicable
Acceptance Criteria
Generated by Nightly Fix Finder · ● 6.7M · ◷
Problem
src/Xamarin.Android.Tools.Aidl/AidlCompiler.csdoes not have nullable reference types enabled. The file has several fields and parameters that can be null but are typed as non-nullable, which hides potential null reference issues at compile time.Location
src/Xamarin.Android.Tools.Aidl/AidlCompiler.csCurrent Code
Suggested Fix
#nullable enableat the top of the file (no preceding blank lines)string output_path;→string? output_path;string output_ns;→string? output_ns;public event Action<string,string> FileWritten;→public event Action<string,string>? FileWritten;FileWritten (file, csharp);→FileWritten?.Invoke (file, csharp);out string output→out string? outputAssemblyDefinition[] references = null→AssemblyDefinition[]? references = null!(null-forgiving) operator anywhere — always handle nulls explicitlyGuidelines
netstandard2.0, so usethrow new ArgumentNullException (nameof(parameter))for null argument checks (notArgumentNullException.ThrowIfNull)(in method calls (e.g.,FileWritten?.Invoke (file, csharp))""notstring.Empty<Nullable>enable</Nullable>to the.csproj— only add the per-file#nullable enabledirectiveIsNullOrEmpty()extension method instead ofstring.IsNullOrEmpty()if applicableAcceptance Criteria
#nullable enableadded at the top ofAidlCompiler.cs!(null-forgiving) operatorFileWrittenevent invoked safely with null-conditional operator