未验证 提交 58c7d75f 编写于 作者: R Rikki Gibson 提交者: GitHub

Add diagnostic for nullable annotation used in generated code (#37967)

上级 e713e946
......@@ -14904,6 +14904,24 @@ internal class CSharpResources {
}
}
/// <summary>
/// Looks up a localized string similar to The annotation for nullable reference types should only be used in code within a &apos;#nullable&apos; annotations context. Auto-generated code requires an explicit &apos;#nullable&apos; directive in source..
/// </summary>
internal static string WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode {
get {
return ResourceManager.GetString("WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to The annotation for nullable reference types should only be used in code within a &apos;#nullable&apos; annotations context. Auto-generated code requires an explicit &apos;#nullable&apos; directive in source..
/// </summary>
internal static string WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode_Title {
get {
return ResourceManager.GetString("WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode_Title", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Parameter &apos;{0}&apos; has no matching param tag in the XML comment for &apos;{1}&apos; (but other parameters do).
/// </summary>
......
......@@ -5747,6 +5747,12 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="ERR_NullableDirectiveTargetExpected" xml:space="preserve">
<value>Expected 'warnings', 'annotations', or end of directive</value>
</data>
<data name="WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode" xml:space="preserve">
<value>The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</value>
</data>
<data name="WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode_Title" xml:space="preserve">
<value>The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</value>
</data>
<data name="ERR_ExpressionTreeCantContainRefStruct" xml:space="preserve">
<value>Expression tree cannot contain value of ref struct or restricted type '{0}'.</value>
</data>
......
......@@ -1704,6 +1704,7 @@ internal enum ErrorCode
WRN_NullabilityMismatchInConstraintsOnPartialImplementation = 8667,
ERR_NullableDirectiveTargetExpected = 8668,
WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode = 8669,
ERR_MultipleAnalyzerConfigsInSameDir = 8700,
......
......@@ -410,6 +410,7 @@ internal static int GetWarningLevel(ErrorCode code)
case ErrorCode.WRN_NullableValueTypeMayBeNull:
case ErrorCode.WRN_NullabilityMismatchInTypeParameterConstraint:
case ErrorCode.WRN_MissingNonNullTypesContextForAnnotation:
case ErrorCode.WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode:
case ErrorCode.WRN_NullabilityMismatchInConstraintsOnImplicitImplementation:
case ErrorCode.WRN_NullabilityMismatchInTypeParameterReferenceTypeConstraint:
case ErrorCode.WRN_SwitchExpressionNotExhaustive:
......
......@@ -7,7 +7,7 @@
namespace Microsoft.CodeAnalysis.CSharp
{
/// <summary>
/// A lazily calculated diagnostic for missing [NonNullTypes(true)].
/// A lazily calculated diagnostic for use of nullable annotations outside of a '#nullable' annotations context.
/// </summary>
internal sealed class LazyMissingNonNullTypesContextDiagnosticInfo : LazyDiagnosticInfo
{
......@@ -24,7 +24,7 @@ private LazyMissingNonNullTypesContextDiagnosticInfo(TypeWithAnnotations type, D
public static void AddAll(bool isNullableEnabled, TypeWithAnnotations type, Location location, DiagnosticBag diagnostics)
{
var rawInfos = ArrayBuilder<DiagnosticInfo>.GetInstance();
GetRawDiagnosticInfos(isNullableEnabled, (CSharpParseOptions)location.SourceTree.Options, rawInfos);
GetRawDiagnosticInfos(isNullableEnabled, (CSharpSyntaxTree)location.SourceTree, rawInfos);
foreach (var rawInfo in rawInfos)
{
diagnostics.Add(new LazyMissingNonNullTypesContextDiagnosticInfo(type, rawInfo), location);
......@@ -32,10 +32,10 @@ public static void AddAll(bool isNullableEnabled, TypeWithAnnotations type, Loca
rawInfos.Free();
}
private static void GetRawDiagnosticInfos(bool isNullableEnabled, CSharpParseOptions options, ArrayBuilder<DiagnosticInfo> infos)
private static void GetRawDiagnosticInfos(bool isNullableEnabled, CSharpSyntaxTree tree, ArrayBuilder<DiagnosticInfo> infos)
{
const MessageID featureId = MessageID.IDS_FeatureNullableReferenceTypes;
var info = featureId.GetFeatureAvailabilityDiagnosticInfoOpt(options);
var info = featureId.GetFeatureAvailabilityDiagnosticInfoOpt(tree.Options);
if (info is object)
{
infos.Add(info);
......@@ -43,7 +43,8 @@ private static void GetRawDiagnosticInfos(bool isNullableEnabled, CSharpParseOpt
if (!isNullableEnabled && info?.Severity != DiagnosticSeverity.Error)
{
infos.Add(new CSDiagnosticInfo(ErrorCode.WRN_MissingNonNullTypesContextForAnnotation));
var code = tree.IsGeneratedCode() ? ErrorCode.WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode : ErrorCode.WRN_MissingNonNullTypesContextForAnnotation;
infos.Add(new CSDiagnosticInfo(code));
}
}
......@@ -68,7 +69,7 @@ public static void ReportNullableReferenceTypesIfNeeded(bool isNullableEnabled,
public static void ReportNullableReferenceTypesIfNeeded(bool isNullableEnabled, Location location, DiagnosticBag diagnostics)
{
var rawInfos = ArrayBuilder<DiagnosticInfo>.GetInstance();
GetRawDiagnosticInfos(isNullableEnabled, (CSharpParseOptions)location.SourceTree.Options, rawInfos);
GetRawDiagnosticInfos(isNullableEnabled, (CSharpSyntaxTree)location.SourceTree, rawInfos);
foreach (var rawInfo in rawInfos)
{
diagnostics.Add(rawInfo, location);
......
......@@ -228,6 +228,7 @@ public static bool IsWarning(ErrorCode code)
case ErrorCode.WRN_SwitchExpressionNotExhaustiveForNull:
case ErrorCode.WRN_ImplicitCopyInReadOnlyMember:
case ErrorCode.WRN_NullabilityMismatchInConstraintsOnPartialImplementation:
case ErrorCode.WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode:
case ErrorCode.WRN_NullabilityMismatchInTypeParameterNotNullConstraint:
return true;
default:
......
......@@ -1414,6 +1414,16 @@
<target state="needs-review-translation">Poznámka u typů odkazů s možnou hodnotou null by se měla v kódu používat jen v souvislosti s typem #nullable.</target>
<note />
</trans-unit>
<trans-unit id="WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode">
<source>The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</source>
<target state="new">The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</target>
<note />
</trans-unit>
<trans-unit id="WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode_Title">
<source>The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</source>
<target state="new">The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</target>
<note />
</trans-unit>
<trans-unit id="WRN_MissingNonNullTypesContextForAnnotation_Title">
<source>The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.</source>
<target state="needs-review-translation">Poznámka u typů odkazů s možnou hodnotou null by se měla v kódu používat jen v souvislosti s typem #nullable.</target>
......
......@@ -1414,6 +1414,16 @@
<target state="needs-review-translation">Die Anmerkung für Nullable-Verweistypen darf nur in Code innerhalb eines #nullable-Kontexts verwendet werden.</target>
<note />
</trans-unit>
<trans-unit id="WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode">
<source>The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</source>
<target state="new">The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</target>
<note />
</trans-unit>
<trans-unit id="WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode_Title">
<source>The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</source>
<target state="new">The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</target>
<note />
</trans-unit>
<trans-unit id="WRN_MissingNonNullTypesContextForAnnotation_Title">
<source>The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.</source>
<target state="needs-review-translation">Die Anmerkung für Nullable-Verweistypen darf nur in Code innerhalb eines #nullable-Kontexts verwendet werden.</target>
......
......@@ -1416,6 +1416,16 @@
<target state="needs-review-translation">La anotación para tipos de referencia que aceptan valores NULL solo debe usarse en el código dentro de un contexto "#nullable"</target>
<note />
</trans-unit>
<trans-unit id="WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode">
<source>The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</source>
<target state="new">The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</target>
<note />
</trans-unit>
<trans-unit id="WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode_Title">
<source>The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</source>
<target state="new">The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</target>
<note />
</trans-unit>
<trans-unit id="WRN_MissingNonNullTypesContextForAnnotation_Title">
<source>The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.</source>
<target state="needs-review-translation">La anotación para tipos de referencia que aceptan valores NULL solo debe usarse en el código dentro de un contexto "#nullable"</target>
......
......@@ -1415,6 +1415,16 @@
<target state="needs-review-translation">L'annotation pour les types référence Nullable doit être utilisée uniquement dans le code au sein d'un contexte '#nullable'.</target>
<note />
</trans-unit>
<trans-unit id="WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode">
<source>The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</source>
<target state="new">The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</target>
<note />
</trans-unit>
<trans-unit id="WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode_Title">
<source>The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</source>
<target state="new">The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</target>
<note />
</trans-unit>
<trans-unit id="WRN_MissingNonNullTypesContextForAnnotation_Title">
<source>The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.</source>
<target state="needs-review-translation">L'annotation pour les types référence Nullable doit être utilisée uniquement dans le code au sein d'un contexte '#nullable'.</target>
......
......@@ -1414,6 +1414,16 @@
<target state="needs-review-translation">L'annotazione per i tipi riferimento nullable deve essere usata solo nel codice in un contesto '#nullable'.</target>
<note />
</trans-unit>
<trans-unit id="WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode">
<source>The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</source>
<target state="new">The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</target>
<note />
</trans-unit>
<trans-unit id="WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode_Title">
<source>The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</source>
<target state="new">The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</target>
<note />
</trans-unit>
<trans-unit id="WRN_MissingNonNullTypesContextForAnnotation_Title">
<source>The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.</source>
<target state="needs-review-translation">L'annotazione per i tipi riferimento nullable deve essere usata solo nel codice in un contesto '#nullable'.</target>
......
......@@ -1414,6 +1414,16 @@
<target state="needs-review-translation">'#nullable' コンテキスト内のコードでのみ、Null 許容参照型の注釈を使用する必要があります。</target>
<note />
</trans-unit>
<trans-unit id="WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode">
<source>The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</source>
<target state="new">The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</target>
<note />
</trans-unit>
<trans-unit id="WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode_Title">
<source>The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</source>
<target state="new">The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</target>
<note />
</trans-unit>
<trans-unit id="WRN_MissingNonNullTypesContextForAnnotation_Title">
<source>The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.</source>
<target state="needs-review-translation">'#nullable' コンテキスト内のコードでのみ、Null 許容参照型の注釈を使用する必要があります。</target>
......
......@@ -1414,6 +1414,16 @@
<target state="needs-review-translation">nullable 참조 형식에 대한 주석은 코드에서 '#nullable' 컨텍스트 내에만 사용되어야 합니다.</target>
<note />
</trans-unit>
<trans-unit id="WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode">
<source>The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</source>
<target state="new">The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</target>
<note />
</trans-unit>
<trans-unit id="WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode_Title">
<source>The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</source>
<target state="new">The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</target>
<note />
</trans-unit>
<trans-unit id="WRN_MissingNonNullTypesContextForAnnotation_Title">
<source>The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.</source>
<target state="needs-review-translation">nullable 참조 형식에 대한 주석은 코드에서 '#nullable' 컨텍스트 내에만 사용되어야 합니다.</target>
......
......@@ -1414,6 +1414,16 @@
<target state="needs-review-translation">Adnotacja dla typów referencyjnych dopuszczających wartość null powinna być używana tylko w kodzie z kontekstem „#nullable”.</target>
<note />
</trans-unit>
<trans-unit id="WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode">
<source>The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</source>
<target state="new">The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</target>
<note />
</trans-unit>
<trans-unit id="WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode_Title">
<source>The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</source>
<target state="new">The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</target>
<note />
</trans-unit>
<trans-unit id="WRN_MissingNonNullTypesContextForAnnotation_Title">
<source>The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.</source>
<target state="needs-review-translation">Adnotacja dla typów referencyjnych dopuszczających wartość null powinna być używana tylko w kodzie z kontekstem „#nullable”.</target>
......
......@@ -1414,6 +1414,16 @@
<target state="needs-review-translation">A anotação para tipos de referência que permitem valor nulo deve ser usada apenas em código dentro de um contexto '#nullable'.</target>
<note />
</trans-unit>
<trans-unit id="WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode">
<source>The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</source>
<target state="new">The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</target>
<note />
</trans-unit>
<trans-unit id="WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode_Title">
<source>The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</source>
<target state="new">The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</target>
<note />
</trans-unit>
<trans-unit id="WRN_MissingNonNullTypesContextForAnnotation_Title">
<source>The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.</source>
<target state="needs-review-translation">A anotação para tipos de referência que permitem valor nulo deve ser usada apenas em código dentro de um contexto '#nullable'.</target>
......
......@@ -1414,6 +1414,16 @@
<target state="needs-review-translation">Аннотацию для ссылочных типов, допускающих значение NULL, следует использовать в коде только в контексте "#nullable".</target>
<note />
</trans-unit>
<trans-unit id="WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode">
<source>The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</source>
<target state="new">The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</target>
<note />
</trans-unit>
<trans-unit id="WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode_Title">
<source>The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</source>
<target state="new">The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</target>
<note />
</trans-unit>
<trans-unit id="WRN_MissingNonNullTypesContextForAnnotation_Title">
<source>The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.</source>
<target state="needs-review-translation">Аннотацию для ссылочных типов, допускающих значение NULL, следует использовать в коде только в контексте "#nullable".</target>
......
......@@ -1415,6 +1415,16 @@
<target state="needs-review-translation">Boş değer atanabilir başvuru türleri için ek açıklama kodda yalnızca bir '#nullable' bağlamı içinde kullanılmalıdır.</target>
<note />
</trans-unit>
<trans-unit id="WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode">
<source>The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</source>
<target state="new">The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</target>
<note />
</trans-unit>
<trans-unit id="WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode_Title">
<source>The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</source>
<target state="new">The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</target>
<note />
</trans-unit>
<trans-unit id="WRN_MissingNonNullTypesContextForAnnotation_Title">
<source>The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.</source>
<target state="needs-review-translation">Boş değer atanabilir başvuru türleri için ek açıklama kodda yalnızca bir '#nullable' bağlamı içinde kullanılmalıdır.</target>
......
......@@ -1450,6 +1450,16 @@
<target state="needs-review-translation">应仅在 “#nullable” 上下文中的代码中使用可为 null 的引用类型的注释。</target>
<note />
</trans-unit>
<trans-unit id="WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode">
<source>The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</source>
<target state="new">The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</target>
<note />
</trans-unit>
<trans-unit id="WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode_Title">
<source>The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</source>
<target state="new">The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</target>
<note />
</trans-unit>
<trans-unit id="WRN_MissingNonNullTypesContextForAnnotation_Title">
<source>The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.</source>
<target state="needs-review-translation">应仅在 “#nullable” 上下文中的代码中使用可为 null 的引用类型的注释。</target>
......
......@@ -1414,6 +1414,16 @@
<target state="needs-review-translation">可為 Null 的參考型別註解應只用於 '#nullable' 內容中的程式碼。</target>
<note />
</trans-unit>
<trans-unit id="WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode">
<source>The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</source>
<target state="new">The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</target>
<note />
</trans-unit>
<trans-unit id="WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode_Title">
<source>The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</source>
<target state="new">The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.</target>
<note />
</trans-unit>
<trans-unit id="WRN_MissingNonNullTypesContextForAnnotation_Title">
<source>The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.</source>
<target state="needs-review-translation">可為 Null 的參考型別註解應只用於 '#nullable' 內容中的程式碼。</target>
......
......@@ -5271,6 +5271,48 @@ static void F(object x)
comp.VerifyDiagnostics();
}
[Theory]
[InlineData("")]
[InlineData("#nullable enable warnings")]
[InlineData("#nullable disable")]
public void Directive_GloballyEnabled_GeneratedCode_Annotations_Disabled(string nullableDirective)
{
var source =
$@"
// <autogenerated />
{nullableDirective}
class Program
{{
static void F(string? s)
{{
}}
}}";
var comp = CreateCompilation(source, options: TestOptions.DebugDll.WithNullableContextOptions(NullableContextOptions.Enable));
comp.VerifyDiagnostics(
// (6,25): error CS8669: The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable enable' directive in source.
// static void F(string? s)
Diagnostic(ErrorCode.WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode, "?").WithLocation(6, 25));
}
[Theory]
[InlineData("#nullable enable")]
[InlineData("#nullable enable annotations")]
public void Directive_GloballyEnabled_GeneratedCode_Annotations_Enabled(string nullableDirective)
{
var source =
$@"
// <autogenerated />
{nullableDirective}
class Program
{{
static void F(string? s)
{{
}}
}}";
var comp = CreateCompilation(source, options: TestOptions.DebugDll.WithNullableContextOptions(NullableContextOptions.Enable));
comp.VerifyDiagnostics();
}
[Fact]
public void Directive_GloballyEnabled_GeneratedCode_Enabled()
{
......@@ -295,6 +295,7 @@ public void WarningLevel_2()
case ErrorCode.WRN_NullableValueTypeMayBeNull:
case ErrorCode.WRN_NullabilityMismatchInTypeParameterConstraint:
case ErrorCode.WRN_MissingNonNullTypesContextForAnnotation:
case ErrorCode.WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode:
case ErrorCode.WRN_NullabilityMismatchInConstraintsOnImplicitImplementation:
case ErrorCode.WRN_NullabilityMismatchInTypeParameterReferenceTypeConstraint:
case ErrorCode.WRN_CaseConstantNamedUnderscore:
......@@ -348,6 +349,7 @@ public void NullableWarnings()
var nullableUnrelatedWarnings = new[]
{
ErrorCode.WRN_MissingNonNullTypesContextForAnnotation,
ErrorCode.WRN_MissingNonNullTypesContextForAnnotationInGeneratedCode,
ErrorCode.WRN_ImplicitCopyInReadOnlyMember,
};
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册