Regex Rules Reference

The Regex rules help you search for specific string patterns in your code. To select these rules, use --rule-selector regex. For example, to run all the Regex rules:

sf code-analyzer run --rule-selector regex

AvoidGetHeapSizeInLoop

Description: Detects usage of the Limits.getHeapSize() Apex method in for, while, and do-while loops.

Default Severity: 2 (High)

Default Tags: Recommended, Performance, Apex

Example(s):

  • [Bad] Code that causes a violation:
    public static void someMethodWithForLoop() {
        for (Integer i = 0, j = 0; i < 10; i++) {
            Limits.getHeapSize();
        }
    }

AvoidOldSalesforceApiVersions

Description: Detects usage of Salesforce API versions that are 3 or more years old.

Default Severity: 2 (High)

Default Tags: Recommended, Security, Xml

AvoidTermsWithImplicitBias

Description: Detects usage of terms that reinforce implicit bias.

Default Severity: 5 (Info)

Default Tags: Recommended, BestPractices

More Information: Salesforce Updates Technical Language in Ongoing Effort to Address Implicit Bias.

MinVersionForAbstractVirtualClassesWithPrivateMethod

Description: Detects private methods within abstract or virtual Apex classes when the corresponding API version of the class is less than 61.0.

Default Severity: 2 (High)

Default Tags: Recommended, BestPractices, Apex

Example(s):

  • [Bad] Code that causes a violation:
public abstract class SomeAbstractClass {
    public void somePublicMethod() {
        somePrivateMethod();
    }
    private void somePrivateMethod() {
        // ...
    }
}

With this metadata XML file:

<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
    <!-- Here's a comment -->
    <apiVersion>60.0</apiVersion>
</ApexClass>

NoTrailingWhitespace

Description: Detects trailing whitespace (tabs or spaces) at the end of lines of code and lines that are only whitespace.

Default Severity: 5 (Info)

Default Tags: Recommended, CodeStyle, Apex

NoMixedIndentation

Description: Detect if tabs and spaces are mixed together. Mixing tabs and spaces can cause unexpected formatting issues, especially in the Developer Console.

Default Severity: Moderate (3)

Default Tags: Recommended

Example(s):

  • [Bad] Code that causes a violation:
public class MyClass {
⇥   String msg = '''    // Tab + spaces
        Hello World
    ''';
}
  • [Clean] Code:
public class MyClass {
    String msg = '''    // Only spaces
        Hello World
    ''';
}
public class MyClass {
⇥String msg = '''      // Only tabs
⇥⇥Hello World
⇥''';
}