CodeQL 7: refactor: tighten foreach-to-LINQ where it improves the call site#195
CodeQL 7: refactor: tighten foreach-to-LINQ where it improves the call site#195rlorenzo wants to merge 1 commit into
Conversation
Bundle ReportBundle size has no change ✅ |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #195 +/- ##
=======================================
Coverage 43.02% 43.03%
=======================================
Files 881 881
Lines 51437 51421 -16
Branches 4812 4804 -8
=======================================
- Hits 22131 22129 -2
+ Misses 28780 28766 -14
Partials 526 526
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (5)
📝 WalkthroughWalkthroughFive methods across role management, course/session transformations, and assessment processing were rewritten from imperative foreach/if patterns to LINQ expressions (Select, GroupBy, Where, Any) with no public API or behavioral changes. ChangesRefactoring to LINQ Expressions
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
|
@coderabbitai full review |
✅ Actions performedFull review triggered. |
|
@coderabbitai full review |
✅ Actions performedFull review triggered. |
There was a problem hiding this comment.
Pull request overview
This PR continues the CodeQL cleanup series by replacing selected C# foreach patterns with LINQ where the resulting code keeps behavior equivalent and clarifies intent.
Changes:
- Replaced filtering loops with
Any,Where, andSelectat targeted call sites. - Converted course/session grouping construction to LINQ projections.
- Cleaned tab indentation in
RoleViews.cs.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
web/Classes/UserHelper.cs |
Simplifies role-claim lookup with Any. |
web/Areas/RAPS/Services/RoleViews.cs |
Hoists delete filtering into Where and fixes indentation. |
web/Areas/RAPS/Controllers/RoleTemplatesController.cs |
Filters preview roles before applying missing roles. |
web/Areas/CTS/Services/CrestCourseService.cs |
Converts grouped course/session object creation to LINQ projections. |
web/Areas/CTS/Controllers/AssessmentController.cs |
Converts assessment DTO mapping loop to Select. |
533a616 to
071a111
Compare
|
@bsedwards Merged to TEST. |
Summary
Closes 6 of 34
cs/linq/missed-*alerts where theforeach-to-LINQ conversion makes the intent clearer:UserHelper.IsInRole- claim-search loop →claims.Any(c => c.Type == ClaimTypes.Role && c.Value == roleName)(also collapses the surrounding null guard).CrestCourseService.CourseSessionOfferingsToCourses/…Sessions- staging-list + foreach-Add pattern →csos.GroupBy(...).Select(g => new Course/Session(...)).ToList().AssessmentController.GetAssessmentsForStudent- build-and-mutate loop →assessmentsList.Select(a => { … }).ToList().RoleTemplatesController.Apply-foreach (... in preview.Roles) { if (!UserHasRole) … }→foreach (... in preview.Roles.Where(r => !r.UserHasRole)).RoleViewsdelete loop - compoundiffilter hoisted into theforeach … in roleMembers.Where(...).Also fixed two stray tab-indented lines in
RoleViews.csthatdotnet formatflagged.Why 28 alerts are not addressed
The remaining 28
cs/linq/missed-selectalerts all live in PDF/Excel cell-generation loops in the Effort report services -TeachingActivityService,MeritReportService,SchoolSummaryService,DeptSummaryService,MeritMultiYearService,EvaluationReportService,MeritSummaryService. Pattern is:The
var val = …is what CodeQL identifies as a "mapping", but the surrounding body isQuestPDF.table.Cell()…/ClosedXML.ws.Cell()…side effects, not collection-building. Forcing aSelecthere would just add a tuple-deconstructionforeachover a.Select(t => (Type: t, Val: …))with the same side-effectful body - more code, same behavior, lower readability. These are dismissible as wontfix-by-design on the CodeQL dashboard.Context
Seventh in the
CodeQL N:cleanup series (after #189, #190, #191, #192, #193, #194).Test plan
npm run test:backend- 1946 tests passingnpm run verify:build- clean (0 errors)