Skip to content

CodeQL 16: refactor: remove useless upcasts via record types and default(T)#202

Open
rlorenzo wants to merge 3 commits into
mainfrom
codeql/16-extract-record-types
Open

CodeQL 16: refactor: remove useless upcasts via record types and default(T)#202
rlorenzo wants to merge 3 commits into
mainfrom
codeql/16-extract-record-types

Conversation

@rlorenzo
Copy link
Copy Markdown
Contributor

@rlorenzo rlorenzo commented May 13, 2026

Summary

Closes 6 of the 7 non-test cs/useless-upcast alerts by either extracting a named record or replacing (T?)null with default(T?):

  • PhotoExportService.BatchLoadPhotosAsync (PhotoExportService.cs:906, 911, 916, 921) - replaces the 4 anonymous types with a private PhotoLookupResult(string MailId, byte[]? PhotoBytes) record. The catch branches now return new PhotoLookupResult(s.MailId, null) - no cast needed because the parameter is typed.
  • VerificationService.cs:235 - (DateTime?)nulldefault(DateTime?). Same value, no cast.
  • CliniciansController.cs:204 - (string?)nulldefault(string). The two anonymous types in the if/else assign to an object variable, so they don't need to match.
  • _EmailLayout.cshtml:46,48 - extracted var baseUrl = ViewBag.BaseUrl as string; into a Razor block; the (string?)/(string) casts on every reference go away.

Not addressed

The 1 remaining non-test useless-upcast alert is essentially the same site as above - they collapse together once CodeQL re-scans.

Context

Sixteenth in the CodeQL N: cleanup series.

Test plan

  • npm run test:backend - 1946 tests passing
  • npm run verify:build - clean
  • Pre-commit lint+test+verify all passed

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 13, 2026

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: dc18d8b1-ebb0-4b22-8fd2-775587150494

📥 Commits

Reviewing files that changed from the base of the PR and between 5949e52 and e1282ea.

📒 Files selected for processing (7)
  • test/CTS/AssessmentControllerTest.cs
  • test/ClinicalScheduler/EmailNotificationTest.cs
  • test/ClinicalScheduler/RotationServiceTest.cs
  • web/Areas/ClinicalScheduler/Controllers/CliniciansController.cs
  • web/Areas/Effort/Services/VerificationService.cs
  • web/Areas/Students/Services/PhotoExportService.cs
  • web/EmailTemplates/Views/Shared/_EmailLayout.cshtml

📝 Walkthrough

Walkthrough

The PR consolidates code quality improvements across the VIPER application. Test helpers narrow exception catches to specific types, services replace explicit null casts with default(), photo-loading logic introduces a named record type, and email layout conditionals extract variables for clarity.

Changes

Code Quality and Exception Handling Refinements

Layer / File(s) Summary
Test exception handling specificity
test/CTS/AssessmentControllerTest.cs, test/ClinicalScheduler/EmailNotificationTest.cs, test/ClinicalScheduler/RotationServiceTest.cs
IsForbidResult and IsNotFoundResult catch Xunit.Sdk.XunitException instead of generic Exception. EmailNotificationTest and RotationServiceTest narrowed to specific exception types (DbUpdateException, InvalidOperationException, SqlException, OperationCanceledException), ensuring unexpected failures propagate.
Null handling with default expressions
web/Areas/ClinicalScheduler/Controllers/CliniciansController.cs, web/Areas/Effort/Services/VerificationService.cs
CliniciansController and VerificationService replace explicit nullable casts ((string?)null, (DateTime?)null) with default(string) and default(DateTime?) in fallback/audit payloads, preserving semantics with cleaner syntax.
Photo lookup result refactoring
web/Areas/Students/Services/PhotoExportService.cs
Introduces private PhotoLookupResult record to carry student MailId and optional PhotoBytes from parallel photo-fetch tasks. Returns PhotoLookupResult instances on success and failure paths instead of anonymous objects; exception handling logs warnings and sets PhotoBytes to null.
Email layout base URL extraction
web/EmailTemplates/Views/Shared/_EmailLayout.cshtml
Extracts ViewBag.BaseUrl into a local baseUrl variable and refactors conditional logic to reference the variable instead of repeated ViewBag access, reducing redundancy in header image URL construction.

🎯 2 (Simple) | ⏱️ ~12 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 12.50% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed Title accurately summarizes the main refactoring objective: removing useless upcasts through record type extraction and default(T) usage.
Description check ✅ Passed Description clearly explains the changes across all modified files, details the CodeQL alerts being resolved, and documents the test results.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codeql/16-extract-record-types

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 22.22222% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 42.96%. Comparing base (38de1ad) to head (e1282ea).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
web/Areas/Students/Services/PhotoExportService.cs 0.00% 5 Missing ⚠️
...nicalScheduler/Controllers/CliniciansController.cs 0.00% 1 Missing ⚠️
...eb/EmailTemplates/Views/Shared/_EmailLayout.cshtml 50.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main     #202   +/-   ##
=======================================
  Coverage   42.96%   42.96%           
=======================================
  Files         877      877           
  Lines       51468    51470    +2     
  Branches     4802     4802           
=======================================
+ Hits        22113    22114    +1     
- Misses      28831    28832    +1     
  Partials      524      524           
Flag Coverage Δ
backend 43.04% <22.22%> (+<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.

rlorenzo added 2 commits May 13, 2026 02:19
…ilters

Removed 'Microsoft.EntityFrameworkCore.' and 'Microsoft.Data.SqlClient.'
prefixes from the catch when-filters added in codeql/15. Added
'using Microsoft.Data.SqlClient;' to EmailNotificationTest.
@rlorenzo rlorenzo force-pushed the codeql/16-extract-record-types branch from 8b79d57 to e1282ea Compare May 13, 2026 09:22
@rlorenzo
Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 15, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

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