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
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 · ◷
Problem
MamJsonParser.ReadClassRewritesuses the!null-forgiving operator on a value that is already proven non-null by an explicitif (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 methodReadGlobalMethodCallsperforms the exact same call without the operator, confirming it is unnecessary.Location
src/Xamarin.Android.Build.Tasks/Utilities/MamJsonParser.csReadClassRewrites)Current Code
For comparison,
ReadGlobalMethodCalls(~line 183) makes the identical call with no!:Suggested Fix
Remove the
!operator. After theif (classRewrite == null) continue;guard, the C# compiler already narrowsclassRewriteto non-null, so the operator is redundant:Guidelines
!null-forgiving operator in C# code.Xamarin.Android.Build.Tasks.csproj,netstandard2.0) builds with<Nullable>enable</Nullable>and<WarningsAsErrors>Nullable</WarningsAsErrors>. The identical operator-free call inReadGlobalMethodCallsalready compiles cleanly, so this change will not introduce a nullable warning. The file is also compiled standalone bytools/remap-mam-json-to-xml.JsonNode.Parse ("{}")!on ~line 63 inReadJsonis a genuine (non-redundant) non-null assertion guarding a nullable return value; do not modify it as part of this issue.Acceptance Criteria
!operator is removed from theTryReadClassFromTo (classRewrite!, ...)call inReadClassRewrites(becomesclassRewrite).Fix-finder metadata
02-null-forgiving-operator29/30(actionability: 10, safety: 9, scope: 10)