From 2186c207f7251caa54c9cc3b0800a19a78663e29 Mon Sep 17 00:00:00 2001 From: Rex Lorenzo Date: Tue, 16 Jun 2026 11:27:56 -0700 Subject: [PATCH] chore(codeql): readonly fields and TryGetValue cleanups Mark single-assignment static fields readonly (VMACSService.sharedClient, RolesTests SQLite connection/options) and replace ContainsKey + indexer double lookups with TryGetValue (RotationsController recent-clinicians map, EmergencyContact test). Resolves cs/missed-readonly-modifier and cs/inefficient-containskey. --- test/RAPS/RolesTests.cs | 4 ++-- test/Students/EmergencyContactControllerTests.cs | 4 ++-- .../ClinicalScheduler/Controllers/RotationsController.cs | 4 ++-- web/Areas/Directory/Services/VMACSService.cs | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/test/RAPS/RolesTests.cs b/test/RAPS/RolesTests.cs index c3d57d607..ea6d64afb 100644 --- a/test/RAPS/RolesTests.cs +++ b/test/RAPS/RolesTests.cs @@ -19,8 +19,8 @@ public class RolesTests /// /// Use the _sqlLiteConnection (see RejectAddRole_WhenDuplicateRoleId() to validate unique contraints or to do create, edit, delete actions /// - static SqliteConnection _sqlLiteConnection = new SqliteConnection("Filename=:memory:"); - static DbContextOptions _sqlLiteContextOptions = new DbContextOptionsBuilder() + static readonly SqliteConnection _sqlLiteConnection = new SqliteConnection("Filename=:memory:"); + static readonly DbContextOptions _sqlLiteContextOptions = new DbContextOptionsBuilder() .UseSqlite(_sqlLiteConnection) .Options; diff --git a/test/Students/EmergencyContactControllerTests.cs b/test/Students/EmergencyContactControllerTests.cs index ed48b4943..2efc6b194 100644 --- a/test/Students/EmergencyContactControllerTests.cs +++ b/test/Students/EmergencyContactControllerTests.cs @@ -318,8 +318,8 @@ public async Task UpdateStudentContact_ArgumentException_ReturnsValidationProble // but in unit tests without an HttpContext it stays null — assert the body shape. var objectResult = Assert.IsType(result.Result); var problem = Assert.IsType(objectResult.Value); - Assert.True(problem.Errors.ContainsKey("PhoneValidation")); - Assert.Contains("Invalid phone number: 12345", problem.Errors["PhoneValidation"]); + Assert.True(problem.Errors.TryGetValue("PhoneValidation", out var phoneErrors)); + Assert.Contains("Invalid phone number: 12345", phoneErrors); } [Fact] diff --git a/web/Areas/ClinicalScheduler/Controllers/RotationsController.cs b/web/Areas/ClinicalScheduler/Controllers/RotationsController.cs index e0bf5c64b..fcf16888b 100644 --- a/web/Areas/ClinicalScheduler/Controllers/RotationsController.cs +++ b/web/Areas/ClinicalScheduler/Controllers/RotationsController.cs @@ -562,8 +562,8 @@ private List BuildRecentCliniciansList(IEnumerable mothraIds, Di .Select(mothraId => new { mothraId, - fullName = personData.ContainsKey(mothraId) - ? personData[mothraId].PersonDisplayFullName + fullName = personData.TryGetValue(mothraId, out var person) + ? person.PersonDisplayFullName : $"Clinician {mothraId}" }) .OrderBy(c => c.fullName) diff --git a/web/Areas/Directory/Services/VMACSService.cs b/web/Areas/Directory/Services/VMACSService.cs index bc71f751a..c1ff1c4f1 100644 --- a/web/Areas/Directory/Services/VMACSService.cs +++ b/web/Areas/Directory/Services/VMACSService.cs @@ -7,7 +7,7 @@ namespace Viper.Areas.Directory.Services { public class VMACSService { - private static HttpClient sharedClient = new() + private static readonly HttpClient sharedClient = new() { BaseAddress = new Uri("https://vmacs-vmth.vetmed.ucdavis.edu"), };