-
Notifications
You must be signed in to change notification settings - Fork 468
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
Add UseExceptionThrowHelpers analyzer/fixer #6293
Conversation
Flags patterns involving guarded construction of: - ArgumentNullException - ArgumentException - ArgumentOutOfRangeException - ObjectDisposedException that can be replaced with: - ArgumentNullException.ThrowIfNull - ArgumentException.ThrowIfNullOrEmpty - ArgumentOutOfRangeException.ThrowIfZero - ArgumentOutOfRangeException.ThrowIfNegative - ArgumentOutOfRangeException.ThrowIfNegativeOrZero - ArgumentOutOfRangeException.ThrowIfGreaterThan - ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual - ArgumentOutOfRangeException.ThrowIfLessThan - ArgumentOutOfRangeException.ThrowIfLessThanOrEqual - ObjectDisposedException.ThrowIf For now I have a different diagnostic ID per exception type, but we can either increase or decrease the number (e.g. one all-up, one per method, etc.) I've also trended towards preferring false negatives over false positives, so for example if the analyzer detects any "meaningful" additional arguments (e.g. an inner exception) being passed in, it doesn't warn. There's also a fixer included. It's capable of fixing all flagged call sites with the exception of some ObjectDisposedException uses. It'll fix `new ObjectDisposedException(GetType().Name)` for example, but it won't fix `new ObjectDisposedException("something")` as it won't know what Type/object to pass in as the thing being disposed of.
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/UseExceptionThrowHelpers.cs
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/UseExceptionThrowHelpers.cs
Show resolved
Hide resolved
src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/UseExceptionThrowHelpersTests.cs
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/UseExceptionThrowHelpers.cs
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/UseExceptionThrowHelpers.cs
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/UseExceptionThrowHelpers.cs
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/UseExceptionThrowHelpers.cs
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/UseExceptionThrowHelpers.cs
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/UseExceptionThrowHelpers.cs
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/UseExceptionThrowHelpers.cs
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/UseExceptionThrowHelpersTests.cs
Show resolved
Hide resolved
src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/UseExceptionThrowHelpersTests.cs
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/UseExceptionThrowHelpersTests.cs
Show resolved
Hide resolved
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.
LGTM
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## main #6293 +/- ##
==========================================
+ Coverage 96.06% 96.07% +0.01%
==========================================
Files 1364 1367 +3
Lines 313717 315337 +1620
Branches 10125 10187 +62
==========================================
+ Hits 301357 302961 +1604
- Misses 9939 9943 +4
- Partials 2421 2433 +12 |
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/UseExceptionThrowHelpers.cs
Show resolved
Hide resolved
- Added a unique title per diagnostic ID - Changed category to Maintainability rather than Performance. They're a bit of both, but more on the former. Thus also renumbered everything. - Better handle guards with else blocks. - Avoid flagging nullables and enums for ArgumentOutOfRangeException
src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/UseExceptionThrowHelpersTests.cs
Show resolved
Hide resolved
src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/UseExceptionThrowHelpersTests.cs
Show resolved
Hide resolved
src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/UseExceptionThrowHelpersTests.cs
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/UseExceptionThrowHelpersFixer.cs
Show resolved
Hide resolved
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.
LGTM, thanks, 👍 for the code comments
Thanks for the reviews. |
Flags patterns involving guarded construction of:
that can be replaced with:
For now I have a different diagnostic ID per exception type, but we can either increase or decrease the number (e.g. one all-up, one per method, etc.) We can add additional throw helpers to the analyzer/fixer as we add more. The Argument{Null}Exception and ObjectDisposedException helpers shipped in previous .NET releases; the ArgumentOutOfRangeException helpers are being added in dotnet/runtime#78222 from @hrrrrustic.
I've also trended towards preferring false negatives over false positives, so for example if the analyzer detects any "meaningful" additional arguments (e.g. an inner exception) being passed in, it doesn't warn.
There's also a fixer included. It's capable of fixing all flagged call sites with the exception of some ObjectDisposedException uses. It'll fix
new ObjectDisposedException(GetType().Name)
for example, but it won't fixnew ObjectDisposedException("something")
as it won't know what Type/object to pass in as the thing being disposed of.Fixes dotnet/runtime#68326
cc: @buyaa-n, @mavasani, @Youssef1313 for review
cc: @bartonjs, @terrajobst in case you have feedback/opinions on diagnostic ID usage