Exclude generated code from C # SONAR Analysis

So, in VS2013 we have the option " Suppress generated code results (only for managed) "

Do we have a similar option when analyzing through SONAR ?

The code below reports abuses when the above parameter is not set, but I do not see any option in SONAR to use the above option. I tried the parameter Sonar.dotnet.excludeGeneratedCode = true , but it seems to have no meaning.

  public IEnumerable<string> YieldTest() { foreach(var num in Enumerable.Range(100, 100)) yield return string.Format("{0}", num); } 
+5
source share
2 answers

The MSBuild SonarQube Runner (at least in versions 1.0 and 1.0.1) always causes the Suppress Results from Generated Code (Managed Only) flag to be turned on when FxCop is run during build, See SonarQube.Integration.targets # L342

You can verify this behavior in your build logs by looking at the command launched during the RunCodeAnalysis: phase:

 RunCodeAnalysis: Running Code Analysis... C:\Program Files (x86)\Microsoft Visual Studio 14.0\Team Tools\Static Analysis Tools\FxCop\FxCopCmd.exe /outputCulture:1033 /out:"bin\Debug\ConsoleApplication1.exe.CodeAnalysisLog.xml" /file:"bin\Debug\ConsoleApplication1.exe" /ruleSet:"=C:\Users\dinesh\Desktop\tmp\ConsoleApplication1\.sonarqube\conf\\SonarQubeFxCop-cs.ruleset" [... references ...] /rulesetdirectory:"C:\Program Files (x86)\Microsoft Visual Studio 14.0\Team Tools\Static Analysis Tools\\Rule Sets" /rule:"-C:\Program Files (x86)\Microsoft Visual Studio 14.0\Team Tools\Static Analysis Tools\FxCop\\Rules" /searchgac /ignoreinvalidtargets /forceoutput /successfile **/ignoregeneratedcode** /saveMessagesToReport:Active /timeout:120 /reportMissingIndirectAssemblies 

You should see that / ignoregeneratedcode is passed to FxCopCmd.exe.

Now this flag will be suppressed only by FxCop rules. The StyleCop and ReSharper rules, for example, will not understand this flag and will report this method.

FYI, it seems that FxCop excludes this method due to the presence of the yield : the C # compiler generates quite complex IL code in the assembly for this statement. FxCop parses assemblies (rather than source code), so it must treat the yield as generated code.

So, in SonarQube you should not see a problem for CA1305 , regardless of whether you checked "Suppress generated code results (only managed)" in your project.

+2
source

I run this set of properties and exclude the generated code.

 sonar.exclusions=**/*.Tests/**,**/*Language.Designer.cs,**/*.generated.cs 

If we take a closer look, the exception contains the following parts:

  • / *. Tests / ==> exclude all code found in the folder ending with .Tests (recursive)
  • ** / * Language.Designer.cs ==> exclude all resource identifier files
  • ** / *. generated.cs ==> exclude all files ending with .generated.cs

If you use other generated files (for example, winforms designer files), you will need to ask your own masks.

0
source

All Articles