Skip to content

Commit 0f10946

Browse files
authored
Fix nullable reference warnings using LocalizedString -> string implicit conversion operator (#28359)
* Added NotNullIfNotNull attribute on LocalizedString -> string operator to fix nullable reference warnings. I'm upgrading a solution to .NET 5 and seeing nullable ref warnings anywhere we're doing something like... ```csharp private string GetFriendlyName(int id) { return _Localizer["FriendlyName", id]; // Warning CS8603 Possible null reference return. } ``` Simple to fix with a `NotNullIfNotNull` decoration.
2 parents 31a01b8 + e6e9f0a commit 0f10946

File tree

5 files changed

+22
-2
lines changed

5 files changed

+22
-2
lines changed

src/Localization/Abstractions/src/LocalizedString.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Diagnostics.CodeAnalysis;
56

67
namespace Microsoft.Extensions.Localization
78
{
@@ -60,6 +61,7 @@ public LocalizedString(string name, string value, bool resourceNotFound, string?
6061
/// Implicitly converts the <see cref="LocalizedString"/> to a <see cref="string"/>.
6162
/// </summary>
6263
/// <param name="localizedString">The string to be implicitly converted.</param>
64+
[return: NotNullIfNotNull("localizedString")]
6365
public static implicit operator string?(LocalizedString localizedString)
6466
{
6567
return localizedString?.Value;

src/Localization/Localization.slnf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"solution": {
3+
"path": "..\\..\\AspNetCore.sln",
4+
"projects" : [
5+
"src\\Localization\\Abstractions\\src\\Microsoft.Extensions.Localization.Abstractions.csproj",
6+
"src\\Localization\\Localization\\src\\Microsoft.Extensions.Localization.csproj",
7+
"src\\Localization\\Localization\\test\\Microsoft.Extensions.Localization.RootNamespace.Tests\\Microsoft.Extensions.Localization.RootNamespace.Tests.csproj",
8+
"src\\Localization\\Localization\\test\\Microsoft.Extensions.Localization.Tests\\Microsoft.Extensions.Localization.Tests.csproj",
9+
]
10+
}
11+
}

src/Localization/Localization/test/Microsoft.Extensions.Localization.RootNamespace.Tests/StringLocalizerOfTRootNamespaceTest.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using LocalizationTest.Abc.Controllers;
@@ -20,7 +20,8 @@ public void RootNamespace()
2020
var factory = new ResourceManagerStringLocalizerFactory(options.Object, NullLoggerFactory.Instance);
2121

2222
var valuesLoc = factory.Create(typeof(ValuesController));
23-
Assert.Equal("ValFromResource", valuesLoc["String1"]);
23+
string value = valuesLoc["String1"]; // Note: Tests nullable analysis of implicit string conversion operator.
24+
Assert.Equal("ValFromResource", value);
2425
}
2526
}
2627
}

src/Localization/build.cmd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@ECHO OFF
2+
SET RepoRoot=%~dp0..\..
3+
%RepoRoot%\build.cmd -projects %~dp0**\*.*proj %*

src/Localization/startvs.cmd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@ECHO OFF
2+
3+
%~dp0..\..\startvs.cmd %~dp0Localization.slnf

0 commit comments

Comments
 (0)