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.
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)
.
Two struct types with identical fields can be converted, and hence a manual copy of struct fields could be avoided with the help of type conversion.
Instead of using strings.HasPrefix
and manual slicing, use the
strings.TrimPrefix
function. The original string will be returned
if the string doesn't start with the prefix.
copy()
for sliding elements SCC-S1018copy()
permits using the same source and destination slice, even with
overlapping ranges. This makes it ideal for sliding elements in a slice.
make
call by omitting redundant arguments SCC-S1019The make
function has default values for the length and capacity arguments.
nil
check in type assertion SCC-S1020There is no need to check for nil
when performing type assertion as it is
implicitly handled. When false
, the second value returned confirms that the
value of the asserted type is nil
.
Variable declaration and assignment can be merged, simplifying the code.
Omit redundant control flow in your Go code.
x.Sub(time.Now())
instead of time.Until(x)
SCC-S1024The time.Until
helper has the same effect as using x.Sub(time.Now())
. The
former is easier to read.
fmt.Sprintf("%s", x)
unnecessarily SCC-S1025There are more accessible and efficient ways of getting a value's string
representation in many instances. Whenever a value's underlying type is a
string
already, or the type has a String()
method, they should be used
directly.
nil
check around loop SCC-S1031When using range
on nil slices and maps, the loop will never execute,
as the length of the iterable will be regarded as zero. This makes an additional
nil
check around the loop unnecessary.
sort.Ints(x)
, sort.Float64s(x)
, and sort.Strings(x)
SCC-S1032The sort.Ints
, sort.Float64s
and sort.Strings
functions are shorthands and are easier to read than sort.Sort(sort.IntSlice(x))
, sort.Sort(sort.Float64Slice(x))
and sort.Sort(sort.StringSlice(x))
delete
SCC-S1033Calling delete on a nil
map does not
do anything (no-op), and it can be safely removed, or there is a potential bug
introduced with a typo.
Instead of asserting the type every time in a switch
statement, assign the
result of type assertion to a variable and use it.
net/http.CanonicalHeaderKey
in method call on net/http.Header
SCC-S1035The methods on net/http.Header
, namely Add
, Del
, Get
and Set
, already canonicalize the given header name. Hence, there is no need to call the http.CanonicalHeaderKey
function for these headers.
A process cannot intercept all signals. Specifically, on UNIX-like systems, the
syscall.SIGKILL
and syscall.SIGSTOP
signals cannot be captured by the
process but handled directly by the kernel. It is, therefore, pointless
to try and handle these signals.
It is not recommended to use unnecessary blocks.
Return value of a pure function the value should always be handled else, calling the function is pointless. If the return value is not required, it is recommended to remove the function call.
Multiple, identical build constraints in the same file.