Sub topic 13
Fixing findings and suppressing false positives
This chapter assumes you can run sf code-analyzer run, read the severity and rule columns in the results, and edit Apex. After a scan you have a list of violations. Each one needs a decision: fix the code, or suppress the finding because the rule is wrong for that spot.
Decide: fix first, suppress second
Most findings are real. Start with the highest severity and work down. For each finding, ask two questions:
- Does the code do what the rule describes? If yes, fix it.
- Is the rule's concern impossible or irrelevant here? If yes, it is a false positive. Suppress it and say why.
Suppressing a real problem only hides it. A governor-limit risk stays a risk after you silence the warning.
Fixing a real finding
The AccountUpdater class from the earlier scan runs a DML statement inside a loop. Every loop iteration uses one DML statement, and a transaction is limited to 150. A list of 200 accounts fails at runtime.
public class AccountUpdater {
public static void renameAll(List<Account> accounts) {
for (Account acc : accounts) {
acc.Name = acc.Name + ' Inc.';
}
update accounts;
}
}The loop now only changes records in memory. One update sends the whole list in a single DML statement. Run the scan again to confirm that the violation is gone.
sf code-analyzer run --workspace force-app --rule-selector Recommended --view tableSuppressing a false positive
A false positive is a finding where the rule matched the code but the code is correct. For example, a small utility class may log with System.debug on purpose, and a rule that flags debug statements will report it.
For PMD rules, add a NOPMD comment on the line you want to skip. Write the reason next to it.
public class Logger {
public static void trace(String msg) {
System.debug(msg); // NOPMD - intentional debug logging in sandbox-only utility
}
}The comment applies to that line only. Other violations in the file are still reported.
Choosing the right level of suppression
Inline comments are the narrowest tool. Use the wider tools only when the same decision applies to many places.
| Situation | Tool | Scope |
|---|---|---|
| One line is a verified false positive | Inline `NOPMD` comment | One line |
| A rule is too strict for your team | Lower its severity in `code-analyzer.yml` | Every use of that rule |
| Code is third-party or generated | `ignores` in `code-analyzer.yml` | Whole files or folders |
Lowering a severity can also change CI results. A rule set to Info no longer trips a --severity-threshold 2 check.
A triage routine
Rerun the scan after every change. A fix can expose a different finding in the same method, and a suppression comment placed on the wrong line will not take effect.
Exercise
A scan reports a DML-in-loop violation in a class that updates records one at a time inside a loop over a list. A teammate suggests adding a NOPMD comment so the build passes. What should you do, and when would a suppression be acceptable?
Show answer
Fix it. Collect the changed records in a list and run one DML statement after the loop, then rerun the scan. The rule describes a real governor-limit risk, so suppressing it only hides the problem. A suppression is acceptable only for a verified false positive, and it needs a comment on that line that states the reason.