tetragon icon indicating copy to clipboard operation
tetragon copied to clipboard

cleanup: add new linters and fix bugs and alerts

Open mtardy opened this issue 2 years ago • 2 comments

This is a follow-up of https://github.com/cilium/tetragon/pull/976 to enable new linters to increase the code quality, remove bugs and improve readability in a sustainable way.

Add the following linters and fix their alerts:

  • gosimple: Linter for Go source code that specializes in simplifying code
  • typecheck: Like the front-end of a Go compiler, parses and type-checks Go code
  • asciicheck: Simple linter to check that your code does not contain non-ASCII identifiers
  • bidichk: Checks for dangerous unicode character sequences
  • makezero: Finds slice declarations with non-zero initial length (found bugs)
  • loggercheck: Checks key value pairs for common logger libraries (kitlog,klog,logr,zap). (found bugs)
  • exportloopref: Checks for pointers to enclosing loop variables. (found bugs)
  • dupword: Checks for duplicate words in the source code
  • gofmt: Gofmt checks whether code was gofmt-ed. By default this tool runs with -s option to check for code simplification.
  • errname: Checks that sentinel errors are prefixed with the Err and error types are suffixed with the Error.
  • gocritic: Provides diagnostics that check for bugs, performance and style issues. Extensible without recompilation through dynamic rules. Dynamic rules are written declaratively with AST patterns, filters, report message and optional suggestion.
  • unparam: Reports unused function parameters

I would love to enable these ones about errors, but those will be a followup cleanup PR since this is a lot of work:

  • errcheck (this one is particularly important)
  • nilnil
  • nilerr
  • errorlint

We still don't catch situations where we use the return value before checking the error.

Added this issue for follow-up https://github.com/cilium/tetragon/issues/989.

Just for info the pattern used here, that defaults to error and rely on the function to return nil and override is a bit weird and led to an issue while removing useless "always nil" return value from functions: https://github.com/cilium/tetragon/blob/557a6c93b1f71aed0af6da3c94323867426b2e97/pkg/sensors/manager.go#L60

mtardy avatar May 09 '23 15:05 mtardy

Btw I'm also not a fan of that "BUG in SensorCtl" thing. Maybe we can refactor that slightly in the future.

willfindlay avatar May 11 '23 12:05 willfindlay

LGTM just couple questions/nits.

waw thanks a lot for that fast and good review! :)

mtardy avatar May 11 '23 14:05 mtardy