Sub topic 7
Reading results and severity levels
This chapter assumes you have installed the code-analyzer plugin, can run sf code-analyzer run against a DX project, and know what a rule selector is. Here you learn how to read what a scan prints and how to decide what to fix first.
What a violation contains
Each finding is called a violation. Every violation carries the same core fields, whichever engine produced it.
| Field | Meaning |
|---|---|
| Severity | A number from 1 to 5 and a name (Critical, High, Moderate, Low, Info). |
| Rule | The rule name, such as `AvoidDmlStatementsInLoops`. |
| Engine | The engine that ran the rule, such as `pmd` or `eslint`. |
| Location | The file, line, and column where the problem starts. |
| Message | A short description of what is wrong. |
Run the scan from the earlier chapter against the AccountUpdater class:
sf code-analyzer run --workspace force-app --rule-selector Recommended --view tableThe table lists one row per violation. A row for the DML-in-a-loop problem names the rule, the engine pmd, the file AccountUpdater.cls, and the line of the update acc; statement. Open the file at that line, and the cause is the statement itself.
Severity levels
Severity ranks how serious a rule's findings are. The lower the number, the more serious the finding.
| Number | Name | Typical reading |
|---|---|---|
| 1 | Critical | Likely defect or security hole. Fix before merging. |
| 2 | High | Serious risk, such as a governor-limit or security problem. |
| 3 | Moderate | Worth fixing, usually a quality or performance issue. |
| 4 | Low | Style or minor maintainability. |
| 5 | Info | Informational only. |
Severity belongs to the rule, not to the individual finding, so every violation of one rule has the same severity. You can change a rule's severity in the configuration file, which a later chapter covers.
Reading the output in order
Work through results in a consistent order:
- Start with severity 1 and 2 violations. These are the ones most likely to matter in production.
- Group by rule. A single rule that fires 40 times usually points to one habit, and one fix pattern covers all of them.
- Check the engine column. It tells you which tool's documentation explains the rule, and whether a finding comes from static pattern matching (PMD, ESLint) or from another kind of check.
- Open the location and read the surrounding code before deciding. A rule is a heuristic, so some findings are false positives.
Counts and exit status
The run prints a summary with the total number of violations and a count per severity. The command still exits with status 0 when violations exist, unless you set a severity threshold. A clean exit therefore does not mean a clean scan. A later chapter covers failing a run.
Common mistakes
- Treating a higher number as more serious. Severity 1 is the most serious.
- Reading only the summary count. Ten Info findings matter less than one Critical finding.
- Assuming a zero exit code means no violations.
- Fixing findings by file instead of by rule, which repeats work.
Exercise
A scan reports 30 severity 4 violations of one rule and 2 severity 1 violations of another rule. The command exits with status 0. Which findings do you review first, and why did the exit status not signal a problem?
Show answer
Review the 2 severity 1 violations first, because severity 1 is Critical and the lowest number is the most serious. The exit status was 0 because the command only fails on violations when you set a severity threshold, and none was set.