Go rules
tx0 checks .go files with 199 rules, 70 of them in the default tier. Files that say a tool generated them are skipped. Each rule's page explains what it finds and what it leaves alone.
Default: 70 rules
Bugs and security problems with measured precision. They run without a tx0.toml.
-
tx0/go:address-compared-with-nildefaultThe address of a value is compared with nil, which it never is
-
tx0/go:always-true-inequalitydefaultA value is tested to differ from one constant or another, which every value does
-
tx0/go:append-nothingdefaultappendis called with a slice and nothing to append -
tx0/go:atomic-result-assigneddefaultThe result of an atomic add is assigned back to the variable it updated, which is not atomic
-
tx0/go:benchmark-n-assigneddefaultA benchmark changes
b.N, which the testing package sets and divides by -
tx0/go:bidi-control-characterdefaultThe source contains a character that reorders how text is displayed, so code reads differently than it runs
-
tx0/go:compare-new-errordefaultAn error is compared with a newly made one, which never equals it
-
tx0/go:decimal-file-modedefaultA file mode is written as a decimal number that reads like octal, such as
644 -
tx0/go:defer-before-error-checkdefaultA call on a result is deferred before the error of the call is checked
-
tx0/go:defer-evaluates-nowdefaultA deferred call is given an elapsed time, which is measured when
deferruns rather than at the end -
tx0/go:discarded-query-editdefaultThe query of a URL is changed on a parsed copy that is then dropped
-
tx0/go:discarded-resultdefaultA function that only computes a value is called and its value is thrown away
-
tx0/go:double-lockdefaultA mutex is locked again by the code that holds it, which blocks forever
-
tx0/go:double-negationdefaultA boolean is negated twice
-
tx0/go:duplicate-casedefaultA case of a switch repeats an earlier one
-
tx0/go:duplicate-conditiondefaultA condition of an if-else chain repeats an earlier one
-
tx0/go:empty-error-checkdefaultAn error is checked but not handled
-
tx0/go:errors-as-targetdefaulterrors.Asis given a target that is nil or not a pointer, and panics -
tx0/go:fatal-in-goroutinedefaultA test is stopped with
Fatal,FailNoworSkipfrom a goroutine it started -
tx0/go:find-all-count-zerodefaultA
FindAllmethod of a regular expression is asked for at most zero matches -
tx0/go:foreign-time-layoutdefaultA time layout is written with placeholders such as
YYYYthat Go'stimepackage takes literally -
tx0/go:function-compared-with-nildefaultA declared function is compared with
nil, which it never is -
tx0/go:identical-argumentsdefaultA function that compares or replaces is given the same expression twice
-
tx0/go:identical-branchesdefaultEvery branch of an if-else is the same
-
tx0/go:identical-operandsdefaultBoth operands of an operator are the same
-
tx0/go:impossible-conjunctiondefaultA condition requires one value to meet two comparisons that no value meets together
-
tx0/go:impossible-sign-checkdefaultA length or an unsigned number is compared with zero in a way that is always true or always false
-
tx0/go:index-at-lengthdefaultA slice, array or string is indexed at its length, which is always out of range
-
tx0/go:ineffective-recoverdefaultrecoveris called where it always returns nil and stops no panic -
tx0/go:invalid-regexpdefaultA constant regular expression is not valid in Go's syntax
-
tx0/go:invalid-strconv-sizedefaultA
strconvfunction is given a base or bit size it does not accept -
tx0/go:loop-condition-unchangeddefaultNothing in a loop changes its condition and nothing leaves it, so it never ends once entered
-
tx0/go:loop-variable-addressdefaultThe address of a range variable is kept beyond its iteration, before Go 1.22, so all copies point to one variable
-
tx0/go:loop-variable-captureddefaultA goroutine or deferred function reads a loop variable that later iterations change, before Go 1.22
-
tx0/go:lost-canceldefaultThe cancel function of a derived context is discarded
-
tx0/go:malformed-test-namedefaultA test function is named so that
go testnever runs it -
tx0/go:misdeclared-standard-methoddefaultA method has the name of a standard interface's method but another signature, so it is not used
-
tx0/go:mismatched-unlockdefaultA lock is followed by a deferred call that locks again or releases the other kind of lock
-
tx0/go:misplaced-build-constraintdefaultA build constraint comes after the package clause, where the go tool ignores it
-
tx0/go:modulo-onedefaultA remainder is taken of a division by 1, which is always 0
-
tx0/go:nan-comparisondefaultA number is compared with
math.NaN(), which gives the same result for every number -
tx0/go:negative-zero-literaldefault-0.0is written for negative zero, but the constant is positive zero -
tx0/go:nil-contextdefaultnilis passed as the parent context to a function ofcontext, which panics -
tx0/go:old-tls-versiondefaultA TLS configuration allows SSL 3.0, TLS 1.0 or TLS 1.1
-
tx0/go:printfdefaultA format and its arguments do not match
-
tx0/go:random-range-onedefaultA random number is drawn below 1, which always gives 0
-
tx0/go:recursive-string-methoddefaultA
StringorErrormethod formats its own receiver withfmt, which calls the method again -
tx0/go:reflect-value-equalitydefaultTwo
reflect.Values are compared with==, which compares their internals rather than the values they hold -
tx0/go:replace-count-zerodefaultstrings.Replaceorbytes.Replaceis told to make no replacements -
tx0/go:return-nil-errordefaultAn error variable is returned in the branch where it was just found to be nil
-
tx0/go:self-assignmentdefaultA value is assigned to itself
-
tx0/go:shadowed-assertion-elsedefaultThe
elsebranch of a type assertion uses the new variable, which holds a zero value there -
tx0/go:sleep-without-unitdefaulttime.Sleepis given a small number without a unit, which counts nanoseconds -
tx0/go:slog-unpaired-argumentdefaultA structured log call is given a key without a value
-
tx0/go:small-rsa-keydefaultAn RSA key is generated with fewer than 2048 bits
-
tx0/go:spaced-directivedefaultA
go:directive has a space after the slashes, which turns it into a plain comment -
tx0/go:split-count-zerodefaultstrings.SplitNorbytes.SplitNis asked for zero parts and returns nil -
tx0/go:string-of-intdefaultAn integer is converted to a string, which makes a character rather than its digits
-
tx0/go:struct-tagdefaultA struct tag is malformed or its fields' names conflict
-
tx0/go:swapped-assign-operatordefault=+,=-or=!is written where+=,-=or!=was likely meant -
tx0/go:trim-cutset-repeatsdefaultThe set of characters given to
Trim,TrimLeftorTrimRightrepeats a character, as a prefix or suffix would -
tx0/go:unbuffered-signal-channeldefaultsignal.Notifyis given a channel without a buffer, so signals that arrive while nobody receives are lost -
tx0/go:uncatchable-signaldefaultA program asks to handle
SIGKILLorSIGSTOP, which never reach it -
tx0/go:unconditional-recursiondefaultA function calls itself before anything could stop it, so it recurses until the stack overflows
-
tx0/go:unmarshal-non-pointerdefaultData is decoded into a value that is not a pointer, which cannot receive it
-
tx0/go:unreachable-codedefaultCode that can never run
-
tx0/go:use-after-closedefaultA channel is sent to or closed again after it was closed
-
tx0/go:waitgroup-add-in-goroutinedefaultWaitGroup.Addis called inside the goroutine it counts -
tx0/go:wrong-loop-directiondefaultA loop steps its counter away from the bound of its condition
-
tx0/go:wrong-loop-variabledefaultA loop tests one variable and steps another
Recommended: 64 rules
Likely bugs, resource and performance problems.
-
tx0/go:append-after-make-lengthrecommendedA slice made with a length is appended to, so the values follow that many zero elements
-
tx0/go:archive-entry-pathrecommendedThe name of an archive entry is joined to a directory without checking that it stays inside
-
tx0/go:busy-waitrecommendedA loop spins on the processor while it waits, with an empty body or a
selectwhosedefaultdoes nothing -
tx0/go:cancel-not-called-on-pathrecommendedThe cancel function of a derived context is not called on a path to a
return -
tx0/go:constant-conditionrecommendedAn
iftests the constanttrueorfalse, so one of its branches never runs -
tx0/go:constant-ivrecommendedData is encrypted with an initialization vector or nonce that is the same every time
-
tx0/go:constant-saltrecommendedA password is hashed with a salt written into the program, the same for every password
-
tx0/go:context-key-basic-typerecommendedA context value is stored under a key of a predeclared type, which other packages can collide with
-
tx0/go:copied-lockrecommendedA value that contains a lock is copied
-
tx0/go:dead-storerecommendedA value is assigned to a local variable and replaced or dropped before anything reads it
-
tx0/go:deep-nestingrecommendedControl statements are nested deeper than the configured maximum
-
tx0/go:defer-in-looprecommendedA
deferinside a loop runs only when the function returns -
tx0/go:duplicate-importrecommendedA file imports the same package twice under different names
-
tx0/go:empty-critical-sectionrecommendedA lock is released right after it is taken, protecting nothing
-
tx0/go:error-text-comparisonrecommendedAn error is identified by comparing its message with a constant string
-
tx0/go:exit-skips-deferrecommendedThe program exits from a function after a
defer, so the deferred call never runs -
tx0/go:file-not-closedrecommendedA file is opened and never closed
-
tx0/go:fixed-temp-pathrecommendedA file is created under a fixed name in a shared temporary directory
-
tx0/go:fresh-context-with-parameterrecommendedA function that is given a context starts a new one with
context.Background()orcontext.TODO() -
tx0/go:goroutine-dump-in-responserecommendedA stack trace is written into an HTTP response
-
tx0/go:goroutine-stuck-sendingrecommendedA goroutine sends its result on an unbuffered channel that the function may stop waiting for, and then blocks forever
-
tx0/go:high-complexityrecommendedA function has more independent paths through it than the configured maximum
-
tx0/go:http-error-without-returnrecommendedA handler writes an error response and then carries on as if the request had succeeded
-
tx0/go:ignored-errorrecommendedThe error of a standard library call that changes files, the environment or a value is dropped
-
tx0/go:insecure-cipher-suiterecommendedA TLS configuration names a cipher suite that is known to be insecure
-
tx0/go:jwt-unverifiedrecommendedA JSON Web Token is read without checking its signature, or may be unsigned
-
tx0/go:lock-held-on-returnrecommendedA mutex that the function unlocks elsewhere is still locked on a path to a
return -
tx0/go:long-functionrecommendedA function has more lines of code than the configured maximum
-
tx0/go:long-parameter-listrecommendedA function takes more parameters than the configured maximum
-
tx0/go:loop-runs-oncerecommendedA counting or conditional loop always leaves at the end of its first iteration
-
tx0/go:nil-check-after-userecommendedA pointer is compared with nil after a field was already read or written through it
-
tx0/go:nil-dereferencerecommendedA nil pointer, map or slice is used in a way that panics
-
tx0/go:open-redirectrecommendedA handler redirects to a location taken from the request without a check
-
tx0/go:overwritten-parameterrecommendedA parameter is assigned before its value is read, so the argument is ignored
-
tx0/go:password-fast-hashrecommendedA password is hashed with a fast general-purpose hash instead of a password hashing function
-
tx0/go:predictable-random-secretrecommendedA token, key, password or salt is made from
math/rand, whose output can be predicted -
tx0/go:regexp-in-looprecommendedA constant regular expression is compiled on every iteration of a loop
-
tx0/go:request-chosen-memberrecommendedA method or field is chosen by reflection with a name taken from an HTTP request
-
tx0/go:request-path-in-file-accessrecommendedA file is opened, written or removed at a path taken from an HTTP request without a check
-
tx0/go:request-url-fetchedrecommendedA server sends a request to an address taken from an incoming request without a check
-
tx0/go:request-value-in-commandrecommendedA program is run with a name or argument taken from an HTTP request without a check
-
tx0/go:request-value-in-environmentrecommendedAn environment variable of the process is set from an HTTP request
-
tx0/go:resource-open-on-returnrecommendedA file or connection that the function closes elsewhere is left open on a path to a
return -
tx0/go:response-body-not-closedrecommendedThe body of an HTTP response is never closed, which keeps its connection from being reused
-
tx0/go:rows-error-uncheckedrecommendedRows of a database query or lines of a scanner are read in a loop whose ending error is never checked
-
tx0/go:rows-not-closedrecommendedThe rows of a database query are never closed, which holds on to the connection
-
tx0/go:secret-in-literalrecommendedA private key or a service token is written into a string literal
-
tx0/go:sentinel-error-equalityrecommendedAn error is compared with
==to a standard sentinel error that is usually wrapped -
tx0/go:server-without-timeoutsrecommendedAn HTTP server is started without a timeout for reading requests
-
tx0/go:shell-command-builtrecommendedA command line built at run time is handed to a shell
-
tx0/go:sql-built-from-stringsrecommendedAn SQL statement passed to a query method is pieced together from strings at run time
-
tx0/go:ssh-host-key-ignoredrecommendedAn SSH client accepts any host key, so it cannot tell the server from an impostor
-
tx0/go:stat-then-createrecommendedA file is created after a separate check that it does not exist, leaving a gap for a race
-
tx0/go:strings-index-gt-zerorecommendedThe result of
strings.Indexis compared with> 0, which misses a match at the start -
tx0/go:tick-leakrecommendedtime.Tickis called in a function that returns, which leaks its ticker before Go 1.23 -
tx0/go:tls-verification-disabledrecommendedA TLS configuration turns off the verification of the server's certificate
-
tx0/go:transaction-not-rolled-backrecommendedA database transaction is started in a function that never rolls it back
-
tx0/go:unbounded-decompressionrecommendedEverything a decompressing reader produces is copied without a limit
-
tx0/go:unchecked-close-writerecommendedThe error of closing a file that was written is lost in a
defer -
tx0/go:unescaped-template-contentrecommendedA value computed at run time is marked as safe HTML, JavaScript or URL content for
html/template -
tx0/go:unread-errorrecommendedAn error returned by a call is stored in a variable and overwritten or dropped before anything reads it
-
tx0/go:weak-cipherrecommendedData is encrypted with DES, triple DES or RC4, which are broken or too weak
-
tx0/go:world-writable-filerecommendedThe mode of a file is changed to one that lets every user write to it
-
tx0/go:writer-not-flushedrecommendedA buffering or compressing writer is never flushed or closed, so its last bytes are lost
Strict: 62 rules
Code that can be written more plainly.
-
tx0/go:blank-range-variablestrictA
rangeclause names a blank variable that can be left out -
tx0/go:bool-literal-comparisonstrictA value is compared with
trueorfalse -
tx0/go:buffer-conversionstrictThe content of a buffer is converted between string and bytes instead of read in the wanted form
-
tx0/go:command-from-pathstrictA program is started by a bare name, so the directories of
PATHdecide which one runs -
tx0/go:compare-for-equalitystrictThe three-way
Compareofstringsorbytesis used only to test equality -
tx0/go:complex-conditionstrictA boolean expression chains more
&&and||operators than the configured maximum -
tx0/go:context-fieldstrictA struct type keeps a
context.Contextin a field -
tx0/go:context-not-firststrictA function takes a
context.Contextafter other parameters -
tx0/go:cookie-without-protectionstrictA cookie is made without
Secureor withoutHttpOnly -
tx0/go:deprecated-standard-apistrictA member of the standard library that its documentation marks as deprecated is used
-
tx0/go:else-after-returnstrictAn
elsefollows a branch that always returns, so its body could follow theifunindented -
tx0/go:empty-branchstrictA branch of an
iforelseis empty and has no comment -
tx0/go:empty-commentstrictA block comment has no text
-
tx0/go:empty-functionstrictA function has an empty body without a comment that says why
-
tx0/go:error-message-stylestrictAn error message starts with a capital letter or ends with punctuation or a newline
-
tx0/go:error-not-wrappedstrictfmt.Errorfprints an error with%vor%s, so callers cannot unwrap it -
tx0/go:errorf-without-argumentsstrictfmt.Errorfis called with a constant message and nothing to format -
tx0/go:errors-new-sprintfstricterrors.New(fmt.Sprintf(...))can be written asfmt.Errorf(...) -
tx0/go:for-truestrictA loop has the condition
true, which it needs not -
tx0/go:hardcoded-credentialstrictA password, secret or token is written into the source as a string literal
-
tx0/go:hardcoded-ip-addressstrictAn IP address is written into the source as a string literal
-
tx0/go:http-status-literalstrictAn HTTP status is written as a number instead of the constant
net/httpdefines for it -
tx0/go:identical-functionsstrictTwo functions of a file have the same parameters, results and body
-
tx0/go:if-init-only-in-conditionstrictThe init statement of an
ifnames a value that only its condition reads -
tx0/go:index-for-containsstrictThe result of
Indexis compared with -1 or 0 only to learn whether a text contains another -
tx0/go:large-switchstrictA switch has more cases than the configured maximum
-
tx0/go:legacy-octal-literalstrictAn octal number is written with a leading
0instead of the0oprefix -
tx0/go:listen-all-interfacesstrictA server listens on the unspecified address, which accepts connections on every interface
-
tx0/go:long-case-clausestrictA case of a switch has more lines of code than the configured maximum
-
tx0/go:long-filestrictA file has more lines of code than the configured maximum
-
tx0/go:long-linestrictA line is longer than the configured maximum
-
tx0/go:mergeable-casesstrictAdjacent cases of a switch have the same body
-
tx0/go:negated-equalitystrictAn equality test is negated with
!instead of using the opposite operator -
tx0/go:nested-anonymous-structstrictAnonymous struct types are nested three levels deep
-
tx0/go:nesting-weighted-complexitystrictA function's branches, weighted by how deeply they are nested, exceed the configured maximum
-
tx0/go:parenthesized-conditionstrictThe condition of an
if,fororswitchis wrapped in parentheses that Go does not need -
tx0/go:print-of-sprintfstrictA string formatted with
fmt.Sprintfis printed right away instead of formatting while printing -
tx0/go:profiling-endpoint-importstrictnet/http/pprofis imported for its side effect, which adds profiling endpoints to the default server -
tx0/go:receiver-name-mismatchstrictMethods of one type name their receiver differently, or call it
thisorself -
tx0/go:redundant-breakstrictA
breakends a case that would end anyway -
tx0/go:redundant-make-capacitystrictmakeis given a capacity or buffer size that is the default anyway -
tx0/go:redundant-returnstrictA function without results ends with a bare
return, which it would do anyway -
tx0/go:repeated-string-literalstrictThe same string literal is written more times than the configured maximum in one file
-
tx0/go:request-value-loggedstrictA value taken from an HTTP request is written to a text log as it is
-
tx0/go:rsa-pkcs1v15-encryptionstrictData is encrypted with RSA and PKCS #1 v1.5 padding, which leaks the plaintext to padding queries
-
tx0/go:scattered-importsstrictA file has more than one import declaration
-
tx0/go:semicolon-joined-statementsstrictTwo statements are written on one line, separated by a semicolon
-
tx0/go:shadowed-builtinstrictA declaration reuses the name of a predeclared function or type such as
len,copyorstring -
tx0/go:single-case-selectstrictA
selecthas a single case and nodefault, so it is a plain send or receive -
tx0/go:single-method-interface-namestrictAn interface with a single method is not named after the method with
-er -
tx0/go:slice-to-lengthstrictA slice expression gives the length as its upper bound, which is the default
-
tx0/go:stray-semicolonstrictA statement or declaration ends in a semicolon that Go inserts by itself
-
tx0/go:switch-in-casestrictA switch is written inside a case of another switch
-
tx0/go:task-commentstrictA comment marks work still to be done with
TODO,FIXME,XXXorHACK -
tx0/go:time-sub-nowstrictA difference with
time.Now()can be written withtime.Sinceortime.Until -
tx0/go:too-many-returnsstrictA function has more
returnstatements than the configured maximum -
tx0/go:underscore-namestrictA function or local variable is named with underscores instead of mixed caps
-
tx0/go:undocumented-blank-importstrictA package is imported only for its side effects without a comment that says why
-
tx0/go:unencrypted-requeststrictA request is sent to a fixed URL over an unencrypted protocol
-
tx0/go:ungrouped-parametersstrictAdjacent parameters of the same type each repeat the type instead of sharing it
-
tx0/go:weak-hashstrictMD5 or SHA-1 is used, whose collisions can be produced at will
-
tx0/go:yoda-conditionstrictA comparison puts the constant before the value it tests
: 3 rules
-
tx0/go:else-if-without-elseallAn if-else-if chain has no final
elsefor the case where no condition holds -
tx0/go:missing-license-headerallA file has no copyright or license notice before its package clause
-
tx0/go:switch-without-defaultallA switch has no
defaultcase for values that no case lists