Skip to content

LGTM.com - false positive in cs/empty-collection for SqlConnectionStringBuilder #4199

@bgianfo

Description

@bgianfo

Description of the false positive

We have the following code:

/// <summary>
/// Creates a connection to database
/// </summary>
private static SqlConnection CreateConnectionToPublisherDB(Parameters parameters)
{
    SqlConnectionStringBuilder connectionString = new SqlConnectionStringBuilder();
    connectionString.DataSource = parameters.DatabaseServerName;
    connectionString.InitialCatalog = parameters.DatabaseName;
    SqlCredential credential = new SqlCredential(
         parameters.DatabaseUserName,
         parameters.DatabasePassword);

    SqlConnection connection = new SqlConnection(connectionString.ConnectionString, credential);

    connection.Open();
    parameters.DatabasePassword.Dispose();
    return connection;
}

The cs/empty-collection rule flags connectionString with the following error.

The contents of this container are never initialized.

SqlConnectionStringBuilder does inherit from DbConnectionStringBuilder, which implements ICollection, and other collection interfaces, however these types specifically expose higher order properties like DataSource or DatabaseName which set the collection members internally. This warning is wrong for the case of code like SqlConnectionBuilder.

URL to the alert on the project page on LGTM.com
This issue was raised on a private LGTM instance unfortunately.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions