Skip to content

Commit c096dbb

Browse files
authored
Enable throw helper analyzers (dotnet#45954)
1 parent 351d34b commit c096dbb

File tree

1,145 files changed

+3907
-14776
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,145 files changed

+3907
-14776
lines changed

.editorconfig

+40
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,18 @@ dotnet_diagnostic.CA1305.severity = warning
9292
# CA1507: Use nameof to express symbol names
9393
dotnet_diagnostic.CA1507.severity = warning
9494

95+
# CA1510: Use ArgumentNullException throw helper
96+
dotnet_diagnostic.CA1510.severity = warning
97+
98+
# CA1511: Use ArgumentException throw helper
99+
dotnet_diagnostic.CA1511.severity = warning
100+
101+
# CA1512: Use ArgumentOutOfRangeException throw helper
102+
dotnet_diagnostic.CA1512.severity = warning
103+
104+
# CA1513: Use ObjectDisposedException throw helper
105+
dotnet_diagnostic.CA1513.severity = warning
106+
95107
# CA1725: Parameter names should match base declaration
96108
dotnet_diagnostic.CA1725.severity = suggestion
97109

@@ -187,6 +199,18 @@ dotnet_diagnostic.CA1852.severity = warning
187199
# CA1854: Prefer the IDictionary.TryGetValue(TKey, out TValue) method
188200
dotnet_diagnostic.CA1854.severity = warning
189201

202+
# CA1855: Prefer 'Clear' over 'Fill'
203+
dotnet_diagnostic.CA1855.severity = warning
204+
205+
# CA1856: Incorrect usage of ConstantExpected attribute
206+
dotnet_diagnostic.CA1856.severity = error
207+
208+
# CA1857: A constant is expected for the parameter
209+
dotnet_diagnostic.CA1857.severity = warning
210+
211+
# CA1858: Use 'StartsWith' instead of 'IndexOf'
212+
dotnet_diagnostic.CA1858.severity = warning
213+
190214
# CA2007: Consider calling ConfigureAwait on the awaited task
191215
dotnet_diagnostic.CA2007.severity = warning
192216

@@ -295,6 +319,14 @@ dotnet_diagnostic.IDE2000.severity = warning
295319
dotnet_diagnostic.CA1018.severity = suggestion
296320
# CA1507: Use nameof to express symbol names
297321
dotnet_diagnostic.CA1507.severity = suggestion
322+
# CA1510: Use ArgumentNullException throw helper
323+
dotnet_diagnostic.CA1510.severity = suggestion
324+
# CA1511: Use ArgumentException throw helper
325+
dotnet_diagnostic.CA1511.severity = suggestion
326+
# CA1512: Use ArgumentOutOfRangeException throw helper
327+
dotnet_diagnostic.CA1512.severity = suggestion
328+
# CA1513: Use ObjectDisposedException throw helper
329+
dotnet_diagnostic.CA1513.severity = suggestion
298330
# CA1802: Use literals where appropriate
299331
dotnet_diagnostic.CA1802.severity = suggestion
300332
# CA1805: Do not initialize unnecessarily
@@ -333,6 +365,14 @@ dotnet_diagnostic.CA1847.severity = suggestion
333365
dotnet_diagnostic.CA1852.severity = suggestion
334366
# CA1854: Prefer the IDictionary.TryGetValue(TKey, out TValue) method
335367
dotnet_diagnostic.CA1854.severity = suggestion
368+
# CA1855: Prefer 'Clear' over 'Fill'
369+
dotnet_diagnostic.CA1855.severity = suggestion
370+
# CA1856: Incorrect usage of ConstantExpected attribute
371+
dotnet_diagnostic.CA1856.severity = suggestion
372+
# CA1857: A constant is expected for the parameter
373+
dotnet_diagnostic.CA1857.severity = suggestion
374+
# CA1858: Use 'StartsWith' instead of 'IndexOf'
375+
dotnet_diagnostic.CA1858.severity = suggestion
336376
# CA2007: Consider calling ConfigureAwait on the awaited task
337377
dotnet_diagnostic.CA2007.severity = suggestion
338378
# CA2008: Do not create tasks without passing a TaskScheduler

Directory.Build.targets

+6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@
2828
<!-- Ignore API doc requirements for test assets -->
2929
<NoWarn Condition="'$(IsTestAssetProject)' == 'true' or '$(IsSampleProject)' == 'true' or '$(IsBenchmarkProject)' == 'true' or
3030
'$(IsMicrobenchmarksProject)' == 'true'">$(NoWarn);CS1591</NoWarn>
31+
32+
<!-- Ignore analyzers that recommend APIs introduced in .NET Core when targeting frameworks that lack those APIs
33+
to avoid issues with multitargeting.
34+
Some projects only build DefaultNetCoreTargetFramework when DotNetBuildFromSource is true so also suppress for source build.
35+
-->
36+
<NoWarn Condition="$(TargetFrameworks.Contains('NetFramework')) or $(TargetFrameworks.Contains('netstandard')) or '$(DotNetBuildFromSource)' == 'true'">$(NoWarn);CA1510;CA1511;CA1512;CA1513</NoWarn>
3137
</PropertyGroup>
3238

3339
<PropertyGroup Label="Resx settings">

src/Antiforgery/src/AntiforgeryServiceCollectionExtensions.cs

+3-13
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ public static class AntiforgeryServiceCollectionExtensions
2020
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
2121
public static IServiceCollection AddAntiforgery(this IServiceCollection services)
2222
{
23-
if (services == null)
24-
{
25-
throw new ArgumentNullException(nameof(services));
26-
}
23+
ArgumentNullException.ThrowIfNull(services);
2724

2825
services.AddDataProtection();
2926

@@ -57,15 +54,8 @@ public static IServiceCollection AddAntiforgery(this IServiceCollection services
5754
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
5855
public static IServiceCollection AddAntiforgery(this IServiceCollection services, Action<AntiforgeryOptions> setupAction)
5956
{
60-
if (services == null)
61-
{
62-
throw new ArgumentNullException(nameof(services));
63-
}
64-
65-
if (setupAction == null)
66-
{
67-
throw new ArgumentNullException(nameof(setupAction));
68-
}
57+
ArgumentNullException.ThrowIfNull(services);
58+
ArgumentNullException.ThrowIfNull(setupAction);
6959

7060
services.AddAntiforgery();
7161
services.Configure(setupAction);

src/Antiforgery/src/AntiforgeryTokenSet.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ public AntiforgeryTokenSet(
2121
string formFieldName,
2222
string? headerName)
2323
{
24-
if (formFieldName == null)
25-
{
26-
throw new ArgumentNullException(nameof(formFieldName));
27-
}
24+
ArgumentNullException.ThrowIfNull(formFieldName);
2825

2926
RequestToken = requestToken;
3027
CookieToken = cookieToken;

src/Antiforgery/src/Internal/AntiforgeryOptionsSetup.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ public AntiforgeryOptionsSetup(IOptions<DataProtectionOptions> dataProtectionOpt
2020

2121
public void Configure(AntiforgeryOptions options)
2222
{
23-
if (options == null)
24-
{
25-
throw new ArgumentNullException(nameof(options));
26-
}
23+
ArgumentNullException.ThrowIfNull(options);
2724

2825
if (options.Cookie.Name == null)
2926
{

src/Antiforgery/src/Internal/DefaultAntiforgery.cs

+5-20
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@ public DefaultAntiforgery(
3939
/// <inheritdoc />
4040
public AntiforgeryTokenSet GetAndStoreTokens(HttpContext httpContext)
4141
{
42-
if (httpContext == null)
43-
{
44-
throw new ArgumentNullException(nameof(httpContext));
45-
}
42+
ArgumentNullException.ThrowIfNull(httpContext);
4643

4744
CheckSSLConfig(httpContext);
4845

@@ -79,10 +76,7 @@ public AntiforgeryTokenSet GetAndStoreTokens(HttpContext httpContext)
7976
/// <inheritdoc />
8077
public AntiforgeryTokenSet GetTokens(HttpContext httpContext)
8178
{
82-
if (httpContext == null)
83-
{
84-
throw new ArgumentNullException(nameof(httpContext));
85-
}
79+
ArgumentNullException.ThrowIfNull(httpContext);
8680

8781
CheckSSLConfig(httpContext);
8882

@@ -93,10 +87,7 @@ public AntiforgeryTokenSet GetTokens(HttpContext httpContext)
9387
/// <inheritdoc />
9488
public async Task<bool> IsRequestValidAsync(HttpContext httpContext)
9589
{
96-
if (httpContext == null)
97-
{
98-
throw new ArgumentNullException(nameof(httpContext));
99-
}
90+
ArgumentNullException.ThrowIfNull(httpContext);
10091

10192
CheckSSLConfig(httpContext);
10293

@@ -151,10 +142,7 @@ public async Task<bool> IsRequestValidAsync(HttpContext httpContext)
151142
/// <inheritdoc />
152143
public async Task ValidateRequestAsync(HttpContext httpContext)
153144
{
154-
if (httpContext == null)
155-
{
156-
throw new ArgumentNullException(nameof(httpContext));
157-
}
145+
ArgumentNullException.ThrowIfNull(httpContext);
158146

159147
CheckSSLConfig(httpContext);
160148

@@ -220,10 +208,7 @@ private void ValidateTokens(HttpContext httpContext, AntiforgeryTokenSet antifor
220208
/// <inheritdoc />
221209
public void SetCookieTokenAndHeader(HttpContext httpContext)
222210
{
223-
if (httpContext == null)
224-
{
225-
throw new ArgumentNullException(nameof(httpContext));
226-
}
211+
ArgumentNullException.ThrowIfNull(httpContext);
227212

228213
CheckSSLConfig(httpContext);
229214

src/Antiforgery/src/Internal/DefaultAntiforgeryTokenGenerator.cs

+3-13
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,8 @@ public AntiforgeryToken GenerateRequestToken(
3636
HttpContext httpContext,
3737
AntiforgeryToken cookieToken)
3838
{
39-
if (httpContext == null)
40-
{
41-
throw new ArgumentNullException(nameof(httpContext));
42-
}
43-
44-
if (cookieToken == null)
45-
{
46-
throw new ArgumentNullException(nameof(cookieToken));
47-
}
39+
ArgumentNullException.ThrowIfNull(httpContext);
40+
ArgumentNullException.ThrowIfNull(cookieToken);
4841

4942
if (!IsCookieTokenValid(cookieToken))
5043
{
@@ -112,10 +105,7 @@ public bool TryValidateTokenSet(
112105
AntiforgeryToken requestToken,
113106
[NotNullWhen(false)] out string? message)
114107
{
115-
if (httpContext == null)
116-
{
117-
throw new ArgumentNullException(nameof(httpContext));
118-
}
108+
ArgumentNullException.ThrowIfNull(httpContext);
119109

120110
if (cookieToken == null)
121111
{

src/Antiforgery/src/Internal/DefaultAntiforgeryTokenSerializer.cs

+3-13
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,8 @@ public DefaultAntiforgeryTokenSerializer(
1919
IDataProtectionProvider provider,
2020
ObjectPool<AntiforgerySerializationContext> pool)
2121
{
22-
if (provider == null)
23-
{
24-
throw new ArgumentNullException(nameof(provider));
25-
}
26-
27-
if (pool == null)
28-
{
29-
throw new ArgumentNullException(nameof(pool));
30-
}
22+
ArgumentNullException.ThrowIfNull(provider);
23+
ArgumentNullException.ThrowIfNull(pool);
3124

3225
_cryptoSystem = provider.CreateProtector(Purpose);
3326
_pool = pool;
@@ -131,10 +124,7 @@ public AntiforgeryToken Deserialize(string serializedToken)
131124

132125
public string Serialize(AntiforgeryToken token)
133126
{
134-
if (token == null)
135-
{
136-
throw new ArgumentNullException(nameof(token));
137-
}
127+
ArgumentNullException.ThrowIfNull(token);
138128

139129
var serializationContext = _pool.Get();
140130

src/Antiforgery/src/Internal/DefaultAntiforgeryTokenStore.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ internal sealed class DefaultAntiforgeryTokenStore : IAntiforgeryTokenStore
1414

1515
public DefaultAntiforgeryTokenStore(IOptions<AntiforgeryOptions> optionsAccessor)
1616
{
17-
if (optionsAccessor == null)
18-
{
19-
throw new ArgumentNullException(nameof(optionsAccessor));
20-
}
17+
ArgumentNullException.ThrowIfNull(optionsAccessor);
2118

2219
_options = optionsAccessor.Value;
2320
}

src/Azure/AzureAppServicesIntegration/src/AppServicesWebHostBuilderExtensions.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ public static class AppServicesWebHostBuilderExtensions
1717
/// <returns></returns>
1818
public static IWebHostBuilder UseAzureAppServices(this IWebHostBuilder hostBuilder)
1919
{
20-
if (hostBuilder == null)
21-
{
22-
throw new ArgumentNullException(nameof(hostBuilder));
23-
}
20+
ArgumentNullException.ThrowIfNull(hostBuilder);
2421
#pragma warning disable 618
2522
hostBuilder.ConfigureLogging(builder => builder.AddAzureWebAppDiagnostics());
2623
#pragma warning restore 618

src/Components/Authorization/src/AuthenticationStateProvider.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ public abstract class AuthenticationStateProvider
2626
/// <param name="task">A <see cref="Task"/> that supplies the updated <see cref="AuthenticationState"/>.</param>
2727
protected void NotifyAuthenticationStateChanged(Task<AuthenticationState> task)
2828
{
29-
if (task == null)
30-
{
31-
throw new ArgumentNullException(nameof(task));
32-
}
29+
ArgumentNullException.ThrowIfNull(task);
3330

3431
AuthenticationStateChanged?.Invoke(task);
3532
}

src/Components/Components/src/BindElementAttribute.cs

+3-14
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,9 @@ public sealed class BindElementAttribute : Attribute
1818
/// <param name="changeAttribute">The name of an attribute that will register an associated change event.</param>
1919
public BindElementAttribute(string element, string? suffix, string valueAttribute, string changeAttribute)
2020
{
21-
if (element == null)
22-
{
23-
throw new ArgumentNullException(nameof(element));
24-
}
25-
26-
if (valueAttribute == null)
27-
{
28-
throw new ArgumentNullException(nameof(valueAttribute));
29-
}
30-
31-
if (changeAttribute == null)
32-
{
33-
throw new ArgumentNullException(nameof(changeAttribute));
34-
}
21+
ArgumentNullException.ThrowIfNull(element);
22+
ArgumentNullException.ThrowIfNull(valueAttribute);
23+
ArgumentNullException.ThrowIfNull(changeAttribute);
3524

3625
Element = element;
3726
ValueAttribute = valueAttribute;

src/Components/Components/src/CascadingTypeParameterAttribute.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ public sealed class CascadingTypeParameterAttribute : Attribute
1717
/// <param name="name">The name of the type parameter.</param>
1818
public CascadingTypeParameterAttribute(string name)
1919
{
20-
if (name == null)
21-
{
22-
throw new ArgumentNullException(nameof(name));
23-
}
20+
ArgumentNullException.ThrowIfNull(name);
2421

2522
Name = name;
2623
}

src/Components/Components/src/Dispatcher.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,7 @@ public void AssertAccess()
8585
/// <param name="e">The <see cref="UnhandledExceptionEventArgs"/>.</param>
8686
protected void OnUnhandledException(UnhandledExceptionEventArgs e)
8787
{
88-
if (e is null)
89-
{
90-
throw new ArgumentNullException(nameof(e));
91-
}
88+
ArgumentNullException.ThrowIfNull(e);
9289

9390
UnhandledException?.Invoke(this, e);
9491
}

src/Components/Components/src/ErrorBoundaryBase.cs

+1-5
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,7 @@ public void Recover()
5656

5757
void IErrorBoundary.HandleException(Exception exception)
5858
{
59-
if (exception is null)
60-
{
61-
// This would be a framework bug if it happened. It should not be possible.
62-
throw new ArgumentNullException(nameof(exception));
63-
}
59+
ArgumentNullException.ThrowIfNull(exception);
6460

6561
// If rendering the error content itself causes an error, then re-rendering on error risks creating an
6662
// infinite error loop. Unfortunately it's very hard to distinguish whether the error source is "child content"

0 commit comments

Comments
 (0)