Skip to content

Commit 983ca23

Browse files
Enable CA1854 (dotnet#44799)
1 parent 41925af commit 983ca23

File tree

7 files changed

+22
-16
lines changed

7 files changed

+22
-16
lines changed

Diff for: .editorconfig

+5
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ dotnet_diagnostic.CA1847.severity = warning
184184
# CA1852: Seal internal types
185185
dotnet_diagnostic.CA1852.severity = warning
186186

187+
# CA1854: Prefer the IDictionary.TryGetValue(TKey, out TValue) method
188+
dotnet_diagnostic.CA1854.severity = warning
189+
187190
# CA2007: Consider calling ConfigureAwait on the awaited task
188191
dotnet_diagnostic.CA2007.severity = warning
189192

@@ -328,6 +331,8 @@ dotnet_diagnostic.CA1846.severity = suggestion
328331
dotnet_diagnostic.CA1847.severity = suggestion
329332
# CA1852: Seal internal types
330333
dotnet_diagnostic.CA1852.severity = suggestion
334+
# CA1854: Prefer the IDictionary.TryGetValue(TKey, out TValue) method
335+
dotnet_diagnostic.CA1854.severity = suggestion
331336
# CA2007: Consider calling ConfigureAwait on the awaited task
332337
dotnet_diagnostic.CA2007.severity = suggestion
333338
# CA2008: Do not create tasks without passing a TaskScheduler

Diff for: src/Grpc/JsonTranscoding/src/Microsoft.AspNetCore.Grpc.Swagger/Internal/XmlComments/GrpcXmlCommentsOperationFilter.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,10 @@ private static void ApplyResponseTags(OpenApiOperation operation, XPathNodeItera
100100
while (responseNodes.MoveNext())
101101
{
102102
var code = responseNodes.Current!.GetAttribute("code", "");
103-
var response = operation.Responses.ContainsKey(code)
104-
? operation.Responses[code]
105-
: operation.Responses[code] = new OpenApiResponse();
103+
if (!operation.Responses.TryGetValue(code, out var response))
104+
{
105+
operation.Responses[code] = response = new OpenApiResponse();
106+
}
106107

107108
response.Description = XmlCommentsTextHelper.Humanize(responseNodes.Current.InnerXml);
108109
}

Diff for: src/Http/Authentication.Core/src/AuthenticationSchemeProvider.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ protected AuthenticationSchemeProvider(IOptions<AuthenticationOptions> options,
120120
/// <param name="name">The name of the authenticationScheme.</param>
121121
/// <returns>The scheme or null if not found.</returns>
122122
public virtual Task<AuthenticationScheme?> GetSchemeAsync(string name)
123-
=> Task.FromResult(_schemes.ContainsKey(name) ? _schemes[name] : null);
123+
=> Task.FromResult(_schemes.TryGetValue(name, out var scheme) ? scheme : null);
124124

125125
/// <summary>
126126
/// Returns the schemes in priority order for request handling.
@@ -184,13 +184,13 @@ public virtual void AddScheme(AuthenticationScheme scheme)
184184
/// <param name="name">The name of the authenticationScheme being removed.</param>
185185
public virtual void RemoveScheme(string name)
186186
{
187-
if (!_schemes.TryGetValue(name, out var scheme))
187+
if (!_schemes.TryGetValue(name, out _))
188188
{
189189
return;
190190
}
191191
lock (_lock)
192192
{
193-
if (_schemes.TryGetValue(name, out scheme))
193+
if (_schemes.TryGetValue(name, out var scheme))
194194
{
195195
if (_requestHandlers.Remove(scheme))
196196
{

Diff for: src/Mvc/Mvc.Core/src/Formatters/TextOutputFormatter.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,9 @@ internal static IList<StringWithQualityHeaderValue> GetAcceptCharsetHeaderValues
187187
private string GetMediaTypeWithCharset(string mediaType, Encoding encoding)
188188
{
189189
if (string.Equals(encoding.WebName, Encoding.UTF8.WebName, StringComparison.OrdinalIgnoreCase) &&
190-
OutputMediaTypeCache.ContainsKey(mediaType))
190+
OutputMediaTypeCache.TryGetValue(mediaType, out var mediaTypeWithCharset))
191191
{
192-
return OutputMediaTypeCache[mediaType];
192+
return mediaTypeWithCharset;
193193
}
194194

195195
return MediaType.ReplaceEncoding(mediaType, encoding);

Diff for: src/Mvc/shared/Mvc.Views.TestCommon/TestFileProvider.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ public void DeleteFile(string path)
7070

7171
public virtual IFileInfo GetFileInfo(string subpath)
7272
{
73-
if (_lookup.ContainsKey(subpath))
73+
if (_lookup.TryGetValue(subpath, out var fileInfo))
7474
{
75-
return _lookup[subpath];
75+
return fileInfo;
7676
}
7777
else
7878
{

Diff for: src/Servers/IIS/IntegrationTesting.IIS/src/IISDeployer.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,14 @@ private void GetLogsFromFile()
158158
{
159159
// Handle cases where debug file is redirected by test
160160
var debugLogLocations = new List<string>();
161-
if (IISDeploymentParameters.HandlerSettings.ContainsKey("debugFile"))
161+
if (IISDeploymentParameters.HandlerSettings.TryGetValue("debugFile", out var debugFile))
162162
{
163-
debugLogLocations.Add(IISDeploymentParameters.HandlerSettings["debugFile"]);
163+
debugLogLocations.Add(debugFile);
164164
}
165165

166-
if (DeploymentParameters.EnvironmentVariables.ContainsKey("ASPNETCORE_MODULE_DEBUG_FILE"))
166+
if (DeploymentParameters.EnvironmentVariables.TryGetValue("ASPNETCORE_MODULE_DEBUG_FILE", out debugFile))
167167
{
168-
debugLogLocations.Add(DeploymentParameters.EnvironmentVariables["ASPNETCORE_MODULE_DEBUG_FILE"]);
168+
debugLogLocations.Add(debugFile);
169169
}
170170

171171
// default debug file name
@@ -397,7 +397,7 @@ private void Stop()
397397
Logger.LogInformation($"Stopping pool, state: {state}");
398398
}
399399
}
400-
400+
401401
// Make sure all sites are stopped
402402
foreach (var site in serverManager.Sites)
403403
{

Diff for: src/Tools/Microsoft.dotnet-openapi/src/Commands/BaseCommand.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ private async Task EnsurePackagesInProjectAsync(FileInfo projectFile, CodeGenera
195195
foreach (var kvp in attributePackages)
196196
{
197197
var packageId = kvp.Key;
198-
var version = urlPackages != null && urlPackages.ContainsKey(packageId) ? urlPackages[packageId] : kvp.Value;
198+
var version = urlPackages != null && urlPackages.TryGetValue(packageId, out var urlPackageVersion) ? urlPackageVersion : kvp.Value;
199199

200200
await TryAddPackage(packageId, version, projectFile);
201201
}

0 commit comments

Comments
 (0)