for
loop instead of append
SCC-S1011There is no need to range
over the slice and append elements one by one;
instead, use append
because it's clear, simple, and idiomatic.
A net.IP stores an IPv4 or IPv6 address as a slice of bytes. The length of the slice for an IPv4 address, however, can be either 4 or 16 bytes long, using different ways of representing IPv4 addresses.
for {}
) spins and can block the scheduler SCC-SA5002An empty loop is bad news in two cases: 1) The loop has no condition. In that case, it's just a loop that spins forever and as fast as it can, keeping a core busy.
host:port
pair with net.Listen
SCC-SA1020Valid host:port
pair should be of format hostname:n
where n >= 0 && n <= 65535. Passing invalid host:port
pair causes bug risk and potential security exposure.
Generally, NaN is not considered equal to any number, including itself. That's because it represnts a number outside the range of representation.
A function that calls itself recursively needs to have an exit condition. Otherwise it will recurse forever, until the system runs out of memory.
On ARM, x86-32, and 32-bit MIPS, the caller's responsibility is to arrange for 64-bit alignment of 64-bit words accessed atomically. The first word in a variable or an allocated struct, array, or slice can be relied upon to be
testing.T.FailNow
or testing.T.SkipNow
in a goroutine SCC-SA2002Calling t.Testing.FailNow
or t.Testing.SkipNow
in a goroutine isn't allowed.
Beware of unreachable case clause in a type switch.
for { select { ... } }
with an empty default branch spins SCC-SA5004If you put an empty default:
case in the select
, it makes it non-blocking, which means that when no communication can proceed it executes the default case and, being empty, finishes immediately. Then the for { }
loop runs the select
time.Now().Sub(t)
instead of time.Since(t)
SCC-S1012The time.Since(t)
helper has the same effect as using time.Now().Sub(t)
.
A finalizer is a function associated with an object that runs when the garbage collector is ready to collect said object, that is when the object is no longer referenced by anything.
If the finalizer references the object, however, it will always remain as the final reference to that object, preventing the garbage collector from collecting the object. The finalizer will never run, and the object will never be collected, leading to a memory leak. That is why the finalizer should instead use its first argument to operate on the object. That way, the number of references can temporarily go to zero before the object is being passed to the finalizer.
UTF-8
value provided SCC-SA1011Various methods in the strings package expect valid UTF-8, but invalid input is provided.
Issue is raised when templates cannot be parsed by the Parse
function of html/template
or text/template
. For example, {{.Name}} {{.LastName}
can not be parsed and causes runtime errors.
x ^ 0
, do not do anything useful SCC-SA4016Bitwise operations like x ^ 0
don't do anything.
strings.Replace
/ bytes.Replace
called with n == 0
, which does nothing SCC-SA1018With n == 0
, zero instances will be replaced. To replace all instances, use a negative number, or use strings.ReplaceAll
/ bytes.ReplaceAll
.
time.Timer.Reset()
's return value correctly SCC-SA1025It is not possible to use Reset's return value correctly, as there is a race condition between draining the channel and the new timer expiring. Reset should always be invoked on stopped or expired channels. The return value exists to preserve compatibility with existing programs.
Comparing unsigned values against negative values is pointless. This expression will always be true, because unsigned integers will never be less than zero.
The value used in the loop condition never changes. In most cases, you might be incrementing the wrong variable.
Binding to all network interfaces can potentially open up a service to traffic on unintended interfaces.