sonar-dotnet icon indicating copy to clipboard operation
sonar-dotnet copied to clipboard

S1128 Improve performance and add to SonarWay: UnnecessaryUsings

Open andrei-epure-sonarsource opened this issue 3 years ago • 4 comments

S1128 (UnnecessaryUsings) is a code smell which doesn't bring a lot of functional value, so we don't want it to have a big performance impact on the msbuild time.

We should re-think how we use the Semantic Model inside the rule, in order to improve the performance.

From this investigation, the rule takes 38 seconds to run - 4%.

38.864 4 SonarAnalyzer.Rules.CSharp.UnnecessaryUsings

Improving performance is a good thing! Personally I value this rule highly, as it potentially removes a lot of code, which makes code easier to read. Visual Studio also has tooling for this, I'm not sure if sonar can build on top of that (might help performance wise).

Corniel avatar Nov 27 '20 14:11 Corniel

I agree that performance is important, @Corniel. As our analyzers are used on CI systems, as well as local builds outside the IDE - we cannot rely on Visual Studio. It would be interesting to see their implementation, though.

This rule needs complete redesign. New idea:

  • Walk the syntax tree
  • Collect imported symbol from using statement.
  • Track all imported members from collected symbol (like FileStream from using System.IO;)
  • Do not ask for symbol of tracked identifiers

Leaving this here as an FYI: Global using retrieval will be available from MSBuild version 17.3 onwards through this API, exposed via semanticModel.GetImportScopes.