Skip to content

Replace 'as' casts with explicit casts#4019

Open
SimonCropp wants to merge 2 commits intodotnet:mainfrom
SimonCropp:Replace-'as'-casts-with-explicit-casts
Open

Replace 'as' casts with explicit casts#4019
SimonCropp wants to merge 2 commits intodotnet:mainfrom
SimonCropp:Replace-'as'-casts-with-explicit-casts

Conversation

@SimonCropp
Copy link
Contributor

Replace usages of 'as' with explicit casts across multiple files to ensure correct typing and avoid silent nulls. Updates include DataRow casts in SqlBulkCopy, string casts for Json parsing in SqlDataReader and SqlParameter, object ref cast in SqlDependency, and test parameter casts in AADAuthenticationTests and PoolBlockPeriodTest. This makes failures deterministic (throwing InvalidCastException) and clarifies intent when a null result from 'as' would be unsafe.

Replace usages of 'as' with explicit casts across multiple files to ensure correct typing and avoid silent nulls. Updates include DataRow casts in SqlBulkCopy, string casts for Json parsing in SqlDataReader and SqlParameter, object ref cast in SqlDependency, and test parameter casts in AADAuthenticationTests and PoolBlockPeriodTest. This makes failures deterministic (throwing InvalidCastException) and clarifies intent when a null result from 'as' would be unsafe.
Copilot AI review requested due to automatic review settings March 7, 2026 12:50
@SimonCropp SimonCropp requested a review from a team as a code owner March 7, 2026 12:50
@github-project-automation github-project-automation bot moved this to To triage in SqlClient Board Mar 7, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR replaces several as casts with explicit casts across SqlClient source and test code to avoid silent null results and make type assumptions fail deterministically.

Changes:

  • Updated test parameter handling to use explicit casts for connection pool and AAD authentication tests.
  • Updated SqlBulkCopy DataRow indexing logic to use explicit DataRow casts.
  • Updated JSON/vector-related parsing and remoting deserialization call sites to use explicit casts.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ConnectionPoolTest/PoolBlockPeriodTest.cs Uses explicit string casts for xUnit parameter arrays.
src/Microsoft.Data.SqlClient/tests/FunctionalTests/AADAuthenticationTests.cs Uses explicit string casts when constructing credentials and setting builder keys.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlParameter.cs Uses explicit string/interface casts for vector JSON coercion.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDependency.cs Uses explicit cast for deserialized SqlClientObjRef before unmarshalling.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDataReader.cs Uses explicit string cast when parsing JsonDocument from buffer value.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlBulkCopy.cs Uses explicit DataRow cast for determining row number from enumerator current item.

…ataReader.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 7, 2026 12:54
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated no new comments.

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

Labels

None yet

Projects

Status: To triage

Development

Successfully merging this pull request may close these issues.

2 participants