Start FreeLog in
SonarCloud | Administering your SonarCloud | Setting project configurations at organization level | Adjusting analysis scope | Using advanced exclusion features

Using advanced exclusion features

On this page

This feature is only available in the Enterprise plan.

In very specific situations, you may have to define at the organization level the exclusion of code from the analysis: 

  • File exclusion based on the file content.
  • Exclusion of blocks within files.
  • Exclusion of specific files from specific coding rules.

Such an analysis scope adjustment applies to all projects in the organization. However, it can be overridden at the project level in the UI or through analysis parameters set on the CI/CD host.

This feature requires the Administer organization permission.

Excluding files based on file content

You can exclude from the analysis files that contain a block of code matching a given regular expression. You can enter one or more regular expression patterns. Any file containing at least one of the specified patterns will be ignored. 

The parameter to be configured is Ignore Issues on Files

To define the Ignore Issues on Files parameter:

  1. Retrieve the organization.
  2. Go to Administration > Analysis scope.
  3. In Ignore Issues on Files, enter and save a regular expression pattern.
  4. You can enter a second regular expression pattern and so on.
Example

Let's say you have generated class files in your Java project that you wish to exclude. The files look something like this:

@Generated("com.example.generated")

public class GeneratedClass extends AnotherClass {

    // Some generated code

}

To exclude all such files, you might set this parameter to the following regular expression: @Generated\(".*"\)

Excluding blocks within files

You can exclude from the analysis specific blocks contained in any source file (The rest of the file will be analyzed.). The parameter to be configured is Ignore Issues in Blocks

Principles governing the use of the Ignore Issues in Blocks parameter

Blocks to be ignored are delimited within the file by start and end strings specified by regular expression patterns: 

  • If the first regular expression is found but not the second one, the end of the file is considered to be the end of the block.
  • Regular expressions are not matched across multiple lines.

Any block - within any file - containing at least one of the specified patterns will be ignored.

Defining the Ignore Issues in Blocks parameter 
  1. Retrieve the organization.
  2. Go to Administration > Analysis scope.
  3. In Ignore Issues in Blocks, enter and save a block definition.
  4. You can enter a second block definition and so on.
Example

You can use block delimiters to specify the block to be excluded as illustrated below. In this example, you want to ignore the code in the method doSomethingElse below delimited by // BEGIN-NOSCAN and // END-NOSCAN.

public class MyClass {
    public MyClass() {
        ...
    }
    public void doSomething() {
        ...
    }
    // BEGIN-NOSCAN
    public void doSomethingElse()
    {
        ...
    }
    // END-NOSCAN
}

You could define the block to be excluded with the following regular expressions:

  • Start of block: \s*//\s*START-NOSCAN
  • End of block: \s*//\s*END-NOSCAN

These regular expressions ensure that the start and end block delimiters will be recognized regardless of the number of spaces around the line comment characters (//).

Excluding specific files from specific coding rules

This section explains how to exclude specific files from specific coding rules in your project analysis. 

Introduction to coding rules exclusion

To exclude specific files from specific coding rules, you can:

  • Exclude specific files from the check against specific coding rules.
    To do so, you define exclusion criteria. An exclusion criterion is a combination of:
    • A coding rule key pattern: specifies the coding rules to be excluded.
    • A file path pattern: specifies the files to which the specified coding rules will not be applied.
  • Apply the check against specific coding rules to specific files. It means that the other files are excluded from this check.
    To do so, you define inclusion criteria. An inclusion criterion is a combination of:
    • A coding rule key pattern: specifies the coding rules to be applied.
    • A file path pattern: specifies the files to which the specified rules will be applied. The specified rules will not be applied to the other files.
Defining coding rule inclusion or exclusion criteria
  1. Retrieve the organization.
  2. Go to Administration > Analysis scope.
  3. In the Ignore Issues on Multiple Criteria parameter (to define an exclusion criterion), or in the Restrict Scope of Coding Rules (to define an inclusion criteria), enter and save a pair consisting of :
    • a pattern for coding rule keys: see Defining matching patterns for coding rules below.
    • a pattern for file paths: see Wildcard patterns in Analysis scope.
  4. You can add a second criterion, and so on.
Examples of Ignore Issues on Multiple Criteria values

For information about the pattern definitions, see see Wildcard patterns in Analysis scope.

ExampleInclusion criterion

Check:

  • The coding rule "compareTo" should not be overloaded”
  • Only on "Bean" objects and on nothing else.
  • Rule key pattern: java:s4351
  • File path pattern: **/*Bean.java

Check:

  • The coding rule “GO TO DEPENDING ON should not be used” in COBOL.
  • Only on files in the directories bank/creditcard and bank/bankcard and on nothing else.

Two criteria must be used.

Criterion 1:

  • Rule key pattern: cobol:S4883
  • File path pattern: bank/creditcard/**/*

Criterion 2:

  • Rule key pattern: cobol:S4883
  • File path pattern: bank/bankcard/**/*
Examples of Restrict Scope of Coding Rules values

For information about the pattern definitions, see see Wildcard patterns in Analysis scope.

ExampleExclusion criterion

Ignore:

  • All coding rules.
  • In all files.
  • Rule key pattern: *
  • File path pattern: **/*

Ignore:

  • All coding rules.
  • In the file bank/ZTR00021.cbl.
  • Rule key pattern: *
  • File path pattern: bank/ZTR00021.cbl

Ignore:

  • All coding rules.
  • In files located directly in the Java package com.foo, but not in its sub-packages.
  • Rule key pattern: *
  • File path pattern: com/foo/*

Ignore:

  • The C++ coding rules where the word “union” appears in the name.
  • In files in the directory object and its sub-directories.
  • Rule key pattern: cpp:Union
  • File path pattern: object/**/*

Defining matching patterns for coding rules

To define matching patterns for coding rules, use the following syntax:

<ruleRepository>:<searchString>

Where:

  • ruleRepository: is the identifier of the rule repository
    Examples: SonarQube java (identifier: java) or Security SonarAnalyzer PHP (identifier: phpsecurity), etc.
    You can use the wildcard pattern * (any string) to define the rule repository.
  • searchString: is any search string present in the rule key or in the rule name

The matching pattern means that any rule:

  • of the specified repository 
  • whose name or key contains the specified search string 

is a match.

Rule-matching pattern examples
Rule-matching patternDescription
css:S4655Rule ID s4655 in the repository css.
*:S4655Rule ID s4655 in any repository.
*All rules.
Identifying the repository, name, and key of a rule
  1. Retrieve your organization
  2. Go to Rules.
  3. Use the filter on the left to search for a rule. The search results are displayed in the right panel.
  4. In the search results, click the rule you want to view. The rule opens and you can see the rule parameters. 

Was this page helpful?

© 2008-2024 SonarSource SA. All rights reserved. SONAR, SONARSOURCE, SONARLINT, SONARQUBE, SONARCLOUD, and CLEAN AS YOU CODE are trademarks of SonarSource SA.

Creative Commons License