github.com/wgliang/gometalinter@v2.0.6-0.20180523041418-a75adcf7cb0e+incompatible/_linters/src/honnef.co/go/tools/cmd/unused/main.go (about)

     1  // unused reports unused identifiers (types, functions, ...) in your
     2  // code.
     3  package main // import "honnef.co/go/tools/cmd/unused"
     4  
     5  import (
     6  	"log"
     7  	"os"
     8  
     9  	"honnef.co/go/tools/lint/lintutil"
    10  	"honnef.co/go/tools/unused"
    11  )
    12  
    13  var (
    14  	fConstants    bool
    15  	fFields       bool
    16  	fFunctions    bool
    17  	fTypes        bool
    18  	fVariables    bool
    19  	fDebug        string
    20  	fWholeProgram bool
    21  	fReflection   bool
    22  )
    23  
    24  func newChecker(mode unused.CheckMode) *unused.Checker {
    25  	checker := unused.NewChecker(mode)
    26  
    27  	if fDebug != "" {
    28  		debug, err := os.Create(fDebug)
    29  		if err != nil {
    30  			log.Fatal("couldn't open debug file:", err)
    31  		}
    32  		checker.Debug = debug
    33  	}
    34  
    35  	checker.WholeProgram = fWholeProgram
    36  	checker.ConsiderReflection = fReflection
    37  	return checker
    38  }
    39  
    40  func main() {
    41  	log.SetFlags(0)
    42  
    43  	fs := lintutil.FlagSet("unused")
    44  	fs.BoolVar(&fConstants, "consts", true, "Report unused constants")
    45  	fs.BoolVar(&fFields, "fields", true, "Report unused fields")
    46  	fs.BoolVar(&fFunctions, "funcs", true, "Report unused functions and methods")
    47  	fs.BoolVar(&fTypes, "types", true, "Report unused types")
    48  	fs.BoolVar(&fVariables, "vars", true, "Report unused variables")
    49  	fs.StringVar(&fDebug, "debug", "", "Write a debug graph to `file`. Existing files will be overwritten.")
    50  	fs.BoolVar(&fWholeProgram, "exported", false, "Treat arguments as a program and report unused exported identifiers")
    51  	fs.BoolVar(&fReflection, "reflect", true, "Consider identifiers as used when it's likely they'll be accessed via reflection")
    52  	fs.Parse(os.Args[1:])
    53  
    54  	var mode unused.CheckMode
    55  	if fConstants {
    56  		mode |= unused.CheckConstants
    57  	}
    58  	if fFields {
    59  		mode |= unused.CheckFields
    60  	}
    61  	if fFunctions {
    62  		mode |= unused.CheckFunctions
    63  	}
    64  	if fTypes {
    65  		mode |= unused.CheckTypes
    66  	}
    67  	if fVariables {
    68  		mode |= unused.CheckVariables
    69  	}
    70  
    71  	checker := newChecker(mode)
    72  	l := unused.NewLintChecker(checker)
    73  	cfg := lintutil.CheckerConfig{
    74  		Checker:     l,
    75  		ExitNonZero: true,
    76  	}
    77  	lintutil.ProcessFlagSet([]lintutil.CheckerConfig{cfg}, fs)
    78  }