Sub topic 7
Reading results and severity levels
This chapter assumes you can run sf commands in a terminal, have installed the code-analyzer plugin, and have run at least one scan with sf code-analyzer run. You should also be able to read a short Apex class. The chapter covers how to read a scan's output, what the five severity levels mean, and how to decide which findings to act on first.
What a violation contains
Every finding is a violation. Code Analyzer reports each violation with the same fields, whichever engine produced it:
| Field | Meaning |
|---|---|
| Rule | The rule that fired, written as `engine:RuleName`, for example `pmd:OperationWithLimitsInLoop` |
| Engine | The engine that ran the rule (PMD, ESLint, RetireJS, Regex, Flow, CPD, or the graph engine) |
| Severity | A number from 1 to 5, where 1 is the most serious |
| Location | File path, line, and column where the problem starts |
| Message | A one-sentence description of the problem |
| Tags | Labels such as `Recommended`, `Security`, `Performance`, or `Apex` |
| Resources | Links to documentation for the rule |
Reading a result means answering four questions in order. Which rule fired? How severe is it? Where is it? Is it a real problem in this code?
The five severity levels
Code Analyzer uses one scale for all engines, so a PMD finding and an ESLint finding can be compared directly. Each engine maps its own rating onto this scale.
| Number | Name | Typical meaning |
|---|---|---|
| 1 | Critical | Serious security or correctness problem. Fix before merging. |
| 2 | High | Likely bug, security weakness, or governor-limit failure. |
| 3 | Moderate | Quality or performance problem that should be fixed soon. |
| 4 | Low | Style or maintainability issue. |
| 5 | Info | Advice. Often safe to defer. |
Lower numbers are more severe. This is the opposite of what many people expect, and it affects the threshold option in the next chapter. "Severity 2 or more severe" means severity 1 and 2, not 2 through 5.
A rule's default severity can change. You can override it in code-analyzer.yml, so a team's results may differ from the defaults described here. Treat severity as a starting point for prioritizing, not a final verdict.
Reading the table view
Scan the small class from earlier chapters. It has a DML statement inside a loop:
public class AccountUpdater {
public static void renameAll(List<Account> accounts) {
for (Account acc : accounts) {
acc.Name = acc.Name + ' Inc.';
update acc;
}
}
}Run it with the table view:
sf code-analyzer run --workspace force-app --rule-selector Recommended --view tableThe output looks something like this. The exact rules, severities, and columns depend on your plugin version, so this sample is illustrative.
Found 1 violation(s) across 1 file(s):
Critical: 0
High: 0
Moderate: 1
Low: 0
Info: 0
# Severity Rule Location Message
1 3 (Moderate) pmd:OperationWithLimitsInLoop AccountUpdater.cls:5:13 Avoid operations in loops that may hit governor limitsRead it from top to bottom:
- The summary at the top gives counts per severity. Check it first to decide how urgent the run is.
- The rows are sorted so that the files and violations with the highest severity appear first.
- The Location column has the form
file:line:column. Most terminals and editors let you click or copy it to jump to the code. - The Rule column tells you the engine before the colon. You need this when you override a rule or select rules later.
Here the finding points to line 5, the update acc; statement. The cause is a DML statement inside a loop. A transaction allows 150 DML statements, so a list of 200 accounts would hit the limit. The fix is to collect records and update them once after the loop.
public class AccountUpdater {
public static void renameAll(List<Account> accounts) {
for (Account acc : accounts) {
acc.Name = acc.Name + ' Inc.';
}
update accounts;
}
}Reading the detail view
The table is compact, and long messages are hard to read in it. Use the detail view when you need to study the findings one at a time:
sf code-analyzer run --workspace force-app --rule-selector Recommended --view detailThe detail view groups violations by file and prints each one with its full message, its tags, and its resource links. Use the table to get an overview of a large scan, and switch to detail when you work through the findings for a single file.
Understanding where a violation sits
Two rules can fire on the same line. This is normal, because different engines look for different things. A line of Apex can be flagged by a PMD rule about performance and also by a Regex rule about a banned pattern. Fix the more severe finding first, then re-run, because fixing one often removes the other.
Some violations have more than one location. A data-flow finding from the graph engine, for instance, can point to the place where tainted data enters and to the place where it is used. The report names a primary location, which is the line to open first, and lists the other locations as supporting evidence.
Deciding what to do with each finding
A scan is only useful if the findings turn into decisions. A practical order is:
- Fix every severity 1 finding, unless you can show it is a false positive.
- Review severity 2 findings. Most are real, and these are the usual CI gate.
- Batch severity 3 findings into planned cleanup work.
- Treat severity 4 and 5 findings as advice. If a rule produces mostly noise for your team, lower its severity or remove it from your selector instead of ignoring the output.
A finding falls into one of three groups:
- Real problem: change the code and re-run.
- False positive: the rule is wrong for this line. Suppress it narrowly and write the reason beside the suppression, as a later chapter shows.
- Rule you disagree with: change the rule's severity or leave it out of the selector. Do not suppress findings one at a time to work around a rule you do not want.
flowchart TDCommon mistakes when reading results
- Reading severity backwards. Severity 1 is the worst. A threshold of 2 catches Critical and High, not Moderate through Info.
- Treating zero violations as proof of safety. A scan only checks the files in the workspace with the rules you selected. If you used
--rule-selector pmd:Security, a clean result says nothing about performance rules. - Ignoring the engine prefix. Two engines can define similarly named rules. The
engine:RuleNameform is what the configuration file and selectors expect. - Trusting line numbers after edits. Locations refer to the files as they were when the scan ran. Re-run after you change code.
- Counting violations as a quality score. One severity 1 finding matters more than twenty severity 5 findings. Compare severity counts, not totals.
Exercise
A scan prints "Moderate: 4, High: 1, Critical: 0, Low: 7, Info: 12". Which findings do you look at first, and what does a threshold of --severity-threshold 3 do with this result?
Show answer
Look at the one High (severity 2) finding first, then the four Moderate (severity 3) findings. There are no Critical findings. A threshold of 3 makes the command exit with a nonzero status when any violation has severity 3 or more severe, so this run would fail because it has 1 High and 4 Moderate violations. The 7 Low and 12 Info findings do not affect the exit status.
Exercise
The table shows pmd:OperationWithLimitsInLoop at AccountUpdater.cls:5:13. What do the three numbers after the file name mean, and what is the likely fix?
Show answer
The numbers are the file name, line, and column: line 5, column 13. Line 5 holds a DML statement (update acc;) inside a for loop. The fix is to remove the statement from the loop, collect the changes in the list, and run one update accounts; after the loop.