Elaborate Sleep
ID |
go.elaborate_sleep |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Code Smell |
Language |
Go |
Tags |
CWE:1164, code-style, concurrency, readability |
Description
Reports a select with a single communication case and no default whose case receives from
time.After(d) — an elaborate, allocation-heavy way of writing time.Sleep(d).
Rationale
A select blocking on one ←time.After(d) case waits for the duration d and then proceeds,
which is exactly the behaviour of time.Sleep(d). The select form adds noise and allocates a
timer channel for no benefit, so it obscures the intent — readers expect a select to choose
between several operations. The check fires only on a single-case select (no default) whose case
receives from time.After; a multi-case select or a different channel operation is left alone.
select { // FLAW — this is just time.Sleep(d)
case <-time.After(d):
}
time.Sleep(d) // OK — clearer and cheaper