Skip to content

Replace redundant int.Parse calls with safer parsing in GuitarTabController#1145

Merged
samsmithnz merged 2 commits intomainfrom
copilot/fix-redundant-int-parse
Dec 26, 2025
Merged

Replace redundant int.Parse calls with safer parsing in GuitarTabController#1145
samsmithnz merged 2 commits intomainfrom
copilot/fix-redundant-int-parse

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Dec 26, 2025

Removes redundant int.Parse after validation and replaces unsafe parsing with int.TryParse to prevent exceptions from invalid input.

Changes

  • SaveAlbum: Use already-parsed year variable instead of re-parsing txtYear
  • SaveTab: Replace int.Parse with int.TryParse for txtOrder, cboRating, and cboTuning, defaulting to 0 on invalid input (consistent with AddNewTrack)
  • Remove commented-out parameter from SaveAlbum signature
// Before: redundant parse after validation
if (!int.TryParse(txtYear, out int year)) { ... }
AlbumYear = int.Parse(txtYear)  // throws if validation logic diverges

// After: use validated variable
AlbumYear = year

// Before: unsafe parsing
TabOrder = int.Parse(txtOrder)  // throws on invalid input

// After: safe parsing with fallback
int tabOrder = 0;
int.TryParse(txtOrder, out tabOrder);
Original prompt
Please apply the following diffs and create a pull request.
Once the PR is ready, give it a title based on the messages of the fixes being applied.

[{"message":"Using int.Parse after already validating with int.TryParse is redundant and could throw an exception if the validation logic is inconsistent. Use the parsed 'year' variable instead.","fixFiles":[{"filePath":"src/SamSmithNZ.Web/Controllers/GuitarTabController.cs","diff":"diff --git a/src/SamSmithNZ.Web/Controllers/GuitarTabController.cs b/src/SamSmithNZ.Web/Controllers/GuitarTabController.cs\n--- a/src/SamSmithNZ.Web/Controllers/GuitarTabController.cs\n+++ b/src/SamSmithNZ.Web/Controllers/GuitarTabController.cs\n@@ -116,7 +116,7 @@\n                 AlbumCode = albumCode,\n                 ArtistName = txtArtist,\n                 AlbumName = txtAlbumName,\n-                AlbumYear = int.Parse(txtYear),\n+                AlbumYear = year,\n                 IsBassTab = chkIsBassTab,\n                 IncludeInIndex = chkIncludeInIndex,\n                 IncludeOnWebsite = chkIncludeOnWebsite,\n"}]},{"message":"Using int.Parse without validation could throw an exception if txtOrder is not a valid integer. Consider using int.TryParse with appropriate error handling.","fixFiles":[{"filePath":"src/SamSmithNZ.Web/Controllers/GuitarTabController.cs","diff":"diff --git a/src/SamSmithNZ.Web/Controllers/GuitarTabController.cs b/src/SamSmithNZ.Web/Controllers/GuitarTabController.cs\n--- a/src/SamSmithNZ.Web/Controllers/GuitarTabController.cs\n+++ b/src/SamSmithNZ.Web/Controllers/GuitarTabController.cs\n@@ -165,15 +165,34 @@\n         [HttpPost]\n         public async Task<IActionResult> SaveTab(int tabCode, int albumCode, string txtTabName, string txtTabText, string txtOrder, string cboRating, string cboTuning, bool isAdmin = false)\n         {\n+            // Safely parse numeric inputs to avoid exceptions from invalid data\n+            int tabOrder;\n+            if (!int.TryParse(txtOrder, out tabOrder))\n+            {\n+                tabOrder = 0;\n+            }\n+\n+            int rating;\n+            if (!int.TryParse(cboRating, out rating))\n+            {\n+                rating = 0; // default to no rating\n+            }\n+\n+            int tuningCode;\n+            if (!int.TryParse(cboTuning, out tuningCode))\n+            {\n+                tuningCode = 0; // default to no tuning\n+            }\n+\n             Tab tab = new()\n             {\n                 TabCode = tabCode,\n                 AlbumCode = albumCode,\n                 TabName = txtTabName,\n                 TabText = txtTabText,\n-                TabOrder = int.Parse(txtOrder),\n-                Rating = int.Parse(cboRating),\n-                TuningCode = int.Parse(cboTuning)\n+                TabOrder = tabOrder,\n+                Rating = rating,\n+                TuningCode = tuningCode\n             };\n             await _ServiceApiClient.SaveTab(tab);\n \n"}]},{"message":"Using int.Parse without validation could throw an exception if cboRating is not a valid integer. Consider using int.TryParse with appropriate error handling.","fixFiles":[{"filePath":"src/SamSmithNZ.Web/Controllers/GuitarTabController.cs","diff":"diff --git a/src/SamSmithNZ.Web/Controllers/GuitarTabController.cs b/src/SamSmithNZ.Web/Controllers/GuitarTabController.cs\n--- a/src/SamSmithNZ.Web/Controllers/GuitarTabController.cs\n+++ b/src/SamSmithNZ.Web/Controllers/GuitarTabController.cs\n@@ -165,15 +165,34 @@\n         [HttpPost]\n         public async Task<IActionResult> SaveTab(int tabCode, int albumCode, string txtTabName, string txtTabText, string txtOrder, string cboRating, string cboTuning, bool isAdmin = false)\n         {\n+            // Safely parse numeric inputs to avoid exceptions from invalid user input\n+            int tabOrder;\n+            if (!int.TryParse(txtOrder, out tabOrder))\n+            {\n+                tabOrder = 0;\n+            }\n+\n+            int rating;\n+            if (!int.TryParse(cboRating, out rating))\n+            {\n+                rating = 0; // default: no rating\n+            }\n+\n+            int tuningCode;\n+            if (!int.TryParse(cboTuning, out tuningCode))\n+            {\n+                tuningCode = 0; // default: no tuning\n+            }\n+\n             Tab tab = new()\n             {\n                 TabCode = tabCode,\n                 AlbumCode = albumCode,\n                 TabName = txtTabName,\n                 TabText = txtTabText,\n-                TabOrder = int.Parse(txtOrder),\n-                Rating = int.Parse(cboRating),\n-                TuningCode = int.Parse(cboTuning)\n+                TabOrder = tabOrder,\n+                Rating = rating,\n+                TuningCode = tuningCode\n             };\n             await _ServiceApiClient.SaveTab(tab);\n \n"}]},{"message":"Using int.Parse without validation could throw an exception if cboTuning is not a valid integer. Consider using int.T...

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: samsmithnz <8389039+samsmithnz@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix redundant int.Parse after validation in GuitarTabController Replace redundant int.Parse calls with safer parsing in GuitarTabController Dec 26, 2025
Copilot AI requested a review from samsmithnz December 26, 2025 17:58
@samsmithnz samsmithnz marked this pull request as ready for review December 26, 2025 19:07
@samsmithnz samsmithnz enabled auto-merge December 26, 2025 19:08
@samsmithnz samsmithnz merged commit 09e7fa0 into main Dec 26, 2025
14 checks passed
@samsmithnz samsmithnz deleted the copilot/fix-redundant-int-parse branch December 26, 2025 19:10
@coveralls
Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 20526871317

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 75.734%

Totals Coverage Status
Change from base Build 20520816302: 0.0%
Covered Lines: 2201
Relevant Lines: 2805

💛 - Coveralls

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.

3 participants