Skip to content

Commit

Permalink
Disallow null checking discard parameters (#58952)
Browse files Browse the repository at this point in the history
  • Loading branch information
RikkiGibson committed Jan 20, 2022
1 parent d305677 commit aad378e
Show file tree
Hide file tree
Showing 20 changed files with 224 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,57 @@ class C
}.RunAsync();
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseIsNullCheck)]
public async Task TestDiscard1()
{
await new VerifyCS.Test()
{
TestCode = @"using System;
class C
{
Action<string> lambda = _ =>
{
[|if (_ is null)
throw new ArgumentNullException(nameof(_));|]
};
}
",
FixedCode = @"using System;
class C
{
Action<string> lambda = _!! =>
{
};
}
",
LanguageVersion = LanguageVersionExtensions.CSharpNext
}.RunAsync();
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseIsNullCheck)]
public async Task TestDiscard2()
{
var testCode = @"
using System;
class C
{
Action<string, string> lambda = (_, _) =>
{
if ({|CS0103:_|} is null)
throw new ArgumentNullException(nameof({|CS0103:_|}));
};
}";
await new VerifyCS.Test()
{
TestCode = testCode,
FixedCode = testCode,
LanguageVersion = LanguageVersionExtensions.CSharpNext
}.RunAsync();
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsUseIsNullCheck)]
public async Task TestAnonymousMethod()
{
Expand Down
4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -6169,8 +6169,8 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="ERR_DuplicateNullSuppression" xml:space="preserve">
<value>Duplicate null suppression operator ('!')</value>
</data>
<data name="ERR_IncorrectNullCheckSyntax" xml:space="preserve">
<value>Incorrect parameter null checking syntax. Should be '!!'.</value>
<data name="ERR_DiscardCannotBeNullChecked" xml:space="preserve">
<value>Discard parameter cannot be null-checked.</value>
</data>
<data name="ERR_MustNullCheckInImplementation" xml:space="preserve">
<value>Parameter '{0}' can only have exclamation-point null checking in implementation methods.</value>
Expand Down
2 changes: 1 addition & 1 deletion src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2020,7 +2020,7 @@ internal enum ErrorCode
ERR_StructHasInitializersAndNoDeclaredConstructor = 8983,
ERR_EncUpdateFailedDelegateTypeChanged = 8984,

ERR_IncorrectNullCheckSyntax = 8990,
ERR_DiscardCannotBeNullChecked = 8990,
ERR_MustNullCheckInImplementation = 8991,
ERR_NonNullableValueTypeIsNullChecked = 8992,
WRN_NullCheckedHasDefaultNull = 8993,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,10 @@ internal static void ReportParameterNullCheckingErrors(DiagnosticBag diagnostics
{
diagnostics.Add(useSiteInfo.DiagnosticInfo, location);
}
if (parameter.IsDiscard)
{
diagnostics.Add(ErrorCode.ERR_DiscardCannotBeNullChecked, location);
}
if (parameter.TypeWithAnnotations.NullableAnnotation.IsAnnotated()
|| parameter.Type.IsNullableTypeOrTypeParameter())
{
Expand Down
10 changes: 5 additions & 5 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit aad378e

Please sign in to comment.