-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Relax enforcement on conditional nullability attributes #46201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22407,18 +22407,15 @@ public class C | |
| bool Init() | ||
| { | ||
| bool b = true; | ||
| return b; // 2 | ||
| return b; | ||
| } | ||
| } | ||
| ", MemberNotNullWhenAttributeDefinition }, parseOptions: TestOptions.RegularPreview); | ||
|
|
||
| c.VerifyDiagnostics( | ||
| // (5,19): warning CS8618: Non-nullable field 'field1' is uninitialized. Consider declaring the field as nullable. | ||
| // public string field1; // 1 | ||
| Diagnostic(ErrorCode.WRN_UninitializedNonNullableField, "field1").WithArguments("field", "field1").WithLocation(5, 19), | ||
| // (11,9): warning CS8775: Member 'field1' must have a non-null value when exiting with 'true'. | ||
| // return b; // 2 | ||
| Diagnostic(ErrorCode.WRN_MemberNotNullWhen, "return b;").WithArguments("field1", "true").WithLocation(11, 9) | ||
| Diagnostic(ErrorCode.WRN_UninitializedNonNullableField, "field1").WithArguments("field", "field1").WithLocation(5, 19) | ||
| ); | ||
| } | ||
|
|
||
|
|
@@ -23820,7 +23817,7 @@ bool Init(bool b) | |
| ); | ||
| } | ||
|
|
||
| [Fact] | ||
| [Fact, WorkItem(44080, "https://github.com/dotnet/roslyn/issues/44080")] | ||
| public void MemberNotNullWhenTrue_EnforcedInMethodBody_NonConstantBool() | ||
| { | ||
| var c = CreateNullableCompilation(new[] { @" | ||
|
|
@@ -23844,13 +23841,7 @@ bool Init() | |
| c.VerifyDiagnostics( | ||
| // (5,19): warning CS8618: Non-nullable field 'field1' is uninitialized. Consider declaring the field as nullable. | ||
| // public string field1; | ||
| Diagnostic(ErrorCode.WRN_UninitializedNonNullableField, "field1").WithArguments("field", "field1").WithLocation(5, 19), | ||
| // (13,9): warning CS8775: Member 'field1' must have a non-null value when exiting with 'true'. | ||
| // return NonConstantBool(); | ||
| Diagnostic(ErrorCode.WRN_MemberNotNullWhen, "return NonConstantBool();").WithArguments("field1", "true").WithLocation(13, 9), | ||
| // (13,9): warning CS8775: Member 'field3' must have a non-null value when exiting with 'true'. | ||
| // return NonConstantBool(); | ||
| Diagnostic(ErrorCode.WRN_MemberNotNullWhen, "return NonConstantBool();").WithArguments("field3", "true").WithLocation(13, 9) | ||
| Diagnostic(ErrorCode.WRN_UninitializedNonNullableField, "field1").WithArguments("field", "field1").WithLocation(5, 19) | ||
| ); | ||
| } | ||
|
|
||
|
|
@@ -24635,11 +24626,7 @@ public static bool TryGetValue<T>([MaybeNull] [NotNullWhen(true)] out T t) | |
| } | ||
| "; | ||
| var comp = CreateNullableCompilation(new[] { source, MaybeNullAttributeDefinition, NotNullWhenAttributeDefinition }); | ||
| comp.VerifyDiagnostics( | ||
| // (7,9): warning CS8762: Parameter 't' must have a non-null value when exiting with 'true'. | ||
| // return TryGetValue2<T>(out t); | ||
| Diagnostic(ErrorCode.WRN_ParameterConditionallyDisallowsNull, "return TryGetValue2<T>(out t);").WithArguments("t", "true").WithLocation(7, 9) | ||
| ); | ||
| comp.VerifyDiagnostics(); | ||
| } | ||
|
|
||
| [Fact, WorkItem(39922, "https://github.com/dotnet/roslyn/issues/39922")] | ||
|
|
@@ -24848,20 +24835,16 @@ public class C | |
| public static bool TryGetValue([NotNullWhen(true)] out string? s) | ||
| { | ||
| s = null; | ||
| return NonConstantBool(); // 1 | ||
| return NonConstantBool(); | ||
| } | ||
| static bool NonConstantBool() => throw null!; | ||
| } | ||
| "; | ||
| var comp = CreateNullableCompilation(new[] { source, NotNullWhenAttributeDefinition }); | ||
| comp.VerifyDiagnostics( | ||
| // (8,9): warning CS8762: Parameter 's' must have a non-null value when exiting with 'true'. | ||
| // return NonConstantBool(); // 1 | ||
| Diagnostic(ErrorCode.WRN_ParameterConditionallyDisallowsNull, "return NonConstantBool();").WithArguments("s", "true").WithLocation(8, 9) | ||
| ); | ||
| comp.VerifyDiagnostics(); | ||
| } | ||
|
|
||
| [Fact, WorkItem(39922, "https://github.com/dotnet/roslyn/issues/39922")] | ||
| [Fact, WorkItem(39922, "https://github.com/dotnet/roslyn/issues/39922"), WorkItem(44080, "https://github.com/dotnet/roslyn/issues/44080")] | ||
| public void NotNullWhenFalse_EnforceInMethodBody_Warn_NonConstantReturn() | ||
| { | ||
| var source = @" | ||
|
|
@@ -24871,17 +24854,13 @@ public class C | |
| public static bool TryGetValue([NotNullWhen(false)] out string? s) | ||
| { | ||
| s = null; | ||
| return NonConstantBool(); // 1 | ||
| return NonConstantBool(); | ||
| } | ||
| static bool NonConstantBool() => throw null!; | ||
| } | ||
| "; | ||
| var comp = CreateNullableCompilation(new[] { source, NotNullWhenAttributeDefinition }); | ||
| comp.VerifyDiagnostics( | ||
| // (8,9): warning CS8762: Parameter 's' must have a non-null value when exiting with 'false'. | ||
| // return NonConstantBool(); // 1 | ||
| Diagnostic(ErrorCode.WRN_ParameterConditionallyDisallowsNull, "return NonConstantBool();").WithArguments("s", "false").WithLocation(8, 9) | ||
| ); | ||
| comp.VerifyDiagnostics(); | ||
| } | ||
|
|
||
| [Fact, WorkItem(39922, "https://github.com/dotnet/roslyn/issues/39922"), WorkItem(42386, "https://github.com/dotnet/roslyn/issues/42386")] | ||
|
|
@@ -25041,7 +25020,7 @@ public static bool TryGetValue(C? c, [NotNullWhen(true)] out string? s) | |
| return c; // 1 | ||
| } | ||
|
|
||
| public static bool TryGetValue2(C c, [NotNullWhen(true)] out string? s) | ||
| public static bool TryGetValue2(C c, [NotNullWhen(false)] out string? s) | ||
| { | ||
| s = null; | ||
| return c; // 2 | ||
|
|
@@ -25063,20 +25042,7 @@ static bool TryGetValue4([MaybeNullWhen(false)]out string s) | |
| } | ||
| "; | ||
| var comp = CreateNullableCompilation(new[] { source, NotNullWhenAttributeDefinition, MaybeNullWhenAttributeDefinition }); | ||
| comp.VerifyDiagnostics( | ||
| // (8,9): warning CS8762: Parameter 's' must have a non-null value when exiting with 'true'. | ||
| // return c; // 1 | ||
| Diagnostic(ErrorCode.WRN_ParameterConditionallyDisallowsNull, "return c;").WithArguments("s", "true").WithLocation(8, 9), | ||
| // (14,9): warning CS8762: Parameter 's' must have a non-null value when exiting with 'true'. | ||
| // return c; // 2 | ||
| Diagnostic(ErrorCode.WRN_ParameterConditionallyDisallowsNull, "return c;").WithArguments("s", "true").WithLocation(14, 9), | ||
| // (22,9): warning CS8762: Parameter 's' must have a non-null value when exiting with 'true'. | ||
| // return (bool)true; // 3 | ||
| Diagnostic(ErrorCode.WRN_ParameterConditionallyDisallowsNull, "return (bool)true;").WithArguments("s", "true").WithLocation(22, 9), | ||
| // (28,9): warning CS8762: Parameter 's' must have a non-null value when exiting with 'true'. | ||
| // return (bool)false; // 4 | ||
| Diagnostic(ErrorCode.WRN_ParameterConditionallyDisallowsNull, "return (bool)false;").WithArguments("s", "true").WithLocation(28, 9) | ||
| ); | ||
| comp.VerifyDiagnostics(); | ||
| } | ||
|
|
||
| [Fact] | ||
|
|
@@ -128481,6 +128447,42 @@ class B : A | |
| ); | ||
| } | ||
|
|
||
| [Fact, WorkItem(43071, "https://github.com/dotnet/roslyn/issues/43071")] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does the implementation change affect behavior of this test?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No behavior change to this test, I'm just adding a test to close a different issue. |
||
| public void LocalFunctionInLambdaWithReturnStatement() | ||
| { | ||
| var source = @" | ||
| using System; | ||
| using System.Collections.Generic; | ||
|
|
||
| public class C<T> | ||
| { | ||
| public static C<string> ReproFunction(C<string> collection) | ||
| { | ||
| return collection | ||
| .SelectMany(allStrings => | ||
| { | ||
| return new[] { getSomeString(""custard"") }; | ||
|
|
||
| string getSomeString(string substring) | ||
| { | ||
| return substring; | ||
| } | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| public static class Extension | ||
| { | ||
| public static C<TResult> SelectMany<TSource, TResult>(this C<TSource> source, Func<TSource, IEnumerable<TResult>> selector) | ||
| { | ||
| throw null!; | ||
| } | ||
| } | ||
| "; | ||
| var comp = CreateNullableCompilation(source); | ||
| comp.VerifyDiagnostics(); | ||
| } | ||
|
|
||
| [Fact] | ||
| [WorkItem(44348, "https://github.com/dotnet/roslyn/issues/44348")] | ||
| public void Constraints() | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this works because we enter a split state after visiting a
boolconstant, right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Discussed offline.