Skip to content
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

simplify some dictionary usage #4575

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ internal Assembly GetAssembly(string assemblyPath) => Path.GetFileNameWithoutExt

internal void GetNavigationData(string className, string methodName, out int minLineNumber, out string? fileName)
{
bool found = TypeMethodLocations.TryGetValue(className, out TypeLocation? typeLocation);

if (!found || typeLocation == null)
if (!TypeMethodLocations.TryGetValue(className, out TypeLocation? typeLocation))
{
minLineNumber = 0;
fileName = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public MethodInfo[] GetRuntimeMethods(Type type)
Dictionary<string, PropertyInfo> type = ReflectionDataProvider.TypePropertiesByName[classType];

// We as asking for TestContext here, it may not be there.
PropertyInfo? property = type.TryGetValue(propertyName, out PropertyInfo? propertyInfo) ? propertyInfo : null;
PropertyInfo? property = type.GetValueOrDefault(propertyName);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
PropertyInfo? property = type.GetValueOrDefault(propertyName);
_ = type.TryGetValue(propertyName, PropertyInfo? property);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not GetValueOrDefault ?

Copy link
Member

@Youssef1313 Youssef1313 Jan 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are the same, nothing really wrong about GetValueOrDefault.

GetValueOrDefault will end up calling TryGetValue at the end of the day, so I preferred to call TryGetValue directly unless GetValueOrDefault is saving annoying conditional syntax (like the case in GetMetadata below in the PR)

It's mostly personal opinion/style though.

return !includeNonPublic && (property?.GetMethod?.IsPublic == true || property?.SetMethod?.IsPublic == true)
? null
: property;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@
{
foreach (AsyncConsumerDataProcessor asyncMultiProducerMultiConsumerDataProcessor in dataProcessors)
{
if (!consumerToDrain.TryGetValue(asyncMultiProducerMultiConsumerDataProcessor, out long _))
if (!consumerToDrain.ContainsKey(asyncMultiProducerMultiConsumerDataProcessor))

Check failure on line 168 in src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Release)

src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs#L168

src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs(168,26): error CA1854: (NETCORE_ENGINEERING_TELEMETRY=Build) Prefer a 'TryGetValue' call over a Dictionary indexer access guarded by a 'ContainsKey' check to avoid double lookup (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1854)

Check failure on line 168 in src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Release)

src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs#L168

src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs(168,26): error CA1854: (NETCORE_ENGINEERING_TELEMETRY=Build) Prefer a 'TryGetValue' call over a Dictionary indexer access guarded by a 'ContainsKey' check to avoid double lookup (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1854)

Check failure on line 168 in src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Release)

src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs#L168

src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs(168,26): error CA1854: (NETCORE_ENGINEERING_TELEMETRY=Build) Prefer a 'TryGetValue' call over a Dictionary indexer access guarded by a 'ContainsKey' check to avoid double lookup (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1854)

Check failure on line 168 in src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Release)

src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs#L168

src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs(168,26): error CA1854: (NETCORE_ENGINEERING_TELEMETRY=Build) Prefer a 'TryGetValue' call over a Dictionary indexer access guarded by a 'ContainsKey' check to avoid double lookup (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1854)

Check failure on line 168 in src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Release)

src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs#L168

src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs(168,26): error CA1854: (NETCORE_ENGINEERING_TELEMETRY=Build) Prefer a 'TryGetValue' call over a Dictionary indexer access guarded by a 'ContainsKey' check to avoid double lookup (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1854)

Check failure on line 168 in src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Release)

src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs#L168

src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs(168,26): error CA1854: (NETCORE_ENGINEERING_TELEMETRY=Build) Prefer a 'TryGetValue' call over a Dictionary indexer access guarded by a 'ContainsKey' check to avoid double lookup (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1854)

Check failure on line 168 in src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Release)

src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs#L168

src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs(168,26): error CA1854: (NETCORE_ENGINEERING_TELEMETRY=Build) Prefer a 'TryGetValue' call over a Dictionary indexer access guarded by a 'ContainsKey' check to avoid double lookup (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1854)

Check failure on line 168 in src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Release)

src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs#L168

src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs(168,26): error CA1854: (NETCORE_ENGINEERING_TELEMETRY=Build) Prefer a 'TryGetValue' call over a Dictionary indexer access guarded by a 'ContainsKey' check to avoid double lookup (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1854)

Check failure on line 168 in src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Release)

src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs#L168

src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs(168,26): error CA1854: (NETCORE_ENGINEERING_TELEMETRY=Build) Prefer a 'TryGetValue' call over a Dictionary indexer access guarded by a 'ContainsKey' check to avoid double lookup (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1854)

