Skip to content

[fix-finder] Enable nullable reference types in CodeWhen.cs #11694

@github-actions

Description

@github-actions

Problem

src/Mono.Android.Export/Mono.CodeGeneration/CodeWhen.cs is shipped product code — it compiles into Mono.Android.Export.dll, which ships in the Android runtime pack — but it is not opted into nullable reference types (NRT). Its owning project, Mono.Android.Export, does not set <Nullable> at the project level, so this file compiles in a nullable-oblivious context and gets no compile-time null-safety checking.

This particular file is a clean, low-risk candidate to opt in: the class is already null-correct, so enabling NRT should require only the #nullable enable directive and no logic changes.

Location

  • File(s): src/Mono.Android.Export/Mono.CodeGeneration/CodeWhen.cs
  • Line(s): top of file — insert the directive immediately after the #if !MONOTOUCH guard (currently line 24)

Current Code

The file currently opens like this (license header omitted):

#if !MONOTOUCH
using System;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Reflection.Emit;

namespace Mono.CodeGeneration
{
	[RequiresUnreferencedCode (MonoAndroidExport.DynamicFeatures)]
	internal class CodeWhen: CodeExpression
	{
		CodeExpression condition;
		CodeExpression trueBlock;
		CodeExpression falseBlock;

		public CodeWhen (CodeExpression condition, CodeExpression trueResult, CodeExpression falseResult)
		{
			this.condition = condition;
			if (condition.GetResultType () != typeof(bool))
				throw new InvalidOperationException ("Condition expression is not boolean");
			if (trueResult.GetResultType() != falseResult.GetResultType())
				throw new InvalidOperationException ("The types of the true and false expressions must be the same");
			trueBlock = trueResult;
			falseBlock = falseResult;
		}
		// ... Generate / GenerateCondition / PrintCode / GetResultType ...
	}
}
#endif

Suggested Fix

Add #nullable enable on its own line, immediately after the #if !MONOTOUCH directive:

#if !MONOTOUCH
#nullable enable

using System;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Reflection.Emit;

No other changes are expected to be necessary, because the class is already null-correct:

  • All three reference-type fields (condition, trueBlock, falseBlock) are unconditionally assigned in the single constructor before it returns, so there is no CS8618 (uninitialized non-nullable field).
  • Every method dereferences only non-null parameters or those already-assigned fields.
  • Calls into the (still nullable-oblivious) base class CodeExpression and sibling CodeConditionExpression neither produce nor receive nullable warnings.

If — and only if — the compiler surfaces an unexpected nullable warning, fix it following the repo conventions below. The project targets $(DotNetTargetFramework) (.NET), so any genuine argument null-check should use ArgumentNullException.ThrowIfNull (x); (do not use the explicit netstandard2.0 form here). None is expected.

Guidelines

  • Follow the repo's nullable conventions documented in the AI instructions.
  • Never use the ! null-forgiving operator — check for null explicitly if a warning genuinely requires it.
  • Keep Mono code style: tabs, space before (, etc. Preserve the existing license header and the #if !MONOTOUCH / #endif guard.
  • CI builds with TreatWarningsAsErrors=true, so any new nullable warning will fail the build — verify a clean build of Mono.Android.Export before finishing.

Acceptance Criteria

  • #nullable enable added to CodeWhen.cs, immediately after the #if !MONOTOUCH guard.
  • No ! null-forgiving operator introduced.
  • No behavioral/logic changes (directive-only change unless a warning genuinely requires a fix).
  • Mono.Android.Export builds with no new nullable warnings or errors.
  • All tests pass.
  • No new warnings introduced.

Fix-finder metadata

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

Generated by Nightly Fix Finder · 1.2K AIC · ⌖ 50.9 AIC · ⊞ 40.6K ·

  • expires on Jun 25, 2026, 2:43 AM UTC

Metadata

Metadata

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions