Replace 'as' casts with explicit casts#4019
Open
SimonCropp wants to merge 2 commits intodotnet:mainfrom
Open
Conversation
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.
Contributor
There was a problem hiding this comment.
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
SqlBulkCopyDataRow indexing logic to use explicitDataRowcasts. - 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. |
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDataReader.cs
Outdated
Show resolved
Hide resolved
…ataReader.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.