Check failure on line 168 in src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Release)

src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs#L168

src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs(168,26): error CA1854: (NETCORE_ENGINEERING_TELEMETRY=Build) Prefer a 'TryGetValue' call over a Dictionary indexer access guarded by a 'ContainsKey' check to avoid double lookup (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1854)

Check failure on line 168 in src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Debug)

src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs#L168

src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs(168,26): error CA1854: (NETCORE_ENGINEERING_TELEMETRY=Build) Prefer a 'TryGetValue' call over a Dictionary indexer access guarded by a 'ContainsKey' check to avoid double lookup (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1854)

Check failure on line 168 in src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Debug)

src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs#L168

src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs(168,26): error CA1854: (NETCORE_ENGINEERING_TELEMETRY=Build) Prefer a 'TryGetValue' call over a Dictionary indexer access guarded by a 'ContainsKey' check to avoid double lookup (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1854)

Check failure on line 168 in src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Debug)

src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs#L168

src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs(168,26): error CA1854: (NETCORE_ENGINEERING_TELEMETRY=Build) Prefer a 'TryGetValue' call over a Dictionary indexer access guarded by a 'ContainsKey' check to avoid double lookup (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1854)

Check failure on line 168 in src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Debug)

src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs#L168

src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs(168,26): error CA1854: (NETCORE_ENGINEERING_TELEMETRY=Build) Prefer a 'TryGetValue' call over a Dictionary indexer access guarded by a 'ContainsKey' check to avoid double lookup (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1854)

Check failure on line 168 in src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Debug)

src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs#L168

src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs(168,26): error CA1854: (NETCORE_ENGINEERING_TELEMETRY=Build) Prefer a 'TryGetValue' call over a Dictionary indexer access guarded by a 'ContainsKey' check to avoid double lookup (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1854)

Check failure on line 168 in src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Debug)

src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs#L168

src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs(168,26): error CA1854: (NETCORE_ENGINEERING_TELEMETRY=Build) Prefer a 'TryGetValue' call over a Dictionary indexer access guarded by a 'ContainsKey' check to avoid double lookup (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1854)

Check failure on line 168 in src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Debug)

src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs#L168

src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs(168,26): error CA1854: (NETCORE_ENGINEERING_TELEMETRY=Build) Prefer a 'TryGetValue' call over a Dictionary indexer access guarded by a 'ContainsKey' check to avoid double lookup (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1854)

Check failure on line 168 in src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Debug)

src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs#L168

src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs(168,26): error CA1854: (NETCORE_ENGINEERING_TELEMETRY=Build) Prefer a 'TryGetValue' call over a Dictionary indexer access guarded by a 'ContainsKey' check to avoid double lookup (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1854)

Check failure on line 168 in src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx

src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs#L168

src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs(168,26): error CA1854: (NETCORE_ENGINEERING_TELEMETRY=Build) Prefer a 'TryGetValue' call over a Dictionary indexer access guarded by a 'ContainsKey' check to avoid double lookup (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1854)

Check failure on line 168 in src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx

src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs#L168

src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs(168,26): error CA1854: (NETCORE_ENGINEERING_TELEMETRY=Build) Prefer a 'TryGetValue' call over a Dictionary indexer access guarded by a 'ContainsKey' check to avoid double lookup (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1854)

Check failure on line 168 in src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx

src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs#L168

src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs(168,26): error CA1854: (NETCORE_ENGINEERING_TELEMETRY=Build) Prefer a 'TryGetValue' call over a Dictionary indexer access guarded by a 'ContainsKey' check to avoid double lookup (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1854)

Check failure on line 168 in src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx

src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs#L168

src/Platform/Microsoft.Testing.Platform/Messages/AsynchronousMessageBus.cs(168,26): error CA1854: (NETCORE_ENGINEERING_TELEMETRY=Build) Prefer a 'TryGetValue' call over a Dictionary indexer access guarded by a 'ContainsKey' check to avoid double lookup (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1854)
{
consumerToDrain.Add(asyncMultiProducerMultiConsumerDataProcessor, 0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public CustomTaskItem Add(string key, string value)

public void CopyMetadataTo(ITaskItem destinationItem) => throw new NotImplementedException();

public string? GetMetadata(string metadataName) => _keyValuePairs.TryGetValue(metadataName, out string? value) ? value : null;
public string? GetMetadata(string metadataName) => _keyValuePairs.GetValueOrDefault(metadataName);

public void RemoveMetadata(string metadataName) => throw new NotImplementedException();

Expand Down
Loading