-
Notifications
You must be signed in to change notification settings - Fork 10.1k
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
Enable throw helper analyzers #45954
Conversation
@stephentoub I copied the analyzers you enabled here. How did you handle projects that target older frameworks? Or was it not an issue in dotnet/runtime projects? |
For projects that only target older frameworks, it's not an issue, since the analyzers key off of the APIs being available. For projects that multitarget and thus where the analyzers would trigger for some builds but the methods aren't available for others, I effectively suppressed the analyzers: |
Ok! I'll do the same for additional projects in this PR. At the very least, if we tackle them in aspnetcore, it can be in a follow-up PR. That helps reduce the size of this PR.
That works well, except when the fixer updates shared source files that are reused in a multitargeted project. We have a lot of small shared source files in aspnetcore. I could suppress analyzers in the shared source, but adding the throw helpers is relatively easy. |
src/Components/Server/src/Builder/ComponentEndpointRouteBuilderExtensions.cs
Show resolved
Hide resolved
src/Components/Server/src/DependencyInjection/ServerSideBlazorBuilderExtensions.cs
Show resolved
Hide resolved
LGTM. The only nit: I had was somewhat inconsistent whitespace between throws invocations (probably a side effect of how the code was in the first place). Thanks for the explanation on why you needed the throw helpers, was scratching my head when I first looked at it. |
@mitchdenny Whitespace lines between throw helpers removed There were about 800 😮💨 regex: |
...sting/Server.IntegrationTesting/src/Deployers/RemoteWindowsDeployer/RemoteWindowsDeployer.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.
this is a doozy to review; nit: a bit late now, but I'd probably have said just do the ANEs in one big beast of a PR, as that is going to be the majority and the simplest - then a separate PR for the rest, allowing us to actually focus on the others where there may be more subtlety required, without losing focus (resumes scrolling)
One thing I can't tell from the PR: the analyzer that suggests this - does it ever make the suggestion for #if DEBUG
public static void ThrowIfNull<T>(T value, ...) where T : class
#else
public static void ThrowIfNull(object value, ...)
#endif |
src/Middleware/ResponseCaching/src/Streams/SegmentWriteStream.cs
Outdated
Show resolved
Hide resolved
The analyzer is fairly conservative and requires that the parameter type be a reference type: |
eyeballed all, and my browser officially hates this PR, but: nothing nefarious or worrying found; a few nits/comments added above |
7c3de47
to
d5e79bd
Compare
Exactly one CPU core was harmed in the reviewing of this pull request. |
Fixes #43482
Fixes #43503
Enable analyzers for validating null and argument ranges, object disposed, etc.
These helpers aren't available on older frameworks. Complications from projects that target netframework462 and netstandard2.0 mean that:
ArgumentNullException
and other exceptions. The helpers call through to those methods internally if available.ArgumentOutOfRangeException
throw helpers use generic math. I've made the custom method throw helpers just takeint
. If there are places where other types are used, then we can add those as needed. You can see thatArgumentOutOfRangeThrowHelper.cs
.This PR currently only fixes a portion of warnings from aspnetcore. I want to get feedback on the approach before continuing with the rest of the repo.