Skip to content

CodeQL 8: perf: replace ContainsKey+indexer with TryGetValue#196

Open
rlorenzo wants to merge 1 commit into
mainfrom
codeql/8-trygetvalue
Open

CodeQL 8: perf: replace ContainsKey+indexer with TryGetValue#196
rlorenzo wants to merge 1 commit into
mainfrom
codeql/8-trygetvalue

Conversation

@rlorenzo
Copy link
Copy Markdown
Contributor

@rlorenzo rlorenzo commented May 13, 2026

Summary

Closes ~14 of 18 cs/inefficient-containskey alerts by replacing ContainsKey + indexer (two dictionary lookups) with TryGetValue (one lookup) in C# and dropping the redundant ContainsKey guard in Razor views.

Changes

C# (3 files, 4 sites)

  • RotationsController.cs:498-511 - three personData.ContainsKey(i.MothraId) ? personData[i.MothraId].XXX : "Unknown" ternaries collapsed into one TryGetValue(out var p) plus three p?.XXX ?? "Unknown" expressions.
  • RAPSAuditService.cs:153 - ContainsKey(key) + indexerTryGetValue(key, out var moreRecentActions).
  • VMACSExport.cs:303, 345 - same pattern in two helpers.

Razor views (4 files, 10 sites)

Pattern ViewData.ContainsKey("X") && (bool)(ViewData["X"] ?? false) collapsed to (bool)(ViewData["X"] ?? false) - ViewData[] already returns null on miss, so the ContainsKey guard is redundant once the ?? false is present. Touches:

  • Areas/RAPS/Views/Members/List.cshtml (×4)
  • Areas/RAPS/Views/Members/Roles.cshtml (×1)
  • Areas/RAPS/Views/Roles/Members.cshtml (×1)
  • Areas/RAPS/Views/Roles/Templates.cshtml (×4)

Not addressed: 1 test/Students/EmergencyContactControllerTests.cs:319 site (test infrastructure) and 3 likely already covered or out-of-scope.

Context

Eighth in the CodeQL N: cleanup series.

Test plan

  • npm run test:backend - 1946 tests passing
  • Pre-commit lint+test+verify all passed
  • CodeQL workflow on this PR shows ~14 of the 18 ContainsKey alerts closed

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 13, 2026

Important

Review skipped

Auto reviews are limited based on label configuration.

🏷️ Required labels (at least one) (1)
  • review-ready

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: d88eed65-471d-42f2-bcab-749b3889fe46

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codeql/8-trygetvalue

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov-commenter
Copy link
Copy Markdown

Bundle Report

Bundle size has no change ✅

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 13, 2026

Codecov Report

❌ Patch coverage is 0% with 25 lines in your changes missing coverage. Please review.
✅ Project coverage is 42.96%. Comparing base (38de1ad) to head (3ec2656).

Files with missing lines Patch % Lines
...inicalScheduler/Controllers/RotationsController.cs 0.00% 11 Missing ⚠️
web/Areas/RAPS/Views/Members/List.cshtml 0.00% 4 Missing ⚠️
web/Areas/RAPS/Views/Roles/Templates.cshtml 0.00% 4 Missing ⚠️
web/Areas/RAPS/Services/VMACSExport.cs 0.00% 3 Missing ⚠️
web/Areas/RAPS/Services/RAPSAuditService.cs 0.00% 1 Missing ⚠️
web/Areas/RAPS/Views/Members/Roles.cshtml 0.00% 1 Missing ⚠️
web/Areas/RAPS/Views/Roles/Members.cshtml 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #196      +/-   ##
==========================================
- Coverage   42.96%   42.96%   -0.01%     
==========================================
  Files         877      877              
  Lines       51468    51469       +1     
  Branches     4802     4802              
==========================================
  Hits        22113    22113              
- Misses      28831    28832       +1     
  Partials      524      524              
Flag Coverage Δ
backend 43.04% <0.00%> (-0.01%) ⬇️
frontend 41.34% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants