Skip to content

Enable CA1854 #44799

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
merged 4 commits into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ dotnet_diagnostic.CA1847.severity = warning
# CA1852: Seal internal types
dotnet_diagnostic.CA1852.severity = warning

# CA1854: Prefer the IDictionary.TryGetValue(TKey, out TValue) method
dotnet_diagnostic.CA1854.severity = warning

# CA2007: Consider calling ConfigureAwait on the awaited task
dotnet_diagnostic.CA2007.severity = warning

Expand Down Expand Up @@ -328,6 +331,8 @@ dotnet_diagnostic.CA1846.severity = suggestion
dotnet_diagnostic.CA1847.severity = suggestion
# CA1852: Seal internal types
dotnet_diagnostic.CA1852.severity = suggestion
# CA1854: Prefer the IDictionary.TryGetValue(TKey, out TValue) method
dotnet_diagnostic.CA1854.severity = suggestion
# CA2007: Consider calling ConfigureAwait on the awaited task
dotnet_diagnostic.CA2007.severity = suggestion
# CA2008: Do not create tasks without passing a TaskScheduler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,10 @@ private static void ApplyResponseTags(OpenApiOperation operation, XPathNodeItera
while (responseNodes.MoveNext())
{
var code = responseNodes.Current!.GetAttribute("code", "");
var response = operation.Responses.ContainsKey(code)
? operation.Responses[code]
: operation.Responses[code] = new OpenApiResponse();
if (!operation.Responses.TryGetValue(code, out var response))
{
operation.Responses[code] = response = new OpenApiResponse();
}

response.Description = XmlCommentsTextHelper.Humanize(responseNodes.Current.InnerXml);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ protected AuthenticationSchemeProvider(IOptions<AuthenticationOptions> options,
/// <param name="name">The name of the authenticationScheme.</param>
/// <returns>The scheme or null if not found.</returns>
public virtual Task<AuthenticationScheme?> GetSchemeAsync(string name)
=> Task.FromResult(_schemes.ContainsKey(name) ? _schemes[name] : null);
=> Task.FromResult(_schemes.TryGetValue(name, out var scheme) ? scheme : null);

/// <summary>
/// Returns the schemes in priority order for request handling.
Expand Down Expand Up @@ -184,13 +184,13 @@ public virtual void AddScheme(AuthenticationScheme scheme)
/// <param name="name">The name of the authenticationScheme being removed.</param>
public virtual void RemoveScheme(string name)
{
if (!_schemes.TryGetValue(name, out var scheme))
if (!_schemes.TryGetValue(name, out _))
{
return;
}
lock (_lock)
{
if (_schemes.TryGetValue(name, out scheme))
if (_schemes.TryGetValue(name, out var scheme))
{
if (_requestHandlers.Remove(scheme))
{
Expand Down
4 changes: 2 additions & 2 deletions src/Mvc/Mvc.Core/src/Formatters/TextOutputFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,9 @@ internal static IList<StringWithQualityHeaderValue> GetAcceptCharsetHeaderValues
private string GetMediaTypeWithCharset(string mediaType, Encoding encoding)
{
if (string.Equals(encoding.WebName, Encoding.UTF8.WebName, StringComparison.OrdinalIgnoreCase) &&
OutputMediaTypeCache.ContainsKey(mediaType))
OutputMediaTypeCache.TryGetValue(mediaType, out var mediaTypeWithCharset))
{
return OutputMediaTypeCache[mediaType];
return mediaTypeWithCharset;
}

return MediaType.ReplaceEncoding(mediaType, encoding);
Expand Down
4 changes: 2 additions & 2 deletions src/Mvc/shared/Mvc.Views.TestCommon/TestFileProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ public void DeleteFile(string path)

public virtual IFileInfo GetFileInfo(string subpath)
{
if (_lookup.ContainsKey(subpath))
if (_lookup.TryGetValue(subpath, out var fileInfo))
{
return _lookup[subpath];
return fileInfo;
}
else
{
Expand Down
10 changes: 5 additions & 5 deletions src/Servers/IIS/IntegrationTesting.IIS/src/IISDeployer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,14 @@ private void GetLogsFromFile()
{
// Handle cases where debug file is redirected by test
var debugLogLocations = new List<string>();
if (IISDeploymentParameters.HandlerSettings.ContainsKey("debugFile"))
if (IISDeploymentParameters.HandlerSettings.TryGetValue("debugFile", out var debugFile))
{
debugLogLocations.Add(IISDeploymentParameters.HandlerSettings["debugFile"]);
debugLogLocations.Add(debugFile);
}

if (DeploymentParameters.EnvironmentVariables.ContainsKey("ASPNETCORE_MODULE_DEBUG_FILE"))
if (DeploymentParameters.EnvironmentVariables.TryGetValue("ASPNETCORE_MODULE_DEBUG_FILE", out debugFile))
{
debugLogLocations.Add(DeploymentParameters.EnvironmentVariables["ASPNETCORE_MODULE_DEBUG_FILE"]);
debugLogLocations.Add(debugFile);
}

// default debug file name
Expand Down Expand Up @@ -397,7 +397,7 @@ private void Stop()
Logger.LogInformation($"Stopping pool, state: {state}");
}
}

// Make sure all sites are stopped
foreach (var site in serverManager.Sites)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ private async Task EnsurePackagesInProjectAsync(FileInfo projectFile, CodeGenera
foreach (var kvp in attributePackages)
{
var packageId = kvp.Key;
var version = urlPackages != null && urlPackages.ContainsKey(packageId) ? urlPackages[packageId] : kvp.Value;
var version = urlPackages != null && urlPackages.TryGetValue(packageId, out var urlPackageVersion) ? urlPackageVersion : kvp.Value;

await TryAddPackage(packageId, version, projectFile);
}
Expand Down