Skip to content

[fix-finder] Enable nullable reference types in LayoutAttribute.Partial.cs #12325

Description

@github-actions

Problem

LayoutAttribute.Partial.cs explicitly disables nullable reference type analysis even though its owning project enables nullable warnings. The factory legitimately returns null when an activity has no LayoutAttribute, so opting the file in also requires annotating that return path and the SingleOrDefault() result.

Location

  • File(s): src/Xamarin.Android.Build.Tasks/Mono.Android/LayoutAttribute.Partial.cs
  • Line(s): 1, 25-30

Current Code

#nullable disable

public static LayoutAttribute FromTypeDefinition (TypeDefinition type, TypeDefinitionCache cache)
{
	CustomAttribute attr = type.GetCustomAttributes ("Android.App.LayoutAttribute")
		.SingleOrDefault ();
	if (attr == null)
		return null;

Suggested Fix

Enable nullable analysis and accurately annotate the two values that can be null:

#nullable enable

public static LayoutAttribute? FromTypeDefinition (TypeDefinition type, TypeDefinitionCache cache)
{
	CustomAttribute? attr = type.GetCustomAttributes ("Android.App.LayoutAttribute")
		.SingleOrDefault ();
	if (attr == null)
		return null;

No runtime null-check API is needed. The owning Xamarin.Android.Build.Tasks.csproj targets netstandard2.0, has <Nullable>enable</Nullable>, and does not set <LangVersion> locally, so this suggestion uses only nullable annotations supported by the existing project configuration and remains compatible with its lowest TFM. Preserve the existing null check rather than using ArgumentNullException.ThrowIfNull, which is unavailable on netstandard2.0.

Guidelines

  • Keep #nullable enable as the first line with no preceding blank line.
  • Do not use the null-forgiving (!) operator.
  • Preserve Mono C# formatting (tabs and a space before ().

Acceptance Criteria

  • LayoutAttribute.Partial.cs starts with #nullable enable.
  • FromTypeDefinition returns LayoutAttribute? and its attr local is CustomAttribute?.
  • The owning Xamarin.Android.Build.Tasks.csproj builds without nullable diagnostics from this file.
  • 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 · gpt56 · 62.6 AIC · ⌖ 13.3 AIC · ⊞ 28.2K ·

  • expires on Aug 15, 2026, 2:21 AM UTC

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions