Skip to content

Commit

Permalink
Use TryGetValue instead of ContainsKey + index access to avoid double…
Browse files Browse the repository at this point in the history
… lookup (#193)

This PR fixes an issue that is currently causing CI to fail in all pull
requests:
`Notice: "[CA1854] Prefer a 'TryGetValue' call over a Dictionary indexer
access guarded by a 'ContainsKey' check to avoid double lookup" on
/home/runner/work/Octobot/Octobot/src/Services/Update/ScheduledEventUpdateService.cs(107,4168)`
The issue is resolved by following the advice mentioned in the notice.
  • Loading branch information
Octol1ttle authored Nov 22, 2023
1 parent c031b66 commit e65c7a4
Showing 1 changed file with 1 addition and 2 deletions.
3 changes: 1 addition & 2 deletions src/Services/Update/ScheduledEventUpdateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,13 @@ private static void SyncScheduledEvents(GuildData data, IEnumerable<IGuildSchedu
{
foreach (var @event in events)
{
if (!data.ScheduledEvents.ContainsKey(@event.ID.Value))
if (!data.ScheduledEvents.TryGetValue(@event.ID.Value, out var eventData))
{
data.ScheduledEvents.Add(@event.ID.Value,
new ScheduledEventData(@event.ID.Value, @event.Name, @event.ScheduledStartTime, @event.Status));
continue;
}

var eventData = data.ScheduledEvents[@event.ID.Value];
eventData.Name = @event.Name;
eventData.ScheduledStartTime = @event.ScheduledStartTime;
if (!eventData.ScheduleOnStatusUpdated)
Expand Down

0 comments on commit e65c7a4

Please sign in to comment.