Sub topic 13
Fixing findings and suppressing false positives
A scan only helps if its findings lead to decisions. This chapter covers how to triage a violation, when to fix the code, and when to suppress the finding with a justified comment. It assumes you can run sf code-analyzer run, read the table output, and use severity levels, as covered in earlier chapters. It also assumes you can read basic Apex and JavaScript.
Triage first, then act
Each finding falls into one of three groups:
- True positive. The code has the problem the rule describes. Fix it.
- False positive. The rule misread the code, and the code is safe. Suppress the finding and record why.
- Accepted risk. The rule is right, but the team knowingly accepts the cost, for example debug logging in a sandbox-only utility. Suppress it with a written reason, or change the rule's severity in
code-analyzer.yml.
Look at four fields of each violation before deciding: the rule name, the engine, the severity, and the file and line. The rule name tells you what the engine thinks is wrong. The rule's documentation, which you can see with --view detail, explains the reasoning. Read it before you decide the rule is wrong.
Fixing a true positive
The tutorial's running example puts a DML statement inside a loop. DML is any statement that writes to the database, such as update. Apex allows 150 DML statements per transaction, so a loop over a large list can hit that governor limit and fail at runtime. The PMD engine flags this pattern. In recent PMD versions the rule is named OperationWithLimitsInLoop. Use whatever name your own results show.
public class AccountUpdater {
public static void renameAll(List<Account> accounts) {
for (Account acc : accounts) {
acc.Name = acc.Name + ' Inc.';
update acc;
}
}
}The fix is to change the records inside the loop and run one DML statement after it:
public class AccountUpdater {
public static void renameAll(List<Account> accounts) {
for (Account acc : accounts) {
acc.Name = acc.Name + ' Inc.';
}
update accounts;
}
}Records that are already in the list can be updated in place, so no second collection is needed. Rescan the single file to confirm the finding is gone:
sf code-analyzer run --workspace force-app --target force-app/main/default/classes/AccountUpdater.cls --rule-selector Recommended --view tableFixing the code is almost always cheaper than explaining why you did not. Suppression is for the cases where fixing is wrong or impossible.
Suppressing a false positive inline
An inline suppression is a comment or annotation that tells one engine to ignore a rule at one location. The syntax belongs to the engine, not to Code Analyzer, so each engine has its own form.
| Engine | Language | Suppress one line | Suppress a rule in a scope |
|---|---|---|---|
| PMD | Apex | `// NOPMD` at the end of the line | `@SuppressWarnings('PMD.RuleName')` on a class or method |
| ESLint | JavaScript, LWC | `// eslint-disable-next-line rule-name` | `/* eslint-disable rule-name */` around a block |
Keep the three PMD forms apart. // NOPMD silences every PMD rule on that line, so it can hide a second, real problem on the same line. @SuppressWarnings('PMD.AvoidDebugStatements') silences only the named rule. Prefer the named form whenever you can.
Here is the NOPMD form from the earlier example, with the reason beside it:
public class Logger {
public static void trace(String msg) {
System.debug(msg); // NOPMD - intentional debug logging in sandbox-only utility
}
}The same suppression, scoped to one rule on one method, looks like this:
public class Logger {
@SuppressWarnings('PMD.AvoidDebugStatements')
public static void trace(String msg) {
System.debug(msg); // sandbox-only utility, see ticket SEC-142
}
}In an LWC JavaScript file, the ESLint equivalent targets one rule on the next line:
// eslint-disable-next-line no-console -- logging is intentional in this diagnostic helper
console.log('refresh handler fired');ESLint treats the text after -- as a description of why the rule is disabled. Always write one.
Writing a good justification
A suppression without a reason is a future mystery. A reviewer cannot tell whether it was a considered decision or a way to get a build green. A good justification:
- states why the rule does not apply here, not what the rule is
- names a stable reference when one exists, such as a ticket or a design note
- stays on or next to the suppressed line so it moves with the code
Compare // NOPMD with // NOPMD - values come from a Custom Metadata record, not user input. The second one can be reviewed, and it can become wrong later in a way a reviewer can notice.
Choosing between inline suppression and configuration
Inline suppression fits one spot. When the same finding appears in many places, the better tool is the configuration file. There are two common cases.
The rule is too strict for your project. Lower the severity or tags of that rule in code-analyzer.yml. The finding still shows up in reports, but it no longer trips a severity threshold:
# code-analyzer.yml
rules:
pmd:
AvoidDebugStatements:
severity: "Info"The code is not yours. Third-party libraries and generated files should not be fixed or annotated. Exclude them with ignores:
# code-analyzer.yml
ignores:
files:
- "**/node_modules/**"
- "**/staticresources/**"Run a scan that uses the file:
sf code-analyzer run --config-file code-analyzer.yml --workspace force-app --rule-selector Recommended| Situation | Best tool |
|---|---|
| One line is a verified false positive | Inline suppression with a reason |
| One rule is noisy everywhere in your project | Severity override in `code-analyzer.yml` |
| Whole folders are vendor or generated code | `ignores` in `code-analyzer.yml` |
| The finding is real | Fix the code |
Common mistakes
- Suppressing to make CI pass. If a
--severity-thresholdgate fails the build, the gate is doing its job. Fix the code or make a recorded decision. - Wide suppressions.
/* eslint-disable */with no rule name at the top of a file turns off every rule for the whole file. Name the rule and keep the scope small. - Ignoring too much. An
ignoresglob such as**/classes/**removes all your Apex from the scan. Test an ignore pattern by checking that the expected files still appear in results. - Forgetting to rescan. After a fix or suppression, run the same command that produced the finding. A suppression with a typo in the rule name does nothing, and only a rescan shows that.
- Never revisiting suppressions. Search the codebase for
NOPMDandeslint-disablenow and then, and remove those whose reason no longer holds.
A quick audit of PMD suppressions looks like this:
grep -rn --include="*.cls" --include="*.trigger" -E "NOPMD|SuppressWarnings\('PMD" force-appExercise
A scan reports a PMD finding on a line of Apex that also carries // NOPMD. In the same results, an ESLint finding on a line of an LWC file carries // NOPMD. The first finding disappears on rescan, but the second does not. Explain why, and give the better suppression for the second file.
Show answer
NOPMD is a PMD feature, so it only affects findings from the PMD engine, which scans Apex. ESLint does not read it, so the LWC finding remains. Use an ESLint comment that names the rule and gives a reason, for example // eslint-disable-next-line no-console -- logging is intentional here, placed on the line above the code.
Exercise
Your project has 40 System.debug findings from the PMD rule AvoidDebugStatements, all in a utility package that only ever runs in sandboxes. Should you add 40 inline suppressions or change the configuration? State your choice and the config you would use.
Show answer
Change the configuration. The decision applies to the whole project, so repeating it 40 times adds noise. Set the rule's severity in code-analyzer.yml, for example under rules: then pmd: then AvoidDebugStatements: with severity: "Info". The findings stay visible in reports, and a threshold such as --severity-threshold 2 no longer fails on them.