github.com/pankona/gometalinter@v2.0.11+incompatible/_linters/src/honnef.co/go/tools/cmd/megacheck/megacheck.go (about)

     1  // megacheck runs staticcheck, gosimple and unused.
     2  package main // import "honnef.co/go/tools/cmd/megacheck"
     3  
     4  import (
     5  	"os"
     6  
     7  	"honnef.co/go/tools/lint/lintutil"
     8  	"honnef.co/go/tools/simple"
     9  	"honnef.co/go/tools/staticcheck"
    10  	"honnef.co/go/tools/unused"
    11  )
    12  
    13  func main() {
    14  	var flags struct {
    15  		staticcheck struct {
    16  			enabled     bool
    17  			generated   bool
    18  			exitNonZero bool
    19  		}
    20  		gosimple struct {
    21  			enabled     bool
    22  			generated   bool
    23  			exitNonZero bool
    24  		}
    25  		unused struct {
    26  			enabled      bool
    27  			constants    bool
    28  			fields       bool
    29  			functions    bool
    30  			types        bool
    31  			variables    bool
    32  			debug        string
    33  			wholeProgram bool
    34  			reflection   bool
    35  			exitNonZero  bool
    36  		}
    37  	}
    38  	fs := lintutil.FlagSet("megacheck")
    39  	fs.BoolVar(&flags.gosimple.enabled,
    40  		"simple.enabled", true, "Run gosimple")
    41  	fs.BoolVar(&flags.gosimple.generated,
    42  		"simple.generated", false, "Check generated code")
    43  	fs.BoolVar(&flags.gosimple.exitNonZero,
    44  		"simple.exit-non-zero", false, "Exit non-zero if any problems were found")
    45  
    46  	fs.BoolVar(&flags.staticcheck.enabled,
    47  		"staticcheck.enabled", true, "Run staticcheck")
    48  	fs.BoolVar(&flags.staticcheck.generated,
    49  		"staticcheck.generated", false, "Check generated code (only applies to a subset of checks)")
    50  	fs.BoolVar(&flags.staticcheck.exitNonZero,
    51  		"staticcheck.exit-non-zero", true, "Exit non-zero if any problems were found")
    52  
    53  	fs.BoolVar(&flags.unused.enabled,
    54  		"unused.enabled", true, "Run unused")
    55  	fs.BoolVar(&flags.unused.constants,
    56  		"unused.consts", true, "Report unused constants")
    57  	fs.BoolVar(&flags.unused.fields,
    58  		"unused.fields", true, "Report unused fields")
    59  	fs.BoolVar(&flags.unused.functions,
    60  		"unused.funcs", true, "Report unused functions and methods")
    61  	fs.BoolVar(&flags.unused.types,
    62  		"unused.types", true, "Report unused types")
    63  	fs.BoolVar(&flags.unused.variables,
    64  		"unused.vars", true, "Report unused variables")
    65  	fs.BoolVar(&flags.unused.wholeProgram,
    66  		"unused.exported", false, "Treat arguments as a program and report unused exported identifiers")
    67  	fs.BoolVar(&flags.unused.reflection,
    68  		"unused.reflect", true, "Consider identifiers as used when it's likely they'll be accessed via reflection")
    69  	fs.BoolVar(&flags.unused.exitNonZero,
    70  		"unused.exit-non-zero", true, "Exit non-zero if any problems were found")
    71  
    72  	fs.Parse(os.Args[1:])
    73  
    74  	var checkers []lintutil.CheckerConfig
    75  
    76  	if flags.staticcheck.enabled {
    77  		sac := staticcheck.NewChecker()
    78  		sac.CheckGenerated = flags.staticcheck.generated
    79  		checkers = append(checkers, lintutil.CheckerConfig{
    80  			Checker:     sac,
    81  			ExitNonZero: flags.staticcheck.exitNonZero,
    82  		})
    83  	}
    84  
    85  	if flags.gosimple.enabled {
    86  		sc := simple.NewChecker()
    87  		sc.CheckGenerated = flags.gosimple.generated
    88  		checkers = append(checkers, lintutil.CheckerConfig{
    89  			Checker:     sc,
    90  			ExitNonZero: flags.gosimple.exitNonZero,
    91  		})
    92  	}
    93  
    94  	if flags.unused.enabled {
    95  		var mode unused.CheckMode
    96  		if flags.unused.constants {
    97  			mode |= unused.CheckConstants
    98  		}
    99  		if flags.unused.fields {
   100  			mode |= unused.CheckFields
   101  		}
   102  		if flags.unused.functions {
   103  			mode |= unused.CheckFunctions
   104  		}
   105  		if flags.unused.types {
   106  			mode |= unused.CheckTypes
   107  		}
   108  		if flags.unused.variables {
   109  			mode |= unused.CheckVariables
   110  		}
   111  		uc := unused.NewChecker(mode)
   112  		uc.WholeProgram = flags.unused.wholeProgram
   113  		uc.ConsiderReflection = flags.unused.reflection
   114  		checkers = append(checkers, lintutil.CheckerConfig{
   115  			Checker:     unused.NewLintChecker(uc),
   116  			ExitNonZero: flags.unused.exitNonZero,
   117  		})
   118  
   119  	}
   120  
   121  	lintutil.ProcessFlagSet(checkers, fs)
   122  }