github.com/golangci/go-tools@v0.0.0-20190318060251-af6baa5dc196/cmd/unused/main.go (about)

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