-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Add numeric parameter checks refactoring #78748
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
CyrusNajmabadi
merged 5 commits into
dotnet:main
from
DoctorKrolic:numeric-parameter-check
May 29, 2025
Merged
Changes from all commits
Commits
Show all changes
5 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 |
|---|---|---|
|
|
@@ -220,7 +220,7 @@ public async Task TestNotOnValueType() | |
|
|
||
| class C | ||
| { | ||
| public C([||]int i) | ||
| public C([||]DateTime d) | ||
| { | ||
| } | ||
| } | ||
|
|
@@ -3582,4 +3582,352 @@ void M(object o, DayOfWeek dayOfWeek, string s) | |
| ReferenceAssemblies = ReferenceAssemblies.Net.Net80 | ||
| }.RunAsync(); | ||
| } | ||
|
|
||
| [Theory, WorkItem("https://github.com/dotnet/roslyn/issues/37653")] | ||
| [InlineData("sbyte")] | ||
| [InlineData("short")] | ||
| [InlineData("int")] | ||
| [InlineData("long")] | ||
| public async Task TestSimpleNumericChecks_ModernOverloads(string validNumericType) | ||
| { | ||
| var code = $$""" | ||
| using System; | ||
|
|
||
| class C | ||
| { | ||
| void M({{validNumericType}} [|num|]) | ||
| { | ||
| } | ||
| } | ||
| """; | ||
|
|
||
| await new VerifyCS.Test() | ||
| { | ||
| TestCode = code, | ||
| FixedCode = $$""" | ||
| using System; | ||
|
|
||
| class C | ||
| { | ||
| void M({{validNumericType}} [|num|]) | ||
| { | ||
| ArgumentOutOfRangeException.ThrowIfNegative(num); | ||
| } | ||
| } | ||
| """, | ||
| ReferenceAssemblies = ReferenceAssemblies.Net.Net80, | ||
| CodeActionIndex = 0, | ||
| }.RunAsync(); | ||
|
|
||
| await new VerifyCS.Test() | ||
| { | ||
| TestCode = code, | ||
| FixedCode = $$""" | ||
| using System; | ||
|
|
||
| class C | ||
| { | ||
| void M({{validNumericType}} [|num|]) | ||
| { | ||
| ArgumentOutOfRangeException.ThrowIfNegativeOrZero(num); | ||
| } | ||
| } | ||
| """, | ||
| ReferenceAssemblies = ReferenceAssemblies.Net.Net80, | ||
| CodeActionIndex = 1, | ||
| }.RunAsync(); | ||
| } | ||
|
|
||
| [Theory, WorkItem("https://github.com/dotnet/roslyn/issues/37653")] | ||
| [InlineData("sbyte")] | ||
| [InlineData("short")] | ||
| [InlineData("int")] | ||
| [InlineData("long")] | ||
| public async Task TestSimpleNumericChecks_OldStyleCheckStatement(string validNumericType) | ||
| { | ||
| var code = $$""" | ||
| using System; | ||
|
|
||
| class C | ||
| { | ||
| void M({{validNumericType}} [|num|]) | ||
| { | ||
| } | ||
| } | ||
| """; | ||
|
|
||
| await new VerifyCS.Test() | ||
| { | ||
| TestCode = code, | ||
| FixedCode = $$""" | ||
| using System; | ||
|
|
||
| class C | ||
| { | ||
| void M({{validNumericType}} [|num|]) | ||
| { | ||
| if (num < 0) | ||
| { | ||
| throw new ArgumentOutOfRangeException(nameof(num), num, $"{{string.Format(FeaturesResources._0_cannot_be_negative, "{nameof(num)}").Replace("\"", "\\\"")}}"); | ||
| } | ||
| } | ||
| } | ||
| """, | ||
| ReferenceAssemblies = ReferenceAssemblies.NetStandard.NetStandard20, | ||
| CodeActionIndex = 0, | ||
| }.RunAsync(); | ||
|
|
||
| await new VerifyCS.Test() | ||
| { | ||
| TestCode = code, | ||
| FixedCode = $$""" | ||
| using System; | ||
|
|
||
| class C | ||
| { | ||
| void M({{validNumericType}} [|num|]) | ||
| { | ||
| if (num <= 0) | ||
| { | ||
| throw new ArgumentOutOfRangeException(nameof(num), num, $"{{string.Format(FeaturesResources._0_cannot_be_negative_or_zero, "{nameof(num)}").Replace("\"", "\\\"")}}"); | ||
| } | ||
| } | ||
| } | ||
| """, | ||
| ReferenceAssemblies = ReferenceAssemblies.NetStandard.NetStandard20, | ||
| CodeActionIndex = 1, | ||
| }.RunAsync(); | ||
| } | ||
|
|
||
| [Theory, WorkItem("https://github.com/dotnet/roslyn/issues/37653")] | ||
| [InlineData("byte")] | ||
| [InlineData("ushort")] | ||
| [InlineData("uint")] | ||
| [InlineData("ulong")] | ||
| [InlineData("float")] | ||
| [InlineData("double")] | ||
| public async Task TestNoNumericChecksForUnsignedAndFloatingPointNumericTypes(string invalidNumericType) | ||
| { | ||
| await VerifyCS.VerifyRefactoringAsync($$""" | ||
| using System; | ||
|
|
||
| class C | ||
| { | ||
| void M({{invalidNumericType}} [|num|]) | ||
| { | ||
| } | ||
| } | ||
| """); | ||
| } | ||
|
|
||
| [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/37653")] | ||
| public async Task TestNoNumericChecksForOutParameter() | ||
| { | ||
| await VerifyCS.VerifyRefactoringAsync(""" | ||
| using System; | ||
|
|
||
| class C | ||
| { | ||
| void M(out int [|num|]) | ||
| { | ||
| num = 0; | ||
| } | ||
| } | ||
| """); | ||
| } | ||
|
|
||
| [Theory, WorkItem("https://github.com/dotnet/roslyn/issues/37653")] | ||
| [InlineData("ThrowIfNegative(num)")] | ||
| [InlineData("ThrowIfNegativeOrZero(num)")] | ||
| [InlineData("ThrowIfEqual(num, 5)")] | ||
| [InlineData("ThrowIfGreaterThan(num, 6)")] | ||
| [InlineData("ThrowIfGreaterThanOrEqual(num, 1)")] | ||
| [InlineData("ThrowIfLessThan(num, 2)")] | ||
| [InlineData("ThrowIfLessThanOrEqual(num, 3)")] | ||
| [InlineData("ThrowIfEqual(num, 15)")] | ||
| [InlineData("ThrowIfNotEqual(num, 10)")] | ||
| [InlineData("ThrowIfZero(num)")] | ||
|
Contributor
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. Although we have refactorrings only for |
||
| public async Task TestNoNumericChecksIfAlreadyExist_ModernOverloads(string methodInvocation) | ||
| { | ||
| await new VerifyCS.Test() | ||
| { | ||
| TestCode = $$""" | ||
| using System; | ||
|
|
||
| class C | ||
| { | ||
| void M(int [|num|]) | ||
| { | ||
| ArgumentOutOfRangeException.{{methodInvocation}}; | ||
| } | ||
| } | ||
| """, | ||
| ReferenceAssemblies = ReferenceAssemblies.Net.Net80 | ||
| }.RunAsync(); | ||
| } | ||
|
|
||
| [Theory, WorkItem("https://github.com/dotnet/roslyn/issues/37653")] | ||
| [InlineData("num < 0")] | ||
| [InlineData("num <= 0")] | ||
| [InlineData("num > 11")] | ||
| [InlineData("num >= 12")] | ||
| [InlineData("0 > num")] | ||
| [InlineData("0 >= num")] | ||
| [InlineData("14 < num")] | ||
| [InlineData("22 <= num")] | ||
| [InlineData("num is < 0")] | ||
| [InlineData("num is <= 0")] | ||
| [InlineData("num < 1")] | ||
| [InlineData("num <= 39")] | ||
| [InlineData("25 > num")] | ||
| [InlineData("29 >= num")] | ||
| [InlineData("num is < 8")] | ||
| [InlineData("num is <= 18")] | ||
| [InlineData("num is > 5")] | ||
| [InlineData("num is >= 3")] | ||
| public async Task TestNoNumericChecksIfAlreadyExist_OldStyleCheckStatements(string numericCheck) | ||
| { | ||
| await new VerifyCS.Test() | ||
| { | ||
| TestCode = $$""" | ||
| using System; | ||
|
|
||
| class C | ||
| { | ||
| void M(int [|num|]) | ||
| { | ||
| if ({{numericCheck}}) | ||
| { | ||
| throw new ArgumentOutOfRangeException(nameof(num)); | ||
| } | ||
| } | ||
| } | ||
| """, | ||
| LanguageVersion = LanguageVersion.Latest, | ||
| ReferenceAssemblies = ReferenceAssemblies.Net.Net80 | ||
| }.RunAsync(); | ||
| } | ||
|
|
||
| [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/37653")] | ||
| public async Task TestNumericChecksAfterAnotherNumericCheck() | ||
| { | ||
| await new VerifyCS.Test() | ||
| { | ||
| TestCode = """ | ||
| using System; | ||
|
|
||
| class C | ||
| { | ||
| void M(sbyte a, short [|b|]) | ||
| { | ||
| ArgumentOutOfRangeException.ThrowIfNegative(a); | ||
| } | ||
| } | ||
| """, | ||
| FixedCode = """ | ||
| using System; | ||
|
|
||
| class C | ||
| { | ||
| void M(sbyte a, short [|b|]) | ||
| { | ||
| ArgumentOutOfRangeException.ThrowIfNegative(a); | ||
| ArgumentOutOfRangeException.ThrowIfNegative(b); | ||
| } | ||
| } | ||
| """, | ||
| ReferenceAssemblies = ReferenceAssemblies.Net.Net80, | ||
| }.RunAsync(); | ||
| } | ||
|
|
||
| [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/37653")] | ||
| public async Task TestNumericChecksBeforeAnotherNumericCheck() | ||
| { | ||
| await new VerifyCS.Test() | ||
| { | ||
| TestCode = """ | ||
| using System; | ||
|
|
||
| class C | ||
| { | ||
| void M(long [|a|], int b) | ||
| { | ||
| if (b < 0) | ||
| { | ||
| throw new ArgumentOutOfRangeException(nameof(b)); | ||
| } | ||
| } | ||
| } | ||
| """, | ||
| FixedCode = """ | ||
| using System; | ||
|
|
||
| class C | ||
| { | ||
| void M(long a, int b) | ||
| { | ||
| ArgumentOutOfRangeException.ThrowIfNegativeOrZero(a); | ||
| if (b < 0) | ||
| { | ||
| throw new ArgumentOutOfRangeException(nameof(b)); | ||
| } | ||
| } | ||
| } | ||
| """, | ||
| ReferenceAssemblies = ReferenceAssemblies.Net.Net80, | ||
| CodeActionIndex = 1 | ||
| }.RunAsync(); | ||
| } | ||
|
|
||
| [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/37653")] | ||
| public async Task TestNumericChecksInBetweenDifferentChecks() | ||
| { | ||
| await new VerifyCS.Test() | ||
| { | ||
| TestCode = """ | ||
| using System; | ||
|
|
||
| class C | ||
| { | ||
| void M(DayOfWeek day, int [|i|], string s) | ||
| { | ||
| if (!Enum.IsDefined(typeof(DayOfWeek), day)) | ||
| { | ||
| throw new System.ComponentModel.InvalidEnumArgumentException(nameof(day), (int)day, typeof(DayOfWeek)); | ||
| } | ||
|
|
||
| if (string.IsNullOrEmpty(s)) | ||
| { | ||
| throw new ArgumentNullException(nameof(s)); | ||
| } | ||
| } | ||
| } | ||
| """, | ||
| FixedCode = $$""" | ||
| using System; | ||
|
|
||
| class C | ||
| { | ||
| void M(DayOfWeek day, int i, string s) | ||
| { | ||
| if (!Enum.IsDefined(typeof(DayOfWeek), day)) | ||
| { | ||
| throw new System.ComponentModel.InvalidEnumArgumentException(nameof(day), (int)day, typeof(DayOfWeek)); | ||
| } | ||
|
|
||
| if (i < 0) | ||
| { | ||
| throw new ArgumentOutOfRangeException(nameof(i), i, $"{{string.Format(FeaturesResources._0_cannot_be_negative, "{nameof(i)}").Replace("\"", "\\\"")}}"); | ||
| } | ||
|
|
||
| if (string.IsNullOrEmpty(s)) | ||
| { | ||
| throw new ArgumentNullException(nameof(s)); | ||
| } | ||
| } | ||
| } | ||
| """, | ||
| ReferenceAssemblies = ReferenceAssemblies.NetStandard.NetStandard20 | ||
| }.RunAsync(); | ||
| } | ||
| } | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
As soon as I edit something in VS it adds this. So maybe just keep it?