SonarSource / sonar-dotnet

Code analyzer for C# and VB.NET projects
https://redirect.sonarsource.com/plugins/csharp.html
GNU Lesser General Public License v3.0
788 stars 227 forks source link

Fix S2857 FN: No issue is raised if SQL related namespaces are imported through global using directives #5956

Open mary-georgiou-sonarsource opened 2 years ago

mary-georgiou-sonarsource commented 2 years ago

Description

No issue is raised if SQL related namespaces are imported through global using directives

Repro steps

// global using System.Data.SqlClient is declared in another file.
class Foo
{
    private string field = "SELECT *" + // FN
        "FROM TABLE" +
        "WHERE X = 1;";
}

Expected behavior

Issue is raised.

Actual behavior

No issue is raised.

Related information

How?

Global using retrieval will be available from MSBuild version 17.3 onwards through this API, exposed via semanticModel.GetImportScopes.

Since the semantic model will be called, we have to consider the potential performance regression but running tests.

mary-georgiou-sonarsource commented 2 years ago

~It might be possible to retrieve from Roslyn what usings are in scope at the syntax node that is being analyzed.~

~Otherwise, there might be a big performance impact~

EDIT: Global using retrieval will be available from MSBuild version 17.3 onwards through this API, exposed via semanticModel.GetImportScopes.

Since the semantic model will be called, we have to consider the potential performance regression but running tests.