lint.go 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361
  1. // Package staticcheck contains a linter for Go source code.
  2. package staticcheck // import "honnef.co/go/tools/staticcheck"
  3. import (
  4. "fmt"
  5. "go/ast"
  6. "go/constant"
  7. "go/token"
  8. "go/types"
  9. htmltemplate "html/template"
  10. "net/http"
  11. "reflect"
  12. "regexp"
  13. "regexp/syntax"
  14. "sort"
  15. "strconv"
  16. "strings"
  17. texttemplate "text/template"
  18. "unicode"
  19. . "honnef.co/go/tools/arg"
  20. "honnef.co/go/tools/deprecated"
  21. "honnef.co/go/tools/facts"
  22. "honnef.co/go/tools/functions"
  23. "honnef.co/go/tools/internal/passes/buildssa"
  24. "honnef.co/go/tools/internal/sharedcheck"
  25. "honnef.co/go/tools/lint"
  26. . "honnef.co/go/tools/lint/lintdsl"
  27. "honnef.co/go/tools/printf"
  28. "honnef.co/go/tools/ssa"
  29. "honnef.co/go/tools/ssautil"
  30. "honnef.co/go/tools/staticcheck/vrp"
  31. "golang.org/x/tools/go/analysis"
  32. "golang.org/x/tools/go/analysis/passes/inspect"
  33. "golang.org/x/tools/go/ast/astutil"
  34. "golang.org/x/tools/go/ast/inspector"
  35. "golang.org/x/tools/go/types/typeutil"
  36. )
  37. func validRegexp(call *Call) {
  38. arg := call.Args[0]
  39. err := ValidateRegexp(arg.Value)
  40. if err != nil {
  41. arg.Invalid(err.Error())
  42. }
  43. }
  44. type runeSlice []rune
  45. func (rs runeSlice) Len() int { return len(rs) }
  46. func (rs runeSlice) Less(i int, j int) bool { return rs[i] < rs[j] }
  47. func (rs runeSlice) Swap(i int, j int) { rs[i], rs[j] = rs[j], rs[i] }
  48. func utf8Cutset(call *Call) {
  49. arg := call.Args[1]
  50. if InvalidUTF8(arg.Value) {
  51. arg.Invalid(MsgInvalidUTF8)
  52. }
  53. }
  54. func uniqueCutset(call *Call) {
  55. arg := call.Args[1]
  56. if !UniqueStringCutset(arg.Value) {
  57. arg.Invalid(MsgNonUniqueCutset)
  58. }
  59. }
  60. func unmarshalPointer(name string, arg int) CallCheck {
  61. return func(call *Call) {
  62. if !Pointer(call.Args[arg].Value) {
  63. call.Args[arg].Invalid(fmt.Sprintf("%s expects to unmarshal into a pointer, but the provided value is not a pointer", name))
  64. }
  65. }
  66. }
  67. func pointlessIntMath(call *Call) {
  68. if ConvertedFromInt(call.Args[0].Value) {
  69. call.Invalid(fmt.Sprintf("calling %s on a converted integer is pointless", CallName(call.Instr.Common())))
  70. }
  71. }
  72. func checkValidHostPort(arg int) CallCheck {
  73. return func(call *Call) {
  74. if !ValidHostPort(call.Args[arg].Value) {
  75. call.Args[arg].Invalid(MsgInvalidHostPort)
  76. }
  77. }
  78. }
  79. var (
  80. checkRegexpRules = map[string]CallCheck{
  81. "regexp.MustCompile": validRegexp,
  82. "regexp.Compile": validRegexp,
  83. "regexp.Match": validRegexp,
  84. "regexp.MatchReader": validRegexp,
  85. "regexp.MatchString": validRegexp,
  86. }
  87. checkTimeParseRules = map[string]CallCheck{
  88. "time.Parse": func(call *Call) {
  89. arg := call.Args[Arg("time.Parse.layout")]
  90. err := ValidateTimeLayout(arg.Value)
  91. if err != nil {
  92. arg.Invalid(err.Error())
  93. }
  94. },
  95. }
  96. checkEncodingBinaryRules = map[string]CallCheck{
  97. "encoding/binary.Write": func(call *Call) {
  98. arg := call.Args[Arg("encoding/binary.Write.data")]
  99. if !CanBinaryMarshal(call.Pass, arg.Value) {
  100. arg.Invalid(fmt.Sprintf("value of type %s cannot be used with binary.Write", arg.Value.Value.Type()))
  101. }
  102. },
  103. }
  104. checkURLsRules = map[string]CallCheck{
  105. "net/url.Parse": func(call *Call) {
  106. arg := call.Args[Arg("net/url.Parse.rawurl")]
  107. err := ValidateURL(arg.Value)
  108. if err != nil {
  109. arg.Invalid(err.Error())
  110. }
  111. },
  112. }
  113. checkSyncPoolValueRules = map[string]CallCheck{
  114. "(*sync.Pool).Put": func(call *Call) {
  115. arg := call.Args[Arg("(*sync.Pool).Put.x")]
  116. typ := arg.Value.Value.Type()
  117. if !IsPointerLike(typ) {
  118. arg.Invalid("argument should be pointer-like to avoid allocations")
  119. }
  120. },
  121. }
  122. checkRegexpFindAllRules = map[string]CallCheck{
  123. "(*regexp.Regexp).FindAll": RepeatZeroTimes("a FindAll method", 1),
  124. "(*regexp.Regexp).FindAllIndex": RepeatZeroTimes("a FindAll method", 1),
  125. "(*regexp.Regexp).FindAllString": RepeatZeroTimes("a FindAll method", 1),
  126. "(*regexp.Regexp).FindAllStringIndex": RepeatZeroTimes("a FindAll method", 1),
  127. "(*regexp.Regexp).FindAllStringSubmatch": RepeatZeroTimes("a FindAll method", 1),
  128. "(*regexp.Regexp).FindAllStringSubmatchIndex": RepeatZeroTimes("a FindAll method", 1),
  129. "(*regexp.Regexp).FindAllSubmatch": RepeatZeroTimes("a FindAll method", 1),
  130. "(*regexp.Regexp).FindAllSubmatchIndex": RepeatZeroTimes("a FindAll method", 1),
  131. }
  132. checkUTF8CutsetRules = map[string]CallCheck{
  133. "strings.IndexAny": utf8Cutset,
  134. "strings.LastIndexAny": utf8Cutset,
  135. "strings.ContainsAny": utf8Cutset,
  136. "strings.Trim": utf8Cutset,
  137. "strings.TrimLeft": utf8Cutset,
  138. "strings.TrimRight": utf8Cutset,
  139. }
  140. checkUniqueCutsetRules = map[string]CallCheck{
  141. "strings.Trim": uniqueCutset,
  142. "strings.TrimLeft": uniqueCutset,
  143. "strings.TrimRight": uniqueCutset,
  144. }
  145. checkUnmarshalPointerRules = map[string]CallCheck{
  146. "encoding/xml.Unmarshal": unmarshalPointer("xml.Unmarshal", 1),
  147. "(*encoding/xml.Decoder).Decode": unmarshalPointer("Decode", 0),
  148. "(*encoding/xml.Decoder).DecodeElement": unmarshalPointer("DecodeElement", 0),
  149. "encoding/json.Unmarshal": unmarshalPointer("json.Unmarshal", 1),
  150. "(*encoding/json.Decoder).Decode": unmarshalPointer("Decode", 0),
  151. }
  152. checkUnbufferedSignalChanRules = map[string]CallCheck{
  153. "os/signal.Notify": func(call *Call) {
  154. arg := call.Args[Arg("os/signal.Notify.c")]
  155. if UnbufferedChannel(arg.Value) {
  156. arg.Invalid("the channel used with signal.Notify should be buffered")
  157. }
  158. },
  159. }
  160. checkMathIntRules = map[string]CallCheck{
  161. "math.Ceil": pointlessIntMath,
  162. "math.Floor": pointlessIntMath,
  163. "math.IsNaN": pointlessIntMath,
  164. "math.Trunc": pointlessIntMath,
  165. "math.IsInf": pointlessIntMath,
  166. }
  167. checkStringsReplaceZeroRules = map[string]CallCheck{
  168. "strings.Replace": RepeatZeroTimes("strings.Replace", 3),
  169. "bytes.Replace": RepeatZeroTimes("bytes.Replace", 3),
  170. }
  171. checkListenAddressRules = map[string]CallCheck{
  172. "net/http.ListenAndServe": checkValidHostPort(0),
  173. "net/http.ListenAndServeTLS": checkValidHostPort(0),
  174. }
  175. checkBytesEqualIPRules = map[string]CallCheck{
  176. "bytes.Equal": func(call *Call) {
  177. if ConvertedFrom(call.Args[Arg("bytes.Equal.a")].Value, "net.IP") &&
  178. ConvertedFrom(call.Args[Arg("bytes.Equal.b")].Value, "net.IP") {
  179. call.Invalid("use net.IP.Equal to compare net.IPs, not bytes.Equal")
  180. }
  181. },
  182. }
  183. checkRegexpMatchLoopRules = map[string]CallCheck{
  184. "regexp.Match": loopedRegexp("regexp.Match"),
  185. "regexp.MatchReader": loopedRegexp("regexp.MatchReader"),
  186. "regexp.MatchString": loopedRegexp("regexp.MatchString"),
  187. }
  188. checkNoopMarshal = map[string]CallCheck{
  189. // TODO(dh): should we really flag XML? Even an empty struct
  190. // produces a non-zero amount of data, namely its type name.
  191. // Let's see if we encounter any false positives.
  192. //
  193. // Also, should we flag gob?
  194. "encoding/json.Marshal": checkNoopMarshalImpl(Arg("json.Marshal.v"), "MarshalJSON", "MarshalText"),
  195. "encoding/xml.Marshal": checkNoopMarshalImpl(Arg("xml.Marshal.v"), "MarshalXML", "MarshalText"),
  196. "(*encoding/json.Encoder).Encode": checkNoopMarshalImpl(Arg("(*encoding/json.Encoder).Encode.v"), "MarshalJSON", "MarshalText"),
  197. "(*encoding/xml.Encoder).Encode": checkNoopMarshalImpl(Arg("(*encoding/xml.Encoder).Encode.v"), "MarshalXML", "MarshalText"),
  198. "encoding/json.Unmarshal": checkNoopMarshalImpl(Arg("json.Unmarshal.v"), "UnmarshalJSON", "UnmarshalText"),
  199. "encoding/xml.Unmarshal": checkNoopMarshalImpl(Arg("xml.Unmarshal.v"), "UnmarshalXML", "UnmarshalText"),
  200. "(*encoding/json.Decoder).Decode": checkNoopMarshalImpl(Arg("(*encoding/json.Decoder).Decode.v"), "UnmarshalJSON", "UnmarshalText"),
  201. "(*encoding/xml.Decoder).Decode": checkNoopMarshalImpl(Arg("(*encoding/xml.Decoder).Decode.v"), "UnmarshalXML", "UnmarshalText"),
  202. }
  203. checkUnsupportedMarshal = map[string]CallCheck{
  204. "encoding/json.Marshal": checkUnsupportedMarshalImpl(Arg("json.Marshal.v"), "json", "MarshalJSON", "MarshalText"),
  205. "encoding/xml.Marshal": checkUnsupportedMarshalImpl(Arg("xml.Marshal.v"), "xml", "MarshalXML", "MarshalText"),
  206. "(*encoding/json.Encoder).Encode": checkUnsupportedMarshalImpl(Arg("(*encoding/json.Encoder).Encode.v"), "json", "MarshalJSON", "MarshalText"),
  207. "(*encoding/xml.Encoder).Encode": checkUnsupportedMarshalImpl(Arg("(*encoding/xml.Encoder).Encode.v"), "xml", "MarshalXML", "MarshalText"),
  208. }
  209. checkAtomicAlignment = map[string]CallCheck{
  210. "sync/atomic.AddInt64": checkAtomicAlignmentImpl,
  211. "sync/atomic.AddUint64": checkAtomicAlignmentImpl,
  212. "sync/atomic.CompareAndSwapInt64": checkAtomicAlignmentImpl,
  213. "sync/atomic.CompareAndSwapUint64": checkAtomicAlignmentImpl,
  214. "sync/atomic.LoadInt64": checkAtomicAlignmentImpl,
  215. "sync/atomic.LoadUint64": checkAtomicAlignmentImpl,
  216. "sync/atomic.StoreInt64": checkAtomicAlignmentImpl,
  217. "sync/atomic.StoreUint64": checkAtomicAlignmentImpl,
  218. "sync/atomic.SwapInt64": checkAtomicAlignmentImpl,
  219. "sync/atomic.SwapUint64": checkAtomicAlignmentImpl,
  220. }
  221. // TODO(dh): detect printf wrappers
  222. checkPrintfRules = map[string]CallCheck{
  223. "fmt.Errorf": func(call *Call) { checkPrintfCall(call, 0, 1) },
  224. "fmt.Printf": func(call *Call) { checkPrintfCall(call, 0, 1) },
  225. "fmt.Sprintf": func(call *Call) { checkPrintfCall(call, 0, 1) },
  226. "fmt.Fprintf": func(call *Call) { checkPrintfCall(call, 1, 2) },
  227. }
  228. )
  229. func checkPrintfCall(call *Call, fIdx, vIdx int) {
  230. f := call.Args[fIdx]
  231. var args []ssa.Value
  232. switch v := call.Args[vIdx].Value.Value.(type) {
  233. case *ssa.Slice:
  234. var ok bool
  235. args, ok = ssautil.Vararg(v)
  236. if !ok {
  237. // We don't know what the actual arguments to the function are
  238. return
  239. }
  240. case *ssa.Const:
  241. // nil, i.e. no arguments
  242. default:
  243. // We don't know what the actual arguments to the function are
  244. return
  245. }
  246. checkPrintfCallImpl(call, f.Value.Value, args)
  247. }
  248. type verbFlag int
  249. const (
  250. isInt verbFlag = 1 << iota
  251. isBool
  252. isFP
  253. isString
  254. isPointer
  255. isPseudoPointer
  256. isSlice
  257. isAny
  258. noRecurse
  259. )
  260. var verbs = [...]verbFlag{
  261. 'b': isPseudoPointer | isInt | isFP,
  262. 'c': isInt,
  263. 'd': isPseudoPointer | isInt,
  264. 'e': isFP,
  265. 'E': isFP,
  266. 'f': isFP,
  267. 'F': isFP,
  268. 'g': isFP,
  269. 'G': isFP,
  270. 'o': isPseudoPointer | isInt,
  271. 'p': isSlice | isPointer | noRecurse,
  272. 'q': isInt | isString,
  273. 's': isString,
  274. 't': isBool,
  275. 'T': isAny,
  276. 'U': isInt,
  277. 'v': isAny,
  278. 'X': isPseudoPointer | isInt | isString,
  279. 'x': isPseudoPointer | isInt | isString,
  280. }
  281. func checkPrintfCallImpl(call *Call, f ssa.Value, args []ssa.Value) {
  282. var msCache *typeutil.MethodSetCache
  283. if f.Parent() != nil {
  284. msCache = &f.Parent().Prog.MethodSets
  285. }
  286. elem := func(T types.Type, verb rune) ([]types.Type, bool) {
  287. if verbs[verb]&noRecurse != 0 {
  288. return []types.Type{T}, false
  289. }
  290. switch T := T.(type) {
  291. case *types.Slice:
  292. if verbs[verb]&isSlice != 0 {
  293. return []types.Type{T}, false
  294. }
  295. if verbs[verb]&isString != 0 && IsType(T.Elem().Underlying(), "byte") {
  296. return []types.Type{T}, false
  297. }
  298. return []types.Type{T.Elem()}, true
  299. case *types.Map:
  300. key := T.Key()
  301. val := T.Elem()
  302. return []types.Type{key, val}, true
  303. case *types.Struct:
  304. out := make([]types.Type, 0, T.NumFields())
  305. for i := 0; i < T.NumFields(); i++ {
  306. out = append(out, T.Field(i).Type())
  307. }
  308. return out, true
  309. case *types.Array:
  310. return []types.Type{T.Elem()}, true
  311. default:
  312. return []types.Type{T}, false
  313. }
  314. }
  315. isInfo := func(T types.Type, info types.BasicInfo) bool {
  316. basic, ok := T.Underlying().(*types.Basic)
  317. return ok && basic.Info()&info != 0
  318. }
  319. isStringer := func(T types.Type, ms *types.MethodSet) bool {
  320. sel := ms.Lookup(nil, "String")
  321. if sel == nil {
  322. return false
  323. }
  324. fn, ok := sel.Obj().(*types.Func)
  325. if !ok {
  326. // should be unreachable
  327. return false
  328. }
  329. sig := fn.Type().(*types.Signature)
  330. if sig.Params().Len() != 0 {
  331. return false
  332. }
  333. if sig.Results().Len() != 1 {
  334. return false
  335. }
  336. if !IsType(sig.Results().At(0).Type(), "string") {
  337. return false
  338. }
  339. return true
  340. }
  341. isError := func(T types.Type, ms *types.MethodSet) bool {
  342. sel := ms.Lookup(nil, "Error")
  343. if sel == nil {
  344. return false
  345. }
  346. fn, ok := sel.Obj().(*types.Func)
  347. if !ok {
  348. // should be unreachable
  349. return false
  350. }
  351. sig := fn.Type().(*types.Signature)
  352. if sig.Params().Len() != 0 {
  353. return false
  354. }
  355. if sig.Results().Len() != 1 {
  356. return false
  357. }
  358. if !IsType(sig.Results().At(0).Type(), "string") {
  359. return false
  360. }
  361. return true
  362. }
  363. isFormatter := func(T types.Type, ms *types.MethodSet) bool {
  364. sel := ms.Lookup(nil, "Format")
  365. if sel == nil {
  366. return false
  367. }
  368. fn, ok := sel.Obj().(*types.Func)
  369. if !ok {
  370. // should be unreachable
  371. return false
  372. }
  373. sig := fn.Type().(*types.Signature)
  374. if sig.Params().Len() != 2 {
  375. return false
  376. }
  377. // TODO(dh): check the types of the arguments for more
  378. // precision
  379. if sig.Results().Len() != 0 {
  380. return false
  381. }
  382. return true
  383. }
  384. seen := map[types.Type]bool{}
  385. var checkType func(verb rune, T types.Type, top bool) bool
  386. checkType = func(verb rune, T types.Type, top bool) bool {
  387. if top {
  388. for k := range seen {
  389. delete(seen, k)
  390. }
  391. }
  392. if seen[T] {
  393. return true
  394. }
  395. seen[T] = true
  396. if int(verb) >= len(verbs) {
  397. // Unknown verb
  398. return true
  399. }
  400. flags := verbs[verb]
  401. if flags == 0 {
  402. // Unknown verb
  403. return true
  404. }
  405. ms := msCache.MethodSet(T)
  406. if isFormatter(T, ms) {
  407. // the value is responsible for formatting itself
  408. return true
  409. }
  410. if flags&isString != 0 && (isStringer(T, ms) || isError(T, ms)) {
  411. // Check for stringer early because we're about to dereference
  412. return true
  413. }
  414. T = T.Underlying()
  415. if flags&(isPointer|isPseudoPointer) == 0 && top {
  416. T = Dereference(T)
  417. }
  418. if flags&isPseudoPointer != 0 && top {
  419. t := Dereference(T)
  420. if _, ok := t.Underlying().(*types.Struct); ok {
  421. T = t
  422. }
  423. }
  424. if _, ok := T.(*types.Interface); ok {
  425. // We don't know what's in the interface
  426. return true
  427. }
  428. var info types.BasicInfo
  429. if flags&isInt != 0 {
  430. info |= types.IsInteger
  431. }
  432. if flags&isBool != 0 {
  433. info |= types.IsBoolean
  434. }
  435. if flags&isFP != 0 {
  436. info |= types.IsFloat | types.IsComplex
  437. }
  438. if flags&isString != 0 {
  439. info |= types.IsString
  440. }
  441. if info != 0 && isInfo(T, info) {
  442. return true
  443. }
  444. if flags&isString != 0 && (IsType(T, "[]byte") || isStringer(T, ms) || isError(T, ms)) {
  445. return true
  446. }
  447. if flags&isPointer != 0 && IsPointerLike(T) {
  448. return true
  449. }
  450. if flags&isPseudoPointer != 0 {
  451. switch U := T.Underlying().(type) {
  452. case *types.Pointer:
  453. if !top {
  454. return true
  455. }
  456. if _, ok := U.Elem().Underlying().(*types.Struct); !ok {
  457. return true
  458. }
  459. case *types.Chan, *types.Signature:
  460. return true
  461. }
  462. }
  463. if flags&isSlice != 0 {
  464. if _, ok := T.(*types.Slice); ok {
  465. return true
  466. }
  467. }
  468. if flags&isAny != 0 {
  469. return true
  470. }
  471. elems, ok := elem(T.Underlying(), verb)
  472. if !ok {
  473. return false
  474. }
  475. for _, elem := range elems {
  476. if !checkType(verb, elem, false) {
  477. return false
  478. }
  479. }
  480. return true
  481. }
  482. k, ok := f.(*ssa.Const)
  483. if !ok {
  484. return
  485. }
  486. actions, err := printf.Parse(constant.StringVal(k.Value))
  487. if err != nil {
  488. call.Invalid("couldn't parse format string")
  489. return
  490. }
  491. ptr := 1
  492. hasExplicit := false
  493. checkStar := func(verb printf.Verb, star printf.Argument) bool {
  494. if star, ok := star.(printf.Star); ok {
  495. idx := 0
  496. if star.Index == -1 {
  497. idx = ptr
  498. ptr++
  499. } else {
  500. hasExplicit = true
  501. idx = star.Index
  502. ptr = star.Index + 1
  503. }
  504. if idx == 0 {
  505. call.Invalid(fmt.Sprintf("Printf format %s reads invalid arg 0; indices are 1-based", verb.Raw))
  506. return false
  507. }
  508. if idx > len(args) {
  509. call.Invalid(
  510. fmt.Sprintf("Printf format %s reads arg #%d, but call has only %d args",
  511. verb.Raw, idx, len(args)))
  512. return false
  513. }
  514. if arg, ok := args[idx-1].(*ssa.MakeInterface); ok {
  515. if !isInfo(arg.X.Type(), types.IsInteger) {
  516. call.Invalid(fmt.Sprintf("Printf format %s reads non-int arg #%d as argument of *", verb.Raw, idx))
  517. }
  518. }
  519. }
  520. return true
  521. }
  522. // We only report one problem per format string. Making a
  523. // mistake with an index tends to invalidate all future
  524. // implicit indices.
  525. for _, action := range actions {
  526. verb, ok := action.(printf.Verb)
  527. if !ok {
  528. continue
  529. }
  530. if !checkStar(verb, verb.Width) || !checkStar(verb, verb.Precision) {
  531. return
  532. }
  533. off := ptr
  534. if verb.Value != -1 {
  535. hasExplicit = true
  536. off = verb.Value
  537. }
  538. if off > len(args) {
  539. call.Invalid(
  540. fmt.Sprintf("Printf format %s reads arg #%d, but call has only %d args",
  541. verb.Raw, off, len(args)))
  542. return
  543. } else if verb.Value == 0 && verb.Letter != '%' {
  544. call.Invalid(fmt.Sprintf("Printf format %s reads invalid arg 0; indices are 1-based", verb.Raw))
  545. return
  546. } else if off != 0 {
  547. arg, ok := args[off-1].(*ssa.MakeInterface)
  548. if ok {
  549. if !checkType(verb.Letter, arg.X.Type(), true) {
  550. call.Invalid(fmt.Sprintf("Printf format %s has arg #%d of wrong type %s",
  551. verb.Raw, ptr, args[ptr-1].(*ssa.MakeInterface).X.Type()))
  552. return
  553. }
  554. }
  555. }
  556. switch verb.Value {
  557. case -1:
  558. // Consume next argument
  559. ptr++
  560. case 0:
  561. // Don't consume any arguments
  562. default:
  563. ptr = verb.Value + 1
  564. }
  565. }
  566. if !hasExplicit && ptr <= len(args) {
  567. call.Invalid(fmt.Sprintf("Printf call needs %d args but has %d args", ptr-1, len(args)))
  568. }
  569. }
  570. func checkAtomicAlignmentImpl(call *Call) {
  571. sizes := call.Pass.TypesSizes
  572. if sizes.Sizeof(types.Typ[types.Uintptr]) != 4 {
  573. // Not running on a 32-bit platform
  574. return
  575. }
  576. v, ok := call.Args[0].Value.Value.(*ssa.FieldAddr)
  577. if !ok {
  578. // TODO(dh): also check indexing into arrays and slices
  579. return
  580. }
  581. T := v.X.Type().Underlying().(*types.Pointer).Elem().Underlying().(*types.Struct)
  582. fields := make([]*types.Var, 0, T.NumFields())
  583. for i := 0; i < T.NumFields() && i <= v.Field; i++ {
  584. fields = append(fields, T.Field(i))
  585. }
  586. off := sizes.Offsetsof(fields)[v.Field]
  587. if off%8 != 0 {
  588. msg := fmt.Sprintf("address of non 64-bit aligned field %s passed to %s",
  589. T.Field(v.Field).Name(),
  590. CallName(call.Instr.Common()))
  591. call.Invalid(msg)
  592. }
  593. }
  594. func checkNoopMarshalImpl(argN int, meths ...string) CallCheck {
  595. return func(call *Call) {
  596. if IsGenerated(call.Pass, call.Instr.Pos()) {
  597. return
  598. }
  599. arg := call.Args[argN]
  600. T := arg.Value.Value.Type()
  601. Ts, ok := Dereference(T).Underlying().(*types.Struct)
  602. if !ok {
  603. return
  604. }
  605. if Ts.NumFields() == 0 {
  606. return
  607. }
  608. fields := FlattenFields(Ts)
  609. for _, field := range fields {
  610. if field.Var.Exported() {
  611. return
  612. }
  613. }
  614. // OPT(dh): we could use a method set cache here
  615. ms := call.Instr.Parent().Prog.MethodSets.MethodSet(T)
  616. // TODO(dh): we're not checking the signature, which can cause false negatives.
  617. // This isn't a huge problem, however, since vet complains about incorrect signatures.
  618. for _, meth := range meths {
  619. if ms.Lookup(nil, meth) != nil {
  620. return
  621. }
  622. }
  623. arg.Invalid("struct doesn't have any exported fields, nor custom marshaling")
  624. }
  625. }
  626. func checkUnsupportedMarshalImpl(argN int, tag string, meths ...string) CallCheck {
  627. // TODO(dh): flag slices and maps of unsupported types
  628. return func(call *Call) {
  629. msCache := &call.Instr.Parent().Prog.MethodSets
  630. arg := call.Args[argN]
  631. T := arg.Value.Value.Type()
  632. Ts, ok := Dereference(T).Underlying().(*types.Struct)
  633. if !ok {
  634. return
  635. }
  636. ms := msCache.MethodSet(T)
  637. // TODO(dh): we're not checking the signature, which can cause false negatives.
  638. // This isn't a huge problem, however, since vet complains about incorrect signatures.
  639. for _, meth := range meths {
  640. if ms.Lookup(nil, meth) != nil {
  641. return
  642. }
  643. }
  644. fields := FlattenFields(Ts)
  645. for _, field := range fields {
  646. if !(field.Var.Exported()) {
  647. continue
  648. }
  649. if reflect.StructTag(field.Tag).Get(tag) == "-" {
  650. continue
  651. }
  652. ms := msCache.MethodSet(field.Var.Type())
  653. // TODO(dh): we're not checking the signature, which can cause false negatives.
  654. // This isn't a huge problem, however, since vet complains about incorrect signatures.
  655. for _, meth := range meths {
  656. if ms.Lookup(nil, meth) != nil {
  657. return
  658. }
  659. }
  660. switch field.Var.Type().Underlying().(type) {
  661. case *types.Chan, *types.Signature:
  662. arg.Invalid(fmt.Sprintf("trying to marshal chan or func value, field %s", fieldPath(T, field.Path)))
  663. }
  664. }
  665. }
  666. }
  667. func fieldPath(start types.Type, indices []int) string {
  668. p := start.String()
  669. for _, idx := range indices {
  670. field := Dereference(start).Underlying().(*types.Struct).Field(idx)
  671. start = field.Type()
  672. p += "." + field.Name()
  673. }
  674. return p
  675. }
  676. func isInLoop(b *ssa.BasicBlock) bool {
  677. sets := functions.FindLoops(b.Parent())
  678. for _, set := range sets {
  679. if set.Has(b) {
  680. return true
  681. }
  682. }
  683. return false
  684. }
  685. func CheckUntrappableSignal(pass *analysis.Pass) (interface{}, error) {
  686. fn := func(node ast.Node) {
  687. call := node.(*ast.CallExpr)
  688. if !IsCallToAnyAST(pass, call,
  689. "os/signal.Ignore", "os/signal.Notify", "os/signal.Reset") {
  690. return
  691. }
  692. for _, arg := range call.Args {
  693. if conv, ok := arg.(*ast.CallExpr); ok && isName(pass, conv.Fun, "os.Signal") {
  694. arg = conv.Args[0]
  695. }
  696. if isName(pass, arg, "os.Kill") || isName(pass, arg, "syscall.SIGKILL") {
  697. ReportNodef(pass, arg, "%s cannot be trapped (did you mean syscall.SIGTERM?)", Render(pass, arg))
  698. }
  699. if isName(pass, arg, "syscall.SIGSTOP") {
  700. ReportNodef(pass, arg, "%s signal cannot be trapped", Render(pass, arg))
  701. }
  702. }
  703. }
  704. pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.CallExpr)(nil)}, fn)
  705. return nil, nil
  706. }
  707. func CheckTemplate(pass *analysis.Pass) (interface{}, error) {
  708. fn := func(node ast.Node) {
  709. call := node.(*ast.CallExpr)
  710. var kind string
  711. if IsCallToAST(pass, call, "(*text/template.Template).Parse") {
  712. kind = "text"
  713. } else if IsCallToAST(pass, call, "(*html/template.Template).Parse") {
  714. kind = "html"
  715. } else {
  716. return
  717. }
  718. sel := call.Fun.(*ast.SelectorExpr)
  719. if !IsCallToAST(pass, sel.X, "text/template.New") &&
  720. !IsCallToAST(pass, sel.X, "html/template.New") {
  721. // TODO(dh): this is a cheap workaround for templates with
  722. // different delims. A better solution with less false
  723. // negatives would use data flow analysis to see where the
  724. // template comes from and where it has been
  725. return
  726. }
  727. s, ok := ExprToString(pass, call.Args[Arg("(*text/template.Template).Parse.text")])
  728. if !ok {
  729. return
  730. }
  731. var err error
  732. switch kind {
  733. case "text":
  734. _, err = texttemplate.New("").Parse(s)
  735. case "html":
  736. _, err = htmltemplate.New("").Parse(s)
  737. }
  738. if err != nil {
  739. // TODO(dominikh): whitelist other parse errors, if any
  740. if strings.Contains(err.Error(), "unexpected") {
  741. ReportNodef(pass, call.Args[Arg("(*text/template.Template).Parse.text")], "%s", err)
  742. }
  743. }
  744. }
  745. pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.CallExpr)(nil)}, fn)
  746. return nil, nil
  747. }
  748. func CheckTimeSleepConstant(pass *analysis.Pass) (interface{}, error) {
  749. fn := func(node ast.Node) {
  750. call := node.(*ast.CallExpr)
  751. if !IsCallToAST(pass, call, "time.Sleep") {
  752. return
  753. }
  754. lit, ok := call.Args[Arg("time.Sleep.d")].(*ast.BasicLit)
  755. if !ok {
  756. return
  757. }
  758. n, err := strconv.Atoi(lit.Value)
  759. if err != nil {
  760. return
  761. }
  762. if n == 0 || n > 120 {
  763. // time.Sleep(0) is a seldom used pattern in concurrency
  764. // tests. >120 might be intentional. 120 was chosen
  765. // because the user could've meant 2 minutes.
  766. return
  767. }
  768. recommendation := "time.Sleep(time.Nanosecond)"
  769. if n != 1 {
  770. recommendation = fmt.Sprintf("time.Sleep(%d * time.Nanosecond)", n)
  771. }
  772. ReportNodef(pass, call.Args[Arg("time.Sleep.d")],
  773. "sleeping for %d nanoseconds is probably a bug. Be explicit if it isn't: %s", n, recommendation)
  774. }
  775. pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.CallExpr)(nil)}, fn)
  776. return nil, nil
  777. }
  778. func CheckWaitgroupAdd(pass *analysis.Pass) (interface{}, error) {
  779. fn := func(node ast.Node) {
  780. g := node.(*ast.GoStmt)
  781. fun, ok := g.Call.Fun.(*ast.FuncLit)
  782. if !ok {
  783. return
  784. }
  785. if len(fun.Body.List) == 0 {
  786. return
  787. }
  788. stmt, ok := fun.Body.List[0].(*ast.ExprStmt)
  789. if !ok {
  790. return
  791. }
  792. if IsCallToAST(pass, stmt.X, "(*sync.WaitGroup).Add") {
  793. ReportNodef(pass, stmt, "should call %s before starting the goroutine to avoid a race",
  794. Render(pass, stmt))
  795. }
  796. }
  797. pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.GoStmt)(nil)}, fn)
  798. return nil, nil
  799. }
  800. func CheckInfiniteEmptyLoop(pass *analysis.Pass) (interface{}, error) {
  801. fn := func(node ast.Node) {
  802. loop := node.(*ast.ForStmt)
  803. if len(loop.Body.List) != 0 || loop.Post != nil {
  804. return
  805. }
  806. if loop.Init != nil {
  807. // TODO(dh): this isn't strictly necessary, it just makes
  808. // the check easier.
  809. return
  810. }
  811. // An empty loop is bad news in two cases: 1) The loop has no
  812. // condition. In that case, it's just a loop that spins
  813. // forever and as fast as it can, keeping a core busy. 2) The
  814. // loop condition only consists of variable or field reads and
  815. // operators on those. The only way those could change their
  816. // value is with unsynchronised access, which constitutes a
  817. // data race.
  818. //
  819. // If the condition contains any function calls, its behaviour
  820. // is dynamic and the loop might terminate. Similarly for
  821. // channel receives.
  822. if loop.Cond != nil {
  823. if hasSideEffects(loop.Cond) {
  824. return
  825. }
  826. if ident, ok := loop.Cond.(*ast.Ident); ok {
  827. if k, ok := pass.TypesInfo.ObjectOf(ident).(*types.Const); ok {
  828. if !constant.BoolVal(k.Val()) {
  829. // don't flag `for false {}` loops. They're a debug aid.
  830. return
  831. }
  832. }
  833. }
  834. ReportNodef(pass, loop, "loop condition never changes or has a race condition")
  835. }
  836. ReportNodef(pass, loop, "this loop will spin, using 100%% CPU")
  837. }
  838. pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.ForStmt)(nil)}, fn)
  839. return nil, nil
  840. }
  841. func CheckDeferInInfiniteLoop(pass *analysis.Pass) (interface{}, error) {
  842. fn := func(node ast.Node) {
  843. mightExit := false
  844. var defers []ast.Stmt
  845. loop := node.(*ast.ForStmt)
  846. if loop.Cond != nil {
  847. return
  848. }
  849. fn2 := func(node ast.Node) bool {
  850. switch stmt := node.(type) {
  851. case *ast.ReturnStmt:
  852. mightExit = true
  853. return false
  854. case *ast.BranchStmt:
  855. // TODO(dominikh): if this sees a break in a switch or
  856. // select, it doesn't check if it breaks the loop or
  857. // just the select/switch. This causes some false
  858. // negatives.
  859. if stmt.Tok == token.BREAK {
  860. mightExit = true
  861. return false
  862. }
  863. case *ast.DeferStmt:
  864. defers = append(defers, stmt)
  865. case *ast.FuncLit:
  866. // Don't look into function bodies
  867. return false
  868. }
  869. return true
  870. }
  871. ast.Inspect(loop.Body, fn2)
  872. if mightExit {
  873. return
  874. }
  875. for _, stmt := range defers {
  876. ReportNodef(pass, stmt, "defers in this infinite loop will never run")
  877. }
  878. }
  879. pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.ForStmt)(nil)}, fn)
  880. return nil, nil
  881. }
  882. func CheckDubiousDeferInChannelRangeLoop(pass *analysis.Pass) (interface{}, error) {
  883. fn := func(node ast.Node) {
  884. loop := node.(*ast.RangeStmt)
  885. typ := pass.TypesInfo.TypeOf(loop.X)
  886. _, ok := typ.Underlying().(*types.Chan)
  887. if !ok {
  888. return
  889. }
  890. fn2 := func(node ast.Node) bool {
  891. switch stmt := node.(type) {
  892. case *ast.DeferStmt:
  893. ReportNodef(pass, stmt, "defers in this range loop won't run unless the channel gets closed")
  894. case *ast.FuncLit:
  895. // Don't look into function bodies
  896. return false
  897. }
  898. return true
  899. }
  900. ast.Inspect(loop.Body, fn2)
  901. }
  902. pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.RangeStmt)(nil)}, fn)
  903. return nil, nil
  904. }
  905. func CheckTestMainExit(pass *analysis.Pass) (interface{}, error) {
  906. var (
  907. fnmain ast.Node
  908. callsExit bool
  909. callsRun bool
  910. arg types.Object
  911. )
  912. fn := func(node ast.Node, push bool) bool {
  913. if !push {
  914. if fnmain != nil && node == fnmain {
  915. if !callsExit && callsRun {
  916. ReportNodef(pass, fnmain, "TestMain should call os.Exit to set exit code")
  917. }
  918. fnmain = nil
  919. callsExit = false
  920. callsRun = false
  921. arg = nil
  922. }
  923. return true
  924. }
  925. switch node := node.(type) {
  926. case *ast.FuncDecl:
  927. if fnmain != nil {
  928. return true
  929. }
  930. if !isTestMain(pass, node) {
  931. return false
  932. }
  933. fnmain = node
  934. arg = pass.TypesInfo.ObjectOf(node.Type.Params.List[0].Names[0])
  935. return true
  936. case *ast.CallExpr:
  937. if IsCallToAST(pass, node, "os.Exit") {
  938. callsExit = true
  939. return false
  940. }
  941. sel, ok := node.Fun.(*ast.SelectorExpr)
  942. if !ok {
  943. return true
  944. }
  945. ident, ok := sel.X.(*ast.Ident)
  946. if !ok {
  947. return true
  948. }
  949. if arg != pass.TypesInfo.ObjectOf(ident) {
  950. return true
  951. }
  952. if sel.Sel.Name == "Run" {
  953. callsRun = true
  954. return false
  955. }
  956. return true
  957. default:
  958. // unreachable
  959. return true
  960. }
  961. }
  962. pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Nodes([]ast.Node{(*ast.FuncDecl)(nil), (*ast.CallExpr)(nil)}, fn)
  963. return nil, nil
  964. }
  965. func isTestMain(pass *analysis.Pass, decl *ast.FuncDecl) bool {
  966. if decl.Name.Name != "TestMain" {
  967. return false
  968. }
  969. if len(decl.Type.Params.List) != 1 {
  970. return false
  971. }
  972. arg := decl.Type.Params.List[0]
  973. if len(arg.Names) != 1 {
  974. return false
  975. }
  976. return IsOfType(pass, arg.Type, "*testing.M")
  977. }
  978. func CheckExec(pass *analysis.Pass) (interface{}, error) {
  979. fn := func(node ast.Node) {
  980. call := node.(*ast.CallExpr)
  981. if !IsCallToAST(pass, call, "os/exec.Command") {
  982. return
  983. }
  984. val, ok := ExprToString(pass, call.Args[Arg("os/exec.Command.name")])
  985. if !ok {
  986. return
  987. }
  988. if !strings.Contains(val, " ") || strings.Contains(val, `\`) || strings.Contains(val, "/") {
  989. return
  990. }
  991. ReportNodef(pass, call.Args[Arg("os/exec.Command.name")],
  992. "first argument to exec.Command looks like a shell command, but a program name or path are expected")
  993. }
  994. pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.CallExpr)(nil)}, fn)
  995. return nil, nil
  996. }
  997. func CheckLoopEmptyDefault(pass *analysis.Pass) (interface{}, error) {
  998. fn := func(node ast.Node) {
  999. loop := node.(*ast.ForStmt)
  1000. if len(loop.Body.List) != 1 || loop.Cond != nil || loop.Init != nil {
  1001. return
  1002. }
  1003. sel, ok := loop.Body.List[0].(*ast.SelectStmt)
  1004. if !ok {
  1005. return
  1006. }
  1007. for _, c := range sel.Body.List {
  1008. if comm, ok := c.(*ast.CommClause); ok && comm.Comm == nil && len(comm.Body) == 0 {
  1009. ReportNodef(pass, comm, "should not have an empty default case in a for+select loop. The loop will spin.")
  1010. }
  1011. }
  1012. }
  1013. pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.ForStmt)(nil)}, fn)
  1014. return nil, nil
  1015. }
  1016. func CheckLhsRhsIdentical(pass *analysis.Pass) (interface{}, error) {
  1017. fn := func(node ast.Node) {
  1018. op := node.(*ast.BinaryExpr)
  1019. switch op.Op {
  1020. case token.EQL, token.NEQ:
  1021. if basic, ok := pass.TypesInfo.TypeOf(op.X).Underlying().(*types.Basic); ok {
  1022. if kind := basic.Kind(); kind == types.Float32 || kind == types.Float64 {
  1023. // f == f and f != f might be used to check for NaN
  1024. return
  1025. }
  1026. }
  1027. case token.SUB, token.QUO, token.AND, token.REM, token.OR, token.XOR, token.AND_NOT,
  1028. token.LAND, token.LOR, token.LSS, token.GTR, token.LEQ, token.GEQ:
  1029. default:
  1030. // For some ops, such as + and *, it can make sense to
  1031. // have identical operands
  1032. return
  1033. }
  1034. if Render(pass, op.X) != Render(pass, op.Y) {
  1035. return
  1036. }
  1037. l1, ok1 := op.X.(*ast.BasicLit)
  1038. l2, ok2 := op.Y.(*ast.BasicLit)
  1039. if ok1 && ok2 && l1.Kind == token.INT && l2.Kind == l1.Kind && l1.Value == "0" && l2.Value == l1.Value && IsGenerated(pass, l1.Pos()) {
  1040. // cgo generates the following function call:
  1041. // _cgoCheckPointer(_cgoBase0, 0 == 0) – it uses 0 == 0
  1042. // instead of true in case the user shadowed the
  1043. // identifier. Ideally we'd restrict this exception to
  1044. // calls of _cgoCheckPointer, but it's not worth the
  1045. // hassle of keeping track of the stack. <lit> <op> <lit>
  1046. // are very rare to begin with, and we're mostly checking
  1047. // for them to catch typos such as 1 == 1 where the user
  1048. // meant to type i == 1. The odds of a false negative for
  1049. // 0 == 0 are slim.
  1050. return
  1051. }
  1052. ReportNodef(pass, op, "identical expressions on the left and right side of the '%s' operator", op.Op)
  1053. }
  1054. pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.BinaryExpr)(nil)}, fn)
  1055. return nil, nil
  1056. }
  1057. func CheckScopedBreak(pass *analysis.Pass) (interface{}, error) {
  1058. fn := func(node ast.Node) {
  1059. var body *ast.BlockStmt
  1060. switch node := node.(type) {
  1061. case *ast.ForStmt:
  1062. body = node.Body
  1063. case *ast.RangeStmt:
  1064. body = node.Body
  1065. default:
  1066. panic(fmt.Sprintf("unreachable: %T", node))
  1067. }
  1068. for _, stmt := range body.List {
  1069. var blocks [][]ast.Stmt
  1070. switch stmt := stmt.(type) {
  1071. case *ast.SwitchStmt:
  1072. for _, c := range stmt.Body.List {
  1073. blocks = append(blocks, c.(*ast.CaseClause).Body)
  1074. }
  1075. case *ast.SelectStmt:
  1076. for _, c := range stmt.Body.List {
  1077. blocks = append(blocks, c.(*ast.CommClause).Body)
  1078. }
  1079. default:
  1080. continue
  1081. }
  1082. for _, body := range blocks {
  1083. if len(body) == 0 {
  1084. continue
  1085. }
  1086. lasts := []ast.Stmt{body[len(body)-1]}
  1087. // TODO(dh): unfold all levels of nested block
  1088. // statements, not just a single level if statement
  1089. if ifs, ok := lasts[0].(*ast.IfStmt); ok {
  1090. if len(ifs.Body.List) == 0 {
  1091. continue
  1092. }
  1093. lasts[0] = ifs.Body.List[len(ifs.Body.List)-1]
  1094. if block, ok := ifs.Else.(*ast.BlockStmt); ok {
  1095. if len(block.List) != 0 {
  1096. lasts = append(lasts, block.List[len(block.List)-1])
  1097. }
  1098. }
  1099. }
  1100. for _, last := range lasts {
  1101. branch, ok := last.(*ast.BranchStmt)
  1102. if !ok || branch.Tok != token.BREAK || branch.Label != nil {
  1103. continue
  1104. }
  1105. ReportNodef(pass, branch, "ineffective break statement. Did you mean to break out of the outer loop?")
  1106. }
  1107. }
  1108. }
  1109. }
  1110. pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.ForStmt)(nil), (*ast.RangeStmt)(nil)}, fn)
  1111. return nil, nil
  1112. }
  1113. func CheckUnsafePrintf(pass *analysis.Pass) (interface{}, error) {
  1114. fn := func(node ast.Node) {
  1115. call := node.(*ast.CallExpr)
  1116. var arg int
  1117. if IsCallToAnyAST(pass, call, "fmt.Printf", "fmt.Sprintf", "log.Printf") {
  1118. arg = Arg("fmt.Printf.format")
  1119. } else if IsCallToAnyAST(pass, call, "fmt.Fprintf") {
  1120. arg = Arg("fmt.Fprintf.format")
  1121. } else {
  1122. return
  1123. }
  1124. if len(call.Args) != arg+1 {
  1125. return
  1126. }
  1127. switch call.Args[arg].(type) {
  1128. case *ast.CallExpr, *ast.Ident:
  1129. default:
  1130. return
  1131. }
  1132. ReportNodef(pass, call.Args[arg],
  1133. "printf-style function with dynamic format string and no further arguments should use print-style function instead")
  1134. }
  1135. pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.CallExpr)(nil)}, fn)
  1136. return nil, nil
  1137. }
  1138. func CheckEarlyDefer(pass *analysis.Pass) (interface{}, error) {
  1139. fn := func(node ast.Node) {
  1140. block := node.(*ast.BlockStmt)
  1141. if len(block.List) < 2 {
  1142. return
  1143. }
  1144. for i, stmt := range block.List {
  1145. if i == len(block.List)-1 {
  1146. break
  1147. }
  1148. assign, ok := stmt.(*ast.AssignStmt)
  1149. if !ok {
  1150. continue
  1151. }
  1152. if len(assign.Rhs) != 1 {
  1153. continue
  1154. }
  1155. if len(assign.Lhs) < 2 {
  1156. continue
  1157. }
  1158. if lhs, ok := assign.Lhs[len(assign.Lhs)-1].(*ast.Ident); ok && lhs.Name == "_" {
  1159. continue
  1160. }
  1161. call, ok := assign.Rhs[0].(*ast.CallExpr)
  1162. if !ok {
  1163. continue
  1164. }
  1165. sig, ok := pass.TypesInfo.TypeOf(call.Fun).(*types.Signature)
  1166. if !ok {
  1167. continue
  1168. }
  1169. if sig.Results().Len() < 2 {
  1170. continue
  1171. }
  1172. last := sig.Results().At(sig.Results().Len() - 1)
  1173. // FIXME(dh): check that it's error from universe, not
  1174. // another type of the same name
  1175. if last.Type().String() != "error" {
  1176. continue
  1177. }
  1178. lhs, ok := assign.Lhs[0].(*ast.Ident)
  1179. if !ok {
  1180. continue
  1181. }
  1182. def, ok := block.List[i+1].(*ast.DeferStmt)
  1183. if !ok {
  1184. continue
  1185. }
  1186. sel, ok := def.Call.Fun.(*ast.SelectorExpr)
  1187. if !ok {
  1188. continue
  1189. }
  1190. ident, ok := selectorX(sel).(*ast.Ident)
  1191. if !ok {
  1192. continue
  1193. }
  1194. if ident.Obj != lhs.Obj {
  1195. continue
  1196. }
  1197. if sel.Sel.Name != "Close" {
  1198. continue
  1199. }
  1200. ReportNodef(pass, def, "should check returned error before deferring %s", Render(pass, def.Call))
  1201. }
  1202. }
  1203. pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.BlockStmt)(nil)}, fn)
  1204. return nil, nil
  1205. }
  1206. func selectorX(sel *ast.SelectorExpr) ast.Node {
  1207. switch x := sel.X.(type) {
  1208. case *ast.SelectorExpr:
  1209. return selectorX(x)
  1210. default:
  1211. return x
  1212. }
  1213. }
  1214. func CheckEmptyCriticalSection(pass *analysis.Pass) (interface{}, error) {
  1215. // Initially it might seem like this check would be easier to
  1216. // implement in SSA. After all, we're only checking for two
  1217. // consecutive method calls. In reality, however, there may be any
  1218. // number of other instructions between the lock and unlock, while
  1219. // still constituting an empty critical section. For example,
  1220. // given `m.x().Lock(); m.x().Unlock()`, there will be a call to
  1221. // x(). In the AST-based approach, this has a tiny potential for a
  1222. // false positive (the second call to x might be doing work that
  1223. // is protected by the mutex). In an SSA-based approach, however,
  1224. // it would miss a lot of real bugs.
  1225. mutexParams := func(s ast.Stmt) (x ast.Expr, funcName string, ok bool) {
  1226. expr, ok := s.(*ast.ExprStmt)
  1227. if !ok {
  1228. return nil, "", false
  1229. }
  1230. call, ok := expr.X.(*ast.CallExpr)
  1231. if !ok {
  1232. return nil, "", false
  1233. }
  1234. sel, ok := call.Fun.(*ast.SelectorExpr)
  1235. if !ok {
  1236. return nil, "", false
  1237. }
  1238. fn, ok := pass.TypesInfo.ObjectOf(sel.Sel).(*types.Func)
  1239. if !ok {
  1240. return nil, "", false
  1241. }
  1242. sig := fn.Type().(*types.Signature)
  1243. if sig.Params().Len() != 0 || sig.Results().Len() != 0 {
  1244. return nil, "", false
  1245. }
  1246. return sel.X, fn.Name(), true
  1247. }
  1248. fn := func(node ast.Node) {
  1249. block := node.(*ast.BlockStmt)
  1250. if len(block.List) < 2 {
  1251. return
  1252. }
  1253. for i := range block.List[:len(block.List)-1] {
  1254. sel1, method1, ok1 := mutexParams(block.List[i])
  1255. sel2, method2, ok2 := mutexParams(block.List[i+1])
  1256. if !ok1 || !ok2 || Render(pass, sel1) != Render(pass, sel2) {
  1257. continue
  1258. }
  1259. if (method1 == "Lock" && method2 == "Unlock") ||
  1260. (method1 == "RLock" && method2 == "RUnlock") {
  1261. ReportNodef(pass, block.List[i+1], "empty critical section")
  1262. }
  1263. }
  1264. }
  1265. pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.BlockStmt)(nil)}, fn)
  1266. return nil, nil
  1267. }
  1268. // cgo produces code like fn(&*_Cvar_kSomeCallbacks) which we don't
  1269. // want to flag.
  1270. var cgoIdent = regexp.MustCompile(`^_C(func|var)_.+$`)
  1271. func CheckIneffectiveCopy(pass *analysis.Pass) (interface{}, error) {
  1272. fn := func(node ast.Node) {
  1273. if unary, ok := node.(*ast.UnaryExpr); ok {
  1274. if star, ok := unary.X.(*ast.StarExpr); ok && unary.Op == token.AND {
  1275. ident, ok := star.X.(*ast.Ident)
  1276. if !ok || !cgoIdent.MatchString(ident.Name) {
  1277. ReportNodef(pass, unary, "&*x will be simplified to x. It will not copy x.")
  1278. }
  1279. }
  1280. }
  1281. if star, ok := node.(*ast.StarExpr); ok {
  1282. if unary, ok := star.X.(*ast.UnaryExpr); ok && unary.Op == token.AND {
  1283. ReportNodef(pass, star, "*&x will be simplified to x. It will not copy x.")
  1284. }
  1285. }
  1286. }
  1287. pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.UnaryExpr)(nil), (*ast.StarExpr)(nil)}, fn)
  1288. return nil, nil
  1289. }
  1290. func CheckDiffSizeComparison(pass *analysis.Pass) (interface{}, error) {
  1291. ranges := pass.ResultOf[valueRangesAnalyzer].(map[*ssa.Function]vrp.Ranges)
  1292. for _, ssafn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs {
  1293. for _, b := range ssafn.Blocks {
  1294. for _, ins := range b.Instrs {
  1295. binop, ok := ins.(*ssa.BinOp)
  1296. if !ok {
  1297. continue
  1298. }
  1299. if binop.Op != token.EQL && binop.Op != token.NEQ {
  1300. continue
  1301. }
  1302. _, ok1 := binop.X.(*ssa.Slice)
  1303. _, ok2 := binop.Y.(*ssa.Slice)
  1304. if !ok1 && !ok2 {
  1305. continue
  1306. }
  1307. r := ranges[ssafn]
  1308. r1, ok1 := r.Get(binop.X).(vrp.StringInterval)
  1309. r2, ok2 := r.Get(binop.Y).(vrp.StringInterval)
  1310. if !ok1 || !ok2 {
  1311. continue
  1312. }
  1313. if r1.Length.Intersection(r2.Length).Empty() {
  1314. pass.Reportf(binop.Pos(), "comparing strings of different sizes for equality will always return false")
  1315. }
  1316. }
  1317. }
  1318. }
  1319. return nil, nil
  1320. }
  1321. func CheckCanonicalHeaderKey(pass *analysis.Pass) (interface{}, error) {
  1322. fn := func(node ast.Node, push bool) bool {
  1323. if !push {
  1324. return false
  1325. }
  1326. assign, ok := node.(*ast.AssignStmt)
  1327. if ok {
  1328. // TODO(dh): This risks missing some Header reads, for
  1329. // example in `h1["foo"] = h2["foo"]` – these edge
  1330. // cases are probably rare enough to ignore for now.
  1331. for _, expr := range assign.Lhs {
  1332. op, ok := expr.(*ast.IndexExpr)
  1333. if !ok {
  1334. continue
  1335. }
  1336. if IsOfType(pass, op.X, "net/http.Header") {
  1337. return false
  1338. }
  1339. }
  1340. return true
  1341. }
  1342. op, ok := node.(*ast.IndexExpr)
  1343. if !ok {
  1344. return true
  1345. }
  1346. if !IsOfType(pass, op.X, "net/http.Header") {
  1347. return true
  1348. }
  1349. s, ok := ExprToString(pass, op.Index)
  1350. if !ok {
  1351. return true
  1352. }
  1353. if s == http.CanonicalHeaderKey(s) {
  1354. return true
  1355. }
  1356. ReportNodef(pass, op, "keys in http.Header are canonicalized, %q is not canonical; fix the constant or use http.CanonicalHeaderKey", s)
  1357. return true
  1358. }
  1359. pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Nodes([]ast.Node{(*ast.AssignStmt)(nil), (*ast.IndexExpr)(nil)}, fn)
  1360. return nil, nil
  1361. }
  1362. func CheckBenchmarkN(pass *analysis.Pass) (interface{}, error) {
  1363. fn := func(node ast.Node) {
  1364. assign := node.(*ast.AssignStmt)
  1365. if len(assign.Lhs) != 1 || len(assign.Rhs) != 1 {
  1366. return
  1367. }
  1368. sel, ok := assign.Lhs[0].(*ast.SelectorExpr)
  1369. if !ok {
  1370. return
  1371. }
  1372. if sel.Sel.Name != "N" {
  1373. return
  1374. }
  1375. if !IsOfType(pass, sel.X, "*testing.B") {
  1376. return
  1377. }
  1378. ReportNodef(pass, assign, "should not assign to %s", Render(pass, sel))
  1379. }
  1380. pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.AssignStmt)(nil)}, fn)
  1381. return nil, nil
  1382. }
  1383. func CheckUnreadVariableValues(pass *analysis.Pass) (interface{}, error) {
  1384. for _, ssafn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs {
  1385. if IsExample(ssafn) {
  1386. continue
  1387. }
  1388. node := ssafn.Syntax()
  1389. if node == nil {
  1390. continue
  1391. }
  1392. if gen, ok := Generator(pass, node.Pos()); ok && gen == facts.Goyacc {
  1393. // Don't flag unused values in code generated by goyacc.
  1394. // There may be hundreds of those due to the way the state
  1395. // machine is constructed.
  1396. continue
  1397. }
  1398. switchTags := map[ssa.Value]struct{}{}
  1399. ast.Inspect(node, func(node ast.Node) bool {
  1400. s, ok := node.(*ast.SwitchStmt)
  1401. if !ok {
  1402. return true
  1403. }
  1404. v, _ := ssafn.ValueForExpr(s.Tag)
  1405. switchTags[v] = struct{}{}
  1406. return true
  1407. })
  1408. hasUse := func(v ssa.Value) bool {
  1409. if _, ok := switchTags[v]; ok {
  1410. return true
  1411. }
  1412. refs := v.Referrers()
  1413. if refs == nil {
  1414. // TODO investigate why refs can be nil
  1415. return true
  1416. }
  1417. return len(FilterDebug(*refs)) > 0
  1418. }
  1419. ast.Inspect(node, func(node ast.Node) bool {
  1420. assign, ok := node.(*ast.AssignStmt)
  1421. if !ok {
  1422. return true
  1423. }
  1424. if len(assign.Lhs) > 1 && len(assign.Rhs) == 1 {
  1425. // Either a function call with multiple return values,
  1426. // or a comma-ok assignment
  1427. val, _ := ssafn.ValueForExpr(assign.Rhs[0])
  1428. if val == nil {
  1429. return true
  1430. }
  1431. refs := val.Referrers()
  1432. if refs == nil {
  1433. return true
  1434. }
  1435. for _, ref := range *refs {
  1436. ex, ok := ref.(*ssa.Extract)
  1437. if !ok {
  1438. continue
  1439. }
  1440. if !hasUse(ex) {
  1441. lhs := assign.Lhs[ex.Index]
  1442. if ident, ok := lhs.(*ast.Ident); !ok || ok && ident.Name == "_" {
  1443. continue
  1444. }
  1445. ReportNodef(pass, lhs, "this value of %s is never used", lhs)
  1446. }
  1447. }
  1448. return true
  1449. }
  1450. for i, lhs := range assign.Lhs {
  1451. rhs := assign.Rhs[i]
  1452. if ident, ok := lhs.(*ast.Ident); !ok || ok && ident.Name == "_" {
  1453. continue
  1454. }
  1455. val, _ := ssafn.ValueForExpr(rhs)
  1456. if val == nil {
  1457. continue
  1458. }
  1459. if !hasUse(val) {
  1460. ReportNodef(pass, lhs, "this value of %s is never used", lhs)
  1461. }
  1462. }
  1463. return true
  1464. })
  1465. }
  1466. return nil, nil
  1467. }
  1468. func CheckPredeterminedBooleanExprs(pass *analysis.Pass) (interface{}, error) {
  1469. for _, ssafn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs {
  1470. for _, block := range ssafn.Blocks {
  1471. for _, ins := range block.Instrs {
  1472. ssabinop, ok := ins.(*ssa.BinOp)
  1473. if !ok {
  1474. continue
  1475. }
  1476. switch ssabinop.Op {
  1477. case token.GTR, token.LSS, token.EQL, token.NEQ, token.LEQ, token.GEQ:
  1478. default:
  1479. continue
  1480. }
  1481. xs, ok1 := consts(ssabinop.X, nil, nil)
  1482. ys, ok2 := consts(ssabinop.Y, nil, nil)
  1483. if !ok1 || !ok2 || len(xs) == 0 || len(ys) == 0 {
  1484. continue
  1485. }
  1486. trues := 0
  1487. for _, x := range xs {
  1488. for _, y := range ys {
  1489. if x.Value == nil {
  1490. if y.Value == nil {
  1491. trues++
  1492. }
  1493. continue
  1494. }
  1495. if constant.Compare(x.Value, ssabinop.Op, y.Value) {
  1496. trues++
  1497. }
  1498. }
  1499. }
  1500. b := trues != 0
  1501. if trues == 0 || trues == len(xs)*len(ys) {
  1502. pass.Reportf(ssabinop.Pos(), "binary expression is always %t for all possible values (%s %s %s)",
  1503. b, xs, ssabinop.Op, ys)
  1504. }
  1505. }
  1506. }
  1507. }
  1508. return nil, nil
  1509. }
  1510. func CheckNilMaps(pass *analysis.Pass) (interface{}, error) {
  1511. for _, ssafn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs {
  1512. for _, block := range ssafn.Blocks {
  1513. for _, ins := range block.Instrs {
  1514. mu, ok := ins.(*ssa.MapUpdate)
  1515. if !ok {
  1516. continue
  1517. }
  1518. c, ok := mu.Map.(*ssa.Const)
  1519. if !ok {
  1520. continue
  1521. }
  1522. if c.Value != nil {
  1523. continue
  1524. }
  1525. pass.Reportf(mu.Pos(), "assignment to nil map")
  1526. }
  1527. }
  1528. }
  1529. return nil, nil
  1530. }
  1531. func CheckExtremeComparison(pass *analysis.Pass) (interface{}, error) {
  1532. isobj := func(expr ast.Expr, name string) bool {
  1533. sel, ok := expr.(*ast.SelectorExpr)
  1534. if !ok {
  1535. return false
  1536. }
  1537. return IsObject(pass.TypesInfo.ObjectOf(sel.Sel), name)
  1538. }
  1539. fn := func(node ast.Node) {
  1540. expr := node.(*ast.BinaryExpr)
  1541. tx := pass.TypesInfo.TypeOf(expr.X)
  1542. basic, ok := tx.Underlying().(*types.Basic)
  1543. if !ok {
  1544. return
  1545. }
  1546. var max string
  1547. var min string
  1548. switch basic.Kind() {
  1549. case types.Uint8:
  1550. max = "math.MaxUint8"
  1551. case types.Uint16:
  1552. max = "math.MaxUint16"
  1553. case types.Uint32:
  1554. max = "math.MaxUint32"
  1555. case types.Uint64:
  1556. max = "math.MaxUint64"
  1557. case types.Uint:
  1558. max = "math.MaxUint64"
  1559. case types.Int8:
  1560. min = "math.MinInt8"
  1561. max = "math.MaxInt8"
  1562. case types.Int16:
  1563. min = "math.MinInt16"
  1564. max = "math.MaxInt16"
  1565. case types.Int32:
  1566. min = "math.MinInt32"
  1567. max = "math.MaxInt32"
  1568. case types.Int64:
  1569. min = "math.MinInt64"
  1570. max = "math.MaxInt64"
  1571. case types.Int:
  1572. min = "math.MinInt64"
  1573. max = "math.MaxInt64"
  1574. }
  1575. if (expr.Op == token.GTR || expr.Op == token.GEQ) && isobj(expr.Y, max) ||
  1576. (expr.Op == token.LSS || expr.Op == token.LEQ) && isobj(expr.X, max) {
  1577. ReportNodef(pass, expr, "no value of type %s is greater than %s", basic, max)
  1578. }
  1579. if expr.Op == token.LEQ && isobj(expr.Y, max) ||
  1580. expr.Op == token.GEQ && isobj(expr.X, max) {
  1581. ReportNodef(pass, expr, "every value of type %s is <= %s", basic, max)
  1582. }
  1583. if (basic.Info() & types.IsUnsigned) != 0 {
  1584. if (expr.Op == token.LSS || expr.Op == token.LEQ) && IsIntLiteral(expr.Y, "0") ||
  1585. (expr.Op == token.GTR || expr.Op == token.GEQ) && IsIntLiteral(expr.X, "0") {
  1586. ReportNodef(pass, expr, "no value of type %s is less than 0", basic)
  1587. }
  1588. if expr.Op == token.GEQ && IsIntLiteral(expr.Y, "0") ||
  1589. expr.Op == token.LEQ && IsIntLiteral(expr.X, "0") {
  1590. ReportNodef(pass, expr, "every value of type %s is >= 0", basic)
  1591. }
  1592. } else {
  1593. if (expr.Op == token.LSS || expr.Op == token.LEQ) && isobj(expr.Y, min) ||
  1594. (expr.Op == token.GTR || expr.Op == token.GEQ) && isobj(expr.X, min) {
  1595. ReportNodef(pass, expr, "no value of type %s is less than %s", basic, min)
  1596. }
  1597. if expr.Op == token.GEQ && isobj(expr.Y, min) ||
  1598. expr.Op == token.LEQ && isobj(expr.X, min) {
  1599. ReportNodef(pass, expr, "every value of type %s is >= %s", basic, min)
  1600. }
  1601. }
  1602. }
  1603. pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.BinaryExpr)(nil)}, fn)
  1604. return nil, nil
  1605. }
  1606. func consts(val ssa.Value, out []*ssa.Const, visitedPhis map[string]bool) ([]*ssa.Const, bool) {
  1607. if visitedPhis == nil {
  1608. visitedPhis = map[string]bool{}
  1609. }
  1610. var ok bool
  1611. switch val := val.(type) {
  1612. case *ssa.Phi:
  1613. if visitedPhis[val.Name()] {
  1614. break
  1615. }
  1616. visitedPhis[val.Name()] = true
  1617. vals := val.Operands(nil)
  1618. for _, phival := range vals {
  1619. out, ok = consts(*phival, out, visitedPhis)
  1620. if !ok {
  1621. return nil, false
  1622. }
  1623. }
  1624. case *ssa.Const:
  1625. out = append(out, val)
  1626. case *ssa.Convert:
  1627. out, ok = consts(val.X, out, visitedPhis)
  1628. if !ok {
  1629. return nil, false
  1630. }
  1631. default:
  1632. return nil, false
  1633. }
  1634. if len(out) < 2 {
  1635. return out, true
  1636. }
  1637. uniq := []*ssa.Const{out[0]}
  1638. for _, val := range out[1:] {
  1639. if val.Value == uniq[len(uniq)-1].Value {
  1640. continue
  1641. }
  1642. uniq = append(uniq, val)
  1643. }
  1644. return uniq, true
  1645. }
  1646. func CheckLoopCondition(pass *analysis.Pass) (interface{}, error) {
  1647. for _, ssafn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs {
  1648. fn := func(node ast.Node) bool {
  1649. loop, ok := node.(*ast.ForStmt)
  1650. if !ok {
  1651. return true
  1652. }
  1653. if loop.Init == nil || loop.Cond == nil || loop.Post == nil {
  1654. return true
  1655. }
  1656. init, ok := loop.Init.(*ast.AssignStmt)
  1657. if !ok || len(init.Lhs) != 1 || len(init.Rhs) != 1 {
  1658. return true
  1659. }
  1660. cond, ok := loop.Cond.(*ast.BinaryExpr)
  1661. if !ok {
  1662. return true
  1663. }
  1664. x, ok := cond.X.(*ast.Ident)
  1665. if !ok {
  1666. return true
  1667. }
  1668. lhs, ok := init.Lhs[0].(*ast.Ident)
  1669. if !ok {
  1670. return true
  1671. }
  1672. if x.Obj != lhs.Obj {
  1673. return true
  1674. }
  1675. if _, ok := loop.Post.(*ast.IncDecStmt); !ok {
  1676. return true
  1677. }
  1678. v, isAddr := ssafn.ValueForExpr(cond.X)
  1679. if v == nil || isAddr {
  1680. return true
  1681. }
  1682. switch v := v.(type) {
  1683. case *ssa.Phi:
  1684. ops := v.Operands(nil)
  1685. if len(ops) != 2 {
  1686. return true
  1687. }
  1688. _, ok := (*ops[0]).(*ssa.Const)
  1689. if !ok {
  1690. return true
  1691. }
  1692. sigma, ok := (*ops[1]).(*ssa.Sigma)
  1693. if !ok {
  1694. return true
  1695. }
  1696. if sigma.X != v {
  1697. return true
  1698. }
  1699. case *ssa.UnOp:
  1700. return true
  1701. }
  1702. ReportNodef(pass, cond, "variable in loop condition never changes")
  1703. return true
  1704. }
  1705. Inspect(ssafn.Syntax(), fn)
  1706. }
  1707. return nil, nil
  1708. }
  1709. func CheckArgOverwritten(pass *analysis.Pass) (interface{}, error) {
  1710. for _, ssafn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs {
  1711. fn := func(node ast.Node) bool {
  1712. var typ *ast.FuncType
  1713. var body *ast.BlockStmt
  1714. switch fn := node.(type) {
  1715. case *ast.FuncDecl:
  1716. typ = fn.Type
  1717. body = fn.Body
  1718. case *ast.FuncLit:
  1719. typ = fn.Type
  1720. body = fn.Body
  1721. }
  1722. if body == nil {
  1723. return true
  1724. }
  1725. if len(typ.Params.List) == 0 {
  1726. return true
  1727. }
  1728. for _, field := range typ.Params.List {
  1729. for _, arg := range field.Names {
  1730. obj := pass.TypesInfo.ObjectOf(arg)
  1731. var ssaobj *ssa.Parameter
  1732. for _, param := range ssafn.Params {
  1733. if param.Object() == obj {
  1734. ssaobj = param
  1735. break
  1736. }
  1737. }
  1738. if ssaobj == nil {
  1739. continue
  1740. }
  1741. refs := ssaobj.Referrers()
  1742. if refs == nil {
  1743. continue
  1744. }
  1745. if len(FilterDebug(*refs)) != 0 {
  1746. continue
  1747. }
  1748. assigned := false
  1749. ast.Inspect(body, func(node ast.Node) bool {
  1750. assign, ok := node.(*ast.AssignStmt)
  1751. if !ok {
  1752. return true
  1753. }
  1754. for _, lhs := range assign.Lhs {
  1755. ident, ok := lhs.(*ast.Ident)
  1756. if !ok {
  1757. continue
  1758. }
  1759. if pass.TypesInfo.ObjectOf(ident) == obj {
  1760. assigned = true
  1761. return false
  1762. }
  1763. }
  1764. return true
  1765. })
  1766. if assigned {
  1767. ReportNodef(pass, arg, "argument %s is overwritten before first use", arg)
  1768. }
  1769. }
  1770. }
  1771. return true
  1772. }
  1773. Inspect(ssafn.Syntax(), fn)
  1774. }
  1775. return nil, nil
  1776. }
  1777. func CheckIneffectiveLoop(pass *analysis.Pass) (interface{}, error) {
  1778. // This check detects some, but not all unconditional loop exits.
  1779. // We give up in the following cases:
  1780. //
  1781. // - a goto anywhere in the loop. The goto might skip over our
  1782. // return, and we don't check that it doesn't.
  1783. //
  1784. // - any nested, unlabelled continue, even if it is in another
  1785. // loop or closure.
  1786. fn := func(node ast.Node) {
  1787. var body *ast.BlockStmt
  1788. switch fn := node.(type) {
  1789. case *ast.FuncDecl:
  1790. body = fn.Body
  1791. case *ast.FuncLit:
  1792. body = fn.Body
  1793. default:
  1794. panic(fmt.Sprintf("unreachable: %T", node))
  1795. }
  1796. if body == nil {
  1797. return
  1798. }
  1799. labels := map[*ast.Object]ast.Stmt{}
  1800. ast.Inspect(body, func(node ast.Node) bool {
  1801. label, ok := node.(*ast.LabeledStmt)
  1802. if !ok {
  1803. return true
  1804. }
  1805. labels[label.Label.Obj] = label.Stmt
  1806. return true
  1807. })
  1808. ast.Inspect(body, func(node ast.Node) bool {
  1809. var loop ast.Node
  1810. var body *ast.BlockStmt
  1811. switch node := node.(type) {
  1812. case *ast.ForStmt:
  1813. body = node.Body
  1814. loop = node
  1815. case *ast.RangeStmt:
  1816. typ := pass.TypesInfo.TypeOf(node.X)
  1817. if _, ok := typ.Underlying().(*types.Map); ok {
  1818. // looping once over a map is a valid pattern for
  1819. // getting an arbitrary element.
  1820. return true
  1821. }
  1822. body = node.Body
  1823. loop = node
  1824. default:
  1825. return true
  1826. }
  1827. if len(body.List) < 2 {
  1828. // avoid flagging the somewhat common pattern of using
  1829. // a range loop to get the first element in a slice,
  1830. // or the first rune in a string.
  1831. return true
  1832. }
  1833. var unconditionalExit ast.Node
  1834. hasBranching := false
  1835. for _, stmt := range body.List {
  1836. switch stmt := stmt.(type) {
  1837. case *ast.BranchStmt:
  1838. switch stmt.Tok {
  1839. case token.BREAK:
  1840. if stmt.Label == nil || labels[stmt.Label.Obj] == loop {
  1841. unconditionalExit = stmt
  1842. }
  1843. case token.CONTINUE:
  1844. if stmt.Label == nil || labels[stmt.Label.Obj] == loop {
  1845. unconditionalExit = nil
  1846. return false
  1847. }
  1848. }
  1849. case *ast.ReturnStmt:
  1850. unconditionalExit = stmt
  1851. case *ast.IfStmt, *ast.ForStmt, *ast.RangeStmt, *ast.SwitchStmt, *ast.SelectStmt:
  1852. hasBranching = true
  1853. }
  1854. }
  1855. if unconditionalExit == nil || !hasBranching {
  1856. return false
  1857. }
  1858. ast.Inspect(body, func(node ast.Node) bool {
  1859. if branch, ok := node.(*ast.BranchStmt); ok {
  1860. switch branch.Tok {
  1861. case token.GOTO:
  1862. unconditionalExit = nil
  1863. return false
  1864. case token.CONTINUE:
  1865. if branch.Label != nil && labels[branch.Label.Obj] != loop {
  1866. return true
  1867. }
  1868. unconditionalExit = nil
  1869. return false
  1870. }
  1871. }
  1872. return true
  1873. })
  1874. if unconditionalExit != nil {
  1875. ReportNodef(pass, unconditionalExit, "the surrounding loop is unconditionally terminated")
  1876. }
  1877. return true
  1878. })
  1879. }
  1880. pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.FuncDecl)(nil), (*ast.FuncLit)(nil)}, fn)
  1881. return nil, nil
  1882. }
  1883. func CheckNilContext(pass *analysis.Pass) (interface{}, error) {
  1884. fn := func(node ast.Node) {
  1885. call := node.(*ast.CallExpr)
  1886. if len(call.Args) == 0 {
  1887. return
  1888. }
  1889. if typ, ok := pass.TypesInfo.TypeOf(call.Args[0]).(*types.Basic); !ok || typ.Kind() != types.UntypedNil {
  1890. return
  1891. }
  1892. sig, ok := pass.TypesInfo.TypeOf(call.Fun).(*types.Signature)
  1893. if !ok {
  1894. return
  1895. }
  1896. if sig.Params().Len() == 0 {
  1897. return
  1898. }
  1899. if !IsType(sig.Params().At(0).Type(), "context.Context") {
  1900. return
  1901. }
  1902. ReportNodef(pass, call.Args[0],
  1903. "do not pass a nil Context, even if a function permits it; pass context.TODO if you are unsure about which Context to use")
  1904. }
  1905. pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.CallExpr)(nil)}, fn)
  1906. return nil, nil
  1907. }
  1908. func CheckSeeker(pass *analysis.Pass) (interface{}, error) {
  1909. fn := func(node ast.Node) {
  1910. call := node.(*ast.CallExpr)
  1911. sel, ok := call.Fun.(*ast.SelectorExpr)
  1912. if !ok {
  1913. return
  1914. }
  1915. if sel.Sel.Name != "Seek" {
  1916. return
  1917. }
  1918. if len(call.Args) != 2 {
  1919. return
  1920. }
  1921. arg0, ok := call.Args[Arg("(io.Seeker).Seek.offset")].(*ast.SelectorExpr)
  1922. if !ok {
  1923. return
  1924. }
  1925. switch arg0.Sel.Name {
  1926. case "SeekStart", "SeekCurrent", "SeekEnd":
  1927. default:
  1928. return
  1929. }
  1930. pkg, ok := arg0.X.(*ast.Ident)
  1931. if !ok {
  1932. return
  1933. }
  1934. if pkg.Name != "io" {
  1935. return
  1936. }
  1937. ReportNodef(pass, call, "the first argument of io.Seeker is the offset, but an io.Seek* constant is being used instead")
  1938. }
  1939. pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.CallExpr)(nil)}, fn)
  1940. return nil, nil
  1941. }
  1942. func CheckIneffectiveAppend(pass *analysis.Pass) (interface{}, error) {
  1943. isAppend := func(ins ssa.Value) bool {
  1944. call, ok := ins.(*ssa.Call)
  1945. if !ok {
  1946. return false
  1947. }
  1948. if call.Call.IsInvoke() {
  1949. return false
  1950. }
  1951. if builtin, ok := call.Call.Value.(*ssa.Builtin); !ok || builtin.Name() != "append" {
  1952. return false
  1953. }
  1954. return true
  1955. }
  1956. for _, ssafn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs {
  1957. for _, block := range ssafn.Blocks {
  1958. for _, ins := range block.Instrs {
  1959. val, ok := ins.(ssa.Value)
  1960. if !ok || !isAppend(val) {
  1961. continue
  1962. }
  1963. isUsed := false
  1964. visited := map[ssa.Instruction]bool{}
  1965. var walkRefs func(refs []ssa.Instruction)
  1966. walkRefs = func(refs []ssa.Instruction) {
  1967. loop:
  1968. for _, ref := range refs {
  1969. if visited[ref] {
  1970. continue
  1971. }
  1972. visited[ref] = true
  1973. if _, ok := ref.(*ssa.DebugRef); ok {
  1974. continue
  1975. }
  1976. switch ref := ref.(type) {
  1977. case *ssa.Phi:
  1978. walkRefs(*ref.Referrers())
  1979. case *ssa.Sigma:
  1980. walkRefs(*ref.Referrers())
  1981. case ssa.Value:
  1982. if !isAppend(ref) {
  1983. isUsed = true
  1984. } else {
  1985. walkRefs(*ref.Referrers())
  1986. }
  1987. case ssa.Instruction:
  1988. isUsed = true
  1989. break loop
  1990. }
  1991. }
  1992. }
  1993. refs := val.Referrers()
  1994. if refs == nil {
  1995. continue
  1996. }
  1997. walkRefs(*refs)
  1998. if !isUsed {
  1999. pass.Reportf(ins.Pos(), "this result of append is never used, except maybe in other appends")
  2000. }
  2001. }
  2002. }
  2003. }
  2004. return nil, nil
  2005. }
  2006. func CheckConcurrentTesting(pass *analysis.Pass) (interface{}, error) {
  2007. for _, ssafn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs {
  2008. for _, block := range ssafn.Blocks {
  2009. for _, ins := range block.Instrs {
  2010. gostmt, ok := ins.(*ssa.Go)
  2011. if !ok {
  2012. continue
  2013. }
  2014. var fn *ssa.Function
  2015. switch val := gostmt.Call.Value.(type) {
  2016. case *ssa.Function:
  2017. fn = val
  2018. case *ssa.MakeClosure:
  2019. fn = val.Fn.(*ssa.Function)
  2020. default:
  2021. continue
  2022. }
  2023. if fn.Blocks == nil {
  2024. continue
  2025. }
  2026. for _, block := range fn.Blocks {
  2027. for _, ins := range block.Instrs {
  2028. call, ok := ins.(*ssa.Call)
  2029. if !ok {
  2030. continue
  2031. }
  2032. if call.Call.IsInvoke() {
  2033. continue
  2034. }
  2035. callee := call.Call.StaticCallee()
  2036. if callee == nil {
  2037. continue
  2038. }
  2039. recv := callee.Signature.Recv()
  2040. if recv == nil {
  2041. continue
  2042. }
  2043. if !IsType(recv.Type(), "*testing.common") {
  2044. continue
  2045. }
  2046. fn, ok := call.Call.StaticCallee().Object().(*types.Func)
  2047. if !ok {
  2048. continue
  2049. }
  2050. name := fn.Name()
  2051. switch name {
  2052. case "FailNow", "Fatal", "Fatalf", "SkipNow", "Skip", "Skipf":
  2053. default:
  2054. continue
  2055. }
  2056. pass.Reportf(gostmt.Pos(), "the goroutine calls T.%s, which must be called in the same goroutine as the test", name)
  2057. }
  2058. }
  2059. }
  2060. }
  2061. }
  2062. return nil, nil
  2063. }
  2064. func eachCall(ssafn *ssa.Function, fn func(caller *ssa.Function, site ssa.CallInstruction, callee *ssa.Function)) {
  2065. for _, b := range ssafn.Blocks {
  2066. for _, instr := range b.Instrs {
  2067. if site, ok := instr.(ssa.CallInstruction); ok {
  2068. if g := site.Common().StaticCallee(); g != nil {
  2069. fn(ssafn, site, g)
  2070. }
  2071. }
  2072. }
  2073. }
  2074. }
  2075. func CheckCyclicFinalizer(pass *analysis.Pass) (interface{}, error) {
  2076. fn := func(caller *ssa.Function, site ssa.CallInstruction, callee *ssa.Function) {
  2077. if callee.RelString(nil) != "runtime.SetFinalizer" {
  2078. return
  2079. }
  2080. arg0 := site.Common().Args[Arg("runtime.SetFinalizer.obj")]
  2081. if iface, ok := arg0.(*ssa.MakeInterface); ok {
  2082. arg0 = iface.X
  2083. }
  2084. unop, ok := arg0.(*ssa.UnOp)
  2085. if !ok {
  2086. return
  2087. }
  2088. v, ok := unop.X.(*ssa.Alloc)
  2089. if !ok {
  2090. return
  2091. }
  2092. arg1 := site.Common().Args[Arg("runtime.SetFinalizer.finalizer")]
  2093. if iface, ok := arg1.(*ssa.MakeInterface); ok {
  2094. arg1 = iface.X
  2095. }
  2096. mc, ok := arg1.(*ssa.MakeClosure)
  2097. if !ok {
  2098. return
  2099. }
  2100. for _, b := range mc.Bindings {
  2101. if b == v {
  2102. pos := lint.DisplayPosition(pass.Fset, mc.Fn.Pos())
  2103. pass.Reportf(site.Pos(), "the finalizer closes over the object, preventing the finalizer from ever running (at %s)", pos)
  2104. }
  2105. }
  2106. }
  2107. for _, ssafn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs {
  2108. eachCall(ssafn, fn)
  2109. }
  2110. return nil, nil
  2111. }
  2112. /*
  2113. func CheckSliceOutOfBounds(pass *analysis.Pass) (interface{}, error) {
  2114. for _, ssafn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs {
  2115. for _, block := range ssafn.Blocks {
  2116. for _, ins := range block.Instrs {
  2117. ia, ok := ins.(*ssa.IndexAddr)
  2118. if !ok {
  2119. continue
  2120. }
  2121. if _, ok := ia.X.Type().Underlying().(*types.Slice); !ok {
  2122. continue
  2123. }
  2124. sr, ok1 := c.funcDescs.Get(ssafn).Ranges[ia.X].(vrp.SliceInterval)
  2125. idxr, ok2 := c.funcDescs.Get(ssafn).Ranges[ia.Index].(vrp.IntInterval)
  2126. if !ok1 || !ok2 || !sr.IsKnown() || !idxr.IsKnown() || sr.Length.Empty() || idxr.Empty() {
  2127. continue
  2128. }
  2129. if idxr.Lower.Cmp(sr.Length.Upper) >= 0 {
  2130. ReportNodef(pass, ia, "index out of bounds")
  2131. }
  2132. }
  2133. }
  2134. }
  2135. return nil, nil
  2136. }
  2137. */
  2138. func CheckDeferLock(pass *analysis.Pass) (interface{}, error) {
  2139. for _, ssafn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs {
  2140. for _, block := range ssafn.Blocks {
  2141. instrs := FilterDebug(block.Instrs)
  2142. if len(instrs) < 2 {
  2143. continue
  2144. }
  2145. for i, ins := range instrs[:len(instrs)-1] {
  2146. call, ok := ins.(*ssa.Call)
  2147. if !ok {
  2148. continue
  2149. }
  2150. if !IsCallTo(call.Common(), "(*sync.Mutex).Lock") && !IsCallTo(call.Common(), "(*sync.RWMutex).RLock") {
  2151. continue
  2152. }
  2153. nins, ok := instrs[i+1].(*ssa.Defer)
  2154. if !ok {
  2155. continue
  2156. }
  2157. if !IsCallTo(&nins.Call, "(*sync.Mutex).Lock") && !IsCallTo(&nins.Call, "(*sync.RWMutex).RLock") {
  2158. continue
  2159. }
  2160. if call.Common().Args[0] != nins.Call.Args[0] {
  2161. continue
  2162. }
  2163. name := shortCallName(call.Common())
  2164. alt := ""
  2165. switch name {
  2166. case "Lock":
  2167. alt = "Unlock"
  2168. case "RLock":
  2169. alt = "RUnlock"
  2170. }
  2171. pass.Reportf(nins.Pos(), "deferring %s right after having locked already; did you mean to defer %s?", name, alt)
  2172. }
  2173. }
  2174. }
  2175. return nil, nil
  2176. }
  2177. func CheckNaNComparison(pass *analysis.Pass) (interface{}, error) {
  2178. isNaN := func(v ssa.Value) bool {
  2179. call, ok := v.(*ssa.Call)
  2180. if !ok {
  2181. return false
  2182. }
  2183. return IsCallTo(call.Common(), "math.NaN")
  2184. }
  2185. for _, ssafn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs {
  2186. for _, block := range ssafn.Blocks {
  2187. for _, ins := range block.Instrs {
  2188. ins, ok := ins.(*ssa.BinOp)
  2189. if !ok {
  2190. continue
  2191. }
  2192. if isNaN(ins.X) || isNaN(ins.Y) {
  2193. pass.Reportf(ins.Pos(), "no value is equal to NaN, not even NaN itself")
  2194. }
  2195. }
  2196. }
  2197. }
  2198. return nil, nil
  2199. }
  2200. func CheckInfiniteRecursion(pass *analysis.Pass) (interface{}, error) {
  2201. for _, ssafn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs {
  2202. eachCall(ssafn, func(caller *ssa.Function, site ssa.CallInstruction, callee *ssa.Function) {
  2203. if callee != ssafn {
  2204. return
  2205. }
  2206. if _, ok := site.(*ssa.Go); ok {
  2207. // Recursively spawning goroutines doesn't consume
  2208. // stack space infinitely, so don't flag it.
  2209. return
  2210. }
  2211. block := site.Block()
  2212. canReturn := false
  2213. for _, b := range ssafn.Blocks {
  2214. if block.Dominates(b) {
  2215. continue
  2216. }
  2217. if len(b.Instrs) == 0 {
  2218. continue
  2219. }
  2220. if _, ok := b.Instrs[len(b.Instrs)-1].(*ssa.Return); ok {
  2221. canReturn = true
  2222. break
  2223. }
  2224. }
  2225. if canReturn {
  2226. return
  2227. }
  2228. pass.Reportf(site.Pos(), "infinite recursive call")
  2229. })
  2230. }
  2231. return nil, nil
  2232. }
  2233. func objectName(obj types.Object) string {
  2234. if obj == nil {
  2235. return "<nil>"
  2236. }
  2237. var name string
  2238. if obj.Pkg() != nil && obj.Pkg().Scope().Lookup(obj.Name()) == obj {
  2239. s := obj.Pkg().Path()
  2240. if s != "" {
  2241. name += s + "."
  2242. }
  2243. }
  2244. name += obj.Name()
  2245. return name
  2246. }
  2247. func isName(pass *analysis.Pass, expr ast.Expr, name string) bool {
  2248. var obj types.Object
  2249. switch expr := expr.(type) {
  2250. case *ast.Ident:
  2251. obj = pass.TypesInfo.ObjectOf(expr)
  2252. case *ast.SelectorExpr:
  2253. obj = pass.TypesInfo.ObjectOf(expr.Sel)
  2254. }
  2255. return objectName(obj) == name
  2256. }
  2257. func CheckLeakyTimeTick(pass *analysis.Pass) (interface{}, error) {
  2258. for _, ssafn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs {
  2259. if IsInMain(pass, ssafn) || IsInTest(pass, ssafn) {
  2260. continue
  2261. }
  2262. for _, block := range ssafn.Blocks {
  2263. for _, ins := range block.Instrs {
  2264. call, ok := ins.(*ssa.Call)
  2265. if !ok || !IsCallTo(call.Common(), "time.Tick") {
  2266. continue
  2267. }
  2268. if !functions.Terminates(call.Parent()) {
  2269. continue
  2270. }
  2271. pass.Reportf(call.Pos(), "using time.Tick leaks the underlying ticker, consider using it only in endless functions, tests and the main package, and use time.NewTicker here")
  2272. }
  2273. }
  2274. }
  2275. return nil, nil
  2276. }
  2277. func CheckDoubleNegation(pass *analysis.Pass) (interface{}, error) {
  2278. fn := func(node ast.Node) {
  2279. unary1 := node.(*ast.UnaryExpr)
  2280. unary2, ok := unary1.X.(*ast.UnaryExpr)
  2281. if !ok {
  2282. return
  2283. }
  2284. if unary1.Op != token.NOT || unary2.Op != token.NOT {
  2285. return
  2286. }
  2287. ReportNodef(pass, unary1, "negating a boolean twice has no effect; is this a typo?")
  2288. }
  2289. pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.UnaryExpr)(nil)}, fn)
  2290. return nil, nil
  2291. }
  2292. func hasSideEffects(node ast.Node) bool {
  2293. dynamic := false
  2294. ast.Inspect(node, func(node ast.Node) bool {
  2295. switch node := node.(type) {
  2296. case *ast.CallExpr:
  2297. dynamic = true
  2298. return false
  2299. case *ast.UnaryExpr:
  2300. if node.Op == token.ARROW {
  2301. dynamic = true
  2302. return false
  2303. }
  2304. }
  2305. return true
  2306. })
  2307. return dynamic
  2308. }
  2309. func CheckRepeatedIfElse(pass *analysis.Pass) (interface{}, error) {
  2310. seen := map[ast.Node]bool{}
  2311. var collectConds func(ifstmt *ast.IfStmt, inits []ast.Stmt, conds []ast.Expr) ([]ast.Stmt, []ast.Expr)
  2312. collectConds = func(ifstmt *ast.IfStmt, inits []ast.Stmt, conds []ast.Expr) ([]ast.Stmt, []ast.Expr) {
  2313. seen[ifstmt] = true
  2314. if ifstmt.Init != nil {
  2315. inits = append(inits, ifstmt.Init)
  2316. }
  2317. conds = append(conds, ifstmt.Cond)
  2318. if elsestmt, ok := ifstmt.Else.(*ast.IfStmt); ok {
  2319. return collectConds(elsestmt, inits, conds)
  2320. }
  2321. return inits, conds
  2322. }
  2323. fn := func(node ast.Node) {
  2324. ifstmt := node.(*ast.IfStmt)
  2325. if seen[ifstmt] {
  2326. return
  2327. }
  2328. inits, conds := collectConds(ifstmt, nil, nil)
  2329. if len(inits) > 0 {
  2330. return
  2331. }
  2332. for _, cond := range conds {
  2333. if hasSideEffects(cond) {
  2334. return
  2335. }
  2336. }
  2337. counts := map[string]int{}
  2338. for _, cond := range conds {
  2339. s := Render(pass, cond)
  2340. counts[s]++
  2341. if counts[s] == 2 {
  2342. ReportNodef(pass, cond, "this condition occurs multiple times in this if/else if chain")
  2343. }
  2344. }
  2345. }
  2346. pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.IfStmt)(nil)}, fn)
  2347. return nil, nil
  2348. }
  2349. func CheckSillyBitwiseOps(pass *analysis.Pass) (interface{}, error) {
  2350. for _, ssafn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs {
  2351. for _, block := range ssafn.Blocks {
  2352. for _, ins := range block.Instrs {
  2353. ins, ok := ins.(*ssa.BinOp)
  2354. if !ok {
  2355. continue
  2356. }
  2357. if c, ok := ins.Y.(*ssa.Const); !ok || c.Value == nil || c.Value.Kind() != constant.Int || c.Uint64() != 0 {
  2358. continue
  2359. }
  2360. switch ins.Op {
  2361. case token.AND, token.OR, token.XOR:
  2362. default:
  2363. // we do not flag shifts because too often, x<<0 is part
  2364. // of a pattern, x<<0, x<<8, x<<16, ...
  2365. continue
  2366. }
  2367. path, _ := astutil.PathEnclosingInterval(File(pass, ins), ins.Pos(), ins.Pos())
  2368. if len(path) == 0 {
  2369. continue
  2370. }
  2371. if node, ok := path[0].(*ast.BinaryExpr); !ok || !IsZero(node.Y) {
  2372. continue
  2373. }
  2374. switch ins.Op {
  2375. case token.AND:
  2376. pass.Reportf(ins.Pos(), "x & 0 always equals 0")
  2377. case token.OR, token.XOR:
  2378. pass.Reportf(ins.Pos(), "x %s 0 always equals x", ins.Op)
  2379. }
  2380. }
  2381. }
  2382. }
  2383. return nil, nil
  2384. }
  2385. func CheckNonOctalFileMode(pass *analysis.Pass) (interface{}, error) {
  2386. fn := func(node ast.Node) {
  2387. call := node.(*ast.CallExpr)
  2388. sig, ok := pass.TypesInfo.TypeOf(call.Fun).(*types.Signature)
  2389. if !ok {
  2390. return
  2391. }
  2392. n := sig.Params().Len()
  2393. var args []int
  2394. for i := 0; i < n; i++ {
  2395. typ := sig.Params().At(i).Type()
  2396. if IsType(typ, "os.FileMode") {
  2397. args = append(args, i)
  2398. }
  2399. }
  2400. for _, i := range args {
  2401. lit, ok := call.Args[i].(*ast.BasicLit)
  2402. if !ok {
  2403. continue
  2404. }
  2405. if len(lit.Value) == 3 &&
  2406. lit.Value[0] != '0' &&
  2407. lit.Value[0] >= '0' && lit.Value[0] <= '7' &&
  2408. lit.Value[1] >= '0' && lit.Value[1] <= '7' &&
  2409. lit.Value[2] >= '0' && lit.Value[2] <= '7' {
  2410. v, err := strconv.ParseInt(lit.Value, 10, 64)
  2411. if err != nil {
  2412. continue
  2413. }
  2414. ReportNodef(pass, call.Args[i], "file mode '%s' evaluates to %#o; did you mean '0%s'?", lit.Value, v, lit.Value)
  2415. }
  2416. }
  2417. }
  2418. pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.CallExpr)(nil)}, fn)
  2419. return nil, nil
  2420. }
  2421. func CheckPureFunctions(pass *analysis.Pass) (interface{}, error) {
  2422. pure := pass.ResultOf[facts.Purity].(facts.PurityResult)
  2423. fnLoop:
  2424. for _, ssafn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs {
  2425. if IsInTest(pass, ssafn) {
  2426. params := ssafn.Signature.Params()
  2427. for i := 0; i < params.Len(); i++ {
  2428. param := params.At(i)
  2429. if IsType(param.Type(), "*testing.B") {
  2430. // Ignore discarded pure functions in code related
  2431. // to benchmarks. Instead of matching BenchmarkFoo
  2432. // functions, we match any function accepting a
  2433. // *testing.B. Benchmarks sometimes call generic
  2434. // functions for doing the actual work, and
  2435. // checking for the parameter is a lot easier and
  2436. // faster than analyzing call trees.
  2437. continue fnLoop
  2438. }
  2439. }
  2440. }
  2441. for _, b := range ssafn.Blocks {
  2442. for _, ins := range b.Instrs {
  2443. ins, ok := ins.(*ssa.Call)
  2444. if !ok {
  2445. continue
  2446. }
  2447. refs := ins.Referrers()
  2448. if refs == nil || len(FilterDebug(*refs)) > 0 {
  2449. continue
  2450. }
  2451. callee := ins.Common().StaticCallee()
  2452. if callee == nil {
  2453. continue
  2454. }
  2455. if callee.Object() == nil {
  2456. // TODO(dh): support anonymous functions
  2457. continue
  2458. }
  2459. if _, ok := pure[callee.Object().(*types.Func)]; ok {
  2460. pass.Reportf(ins.Pos(), "%s is a pure function but its return value is ignored", callee.Name())
  2461. continue
  2462. }
  2463. }
  2464. }
  2465. }
  2466. return nil, nil
  2467. }
  2468. func CheckDeprecated(pass *analysis.Pass) (interface{}, error) {
  2469. deprs := pass.ResultOf[facts.Deprecated].(facts.DeprecatedResult)
  2470. // Selectors can appear outside of function literals, e.g. when
  2471. // declaring package level variables.
  2472. var tfn types.Object
  2473. stack := 0
  2474. fn := func(node ast.Node, push bool) bool {
  2475. if !push {
  2476. stack--
  2477. return false
  2478. }
  2479. stack++
  2480. if stack == 1 {
  2481. tfn = nil
  2482. }
  2483. if fn, ok := node.(*ast.FuncDecl); ok {
  2484. tfn = pass.TypesInfo.ObjectOf(fn.Name)
  2485. }
  2486. sel, ok := node.(*ast.SelectorExpr)
  2487. if !ok {
  2488. return true
  2489. }
  2490. obj := pass.TypesInfo.ObjectOf(sel.Sel)
  2491. if obj.Pkg() == nil {
  2492. return true
  2493. }
  2494. if pass.Pkg == obj.Pkg() || obj.Pkg().Path()+"_test" == pass.Pkg.Path() {
  2495. // Don't flag stuff in our own package
  2496. return true
  2497. }
  2498. if depr, ok := deprs.Objects[obj]; ok {
  2499. // Look for the first available alternative, not the first
  2500. // version something was deprecated in. If a function was
  2501. // deprecated in Go 1.6, an alternative has been available
  2502. // already in 1.0, and we're targeting 1.2, it still
  2503. // makes sense to use the alternative from 1.0, to be
  2504. // future-proof.
  2505. minVersion := deprecated.Stdlib[SelectorName(pass, sel)].AlternativeAvailableSince
  2506. if !IsGoVersion(pass, minVersion) {
  2507. return true
  2508. }
  2509. if tfn != nil {
  2510. if _, ok := deprs.Objects[tfn]; ok {
  2511. // functions that are deprecated may use deprecated
  2512. // symbols
  2513. return true
  2514. }
  2515. }
  2516. ReportNodef(pass, sel, "%s is deprecated: %s", Render(pass, sel), depr.Msg)
  2517. return true
  2518. }
  2519. return true
  2520. }
  2521. imps := map[string]*types.Package{}
  2522. for _, imp := range pass.Pkg.Imports() {
  2523. imps[imp.Path()] = imp
  2524. }
  2525. fn2 := func(node ast.Node) {
  2526. spec := node.(*ast.ImportSpec)
  2527. p := spec.Path.Value
  2528. path := p[1 : len(p)-1]
  2529. imp := imps[path]
  2530. if depr, ok := deprs.Packages[imp]; ok {
  2531. ReportNodef(pass, spec, "Package %s is deprecated: %s", path, depr.Msg)
  2532. }
  2533. }
  2534. pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Nodes(nil, fn)
  2535. pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.ImportSpec)(nil)}, fn2)
  2536. return nil, nil
  2537. }
  2538. func callChecker(rules map[string]CallCheck) func(pass *analysis.Pass) (interface{}, error) {
  2539. return func(pass *analysis.Pass) (interface{}, error) {
  2540. return checkCalls(pass, rules)
  2541. }
  2542. }
  2543. func checkCalls(pass *analysis.Pass, rules map[string]CallCheck) (interface{}, error) {
  2544. ranges := pass.ResultOf[valueRangesAnalyzer].(map[*ssa.Function]vrp.Ranges)
  2545. fn := func(caller *ssa.Function, site ssa.CallInstruction, callee *ssa.Function) {
  2546. obj, ok := callee.Object().(*types.Func)
  2547. if !ok {
  2548. return
  2549. }
  2550. r, ok := rules[lint.FuncName(obj)]
  2551. if !ok {
  2552. return
  2553. }
  2554. var args []*Argument
  2555. ssaargs := site.Common().Args
  2556. if callee.Signature.Recv() != nil {
  2557. ssaargs = ssaargs[1:]
  2558. }
  2559. for _, arg := range ssaargs {
  2560. if iarg, ok := arg.(*ssa.MakeInterface); ok {
  2561. arg = iarg.X
  2562. }
  2563. vr := ranges[site.Parent()][arg]
  2564. args = append(args, &Argument{Value: Value{arg, vr}})
  2565. }
  2566. call := &Call{
  2567. Pass: pass,
  2568. Instr: site,
  2569. Args: args,
  2570. Parent: site.Parent(),
  2571. }
  2572. r(call)
  2573. for idx, arg := range call.Args {
  2574. _ = idx
  2575. for _, e := range arg.invalids {
  2576. // path, _ := astutil.PathEnclosingInterval(f.File, edge.Site.Pos(), edge.Site.Pos())
  2577. // if len(path) < 2 {
  2578. // continue
  2579. // }
  2580. // astcall, ok := path[0].(*ast.CallExpr)
  2581. // if !ok {
  2582. // continue
  2583. // }
  2584. // pass.Reportf(astcall.Args[idx], "%s", e)
  2585. pass.Reportf(site.Pos(), "%s", e)
  2586. }
  2587. }
  2588. for _, e := range call.invalids {
  2589. pass.Reportf(call.Instr.Common().Pos(), "%s", e)
  2590. }
  2591. }
  2592. for _, ssafn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs {
  2593. eachCall(ssafn, fn)
  2594. }
  2595. return nil, nil
  2596. }
  2597. func shortCallName(call *ssa.CallCommon) string {
  2598. if call.IsInvoke() {
  2599. return ""
  2600. }
  2601. switch v := call.Value.(type) {
  2602. case *ssa.Function:
  2603. fn, ok := v.Object().(*types.Func)
  2604. if !ok {
  2605. return ""
  2606. }
  2607. return fn.Name()
  2608. case *ssa.Builtin:
  2609. return v.Name()
  2610. }
  2611. return ""
  2612. }
  2613. func CheckWriterBufferModified(pass *analysis.Pass) (interface{}, error) {
  2614. // TODO(dh): this might be a good candidate for taint analysis.
  2615. // Taint the argument as MUST_NOT_MODIFY, then propagate that
  2616. // through functions like bytes.Split
  2617. for _, ssafn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs {
  2618. sig := ssafn.Signature
  2619. if ssafn.Name() != "Write" || sig.Recv() == nil || sig.Params().Len() != 1 || sig.Results().Len() != 2 {
  2620. continue
  2621. }
  2622. tArg, ok := sig.Params().At(0).Type().(*types.Slice)
  2623. if !ok {
  2624. continue
  2625. }
  2626. if basic, ok := tArg.Elem().(*types.Basic); !ok || basic.Kind() != types.Byte {
  2627. continue
  2628. }
  2629. if basic, ok := sig.Results().At(0).Type().(*types.Basic); !ok || basic.Kind() != types.Int {
  2630. continue
  2631. }
  2632. if named, ok := sig.Results().At(1).Type().(*types.Named); !ok || !IsType(named, "error") {
  2633. continue
  2634. }
  2635. for _, block := range ssafn.Blocks {
  2636. for _, ins := range block.Instrs {
  2637. switch ins := ins.(type) {
  2638. case *ssa.Store:
  2639. addr, ok := ins.Addr.(*ssa.IndexAddr)
  2640. if !ok {
  2641. continue
  2642. }
  2643. if addr.X != ssafn.Params[1] {
  2644. continue
  2645. }
  2646. pass.Reportf(ins.Pos(), "io.Writer.Write must not modify the provided buffer, not even temporarily")
  2647. case *ssa.Call:
  2648. if !IsCallTo(ins.Common(), "append") {
  2649. continue
  2650. }
  2651. if ins.Common().Args[0] != ssafn.Params[1] {
  2652. continue
  2653. }
  2654. pass.Reportf(ins.Pos(), "io.Writer.Write must not modify the provided buffer, not even temporarily")
  2655. }
  2656. }
  2657. }
  2658. }
  2659. return nil, nil
  2660. }
  2661. func loopedRegexp(name string) CallCheck {
  2662. return func(call *Call) {
  2663. if len(extractConsts(call.Args[0].Value.Value)) == 0 {
  2664. return
  2665. }
  2666. if !isInLoop(call.Instr.Block()) {
  2667. return
  2668. }
  2669. call.Invalid(fmt.Sprintf("calling %s in a loop has poor performance, consider using regexp.Compile", name))
  2670. }
  2671. }
  2672. func CheckEmptyBranch(pass *analysis.Pass) (interface{}, error) {
  2673. for _, ssafn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs {
  2674. if ssafn.Syntax() == nil {
  2675. continue
  2676. }
  2677. if IsExample(ssafn) {
  2678. continue
  2679. }
  2680. fn := func(node ast.Node) bool {
  2681. ifstmt, ok := node.(*ast.IfStmt)
  2682. if !ok {
  2683. return true
  2684. }
  2685. if ifstmt.Else != nil {
  2686. b, ok := ifstmt.Else.(*ast.BlockStmt)
  2687. if !ok || len(b.List) != 0 {
  2688. return true
  2689. }
  2690. ReportfFG(pass, ifstmt.Else.Pos(), "empty branch")
  2691. }
  2692. if len(ifstmt.Body.List) != 0 {
  2693. return true
  2694. }
  2695. ReportfFG(pass, ifstmt.Pos(), "empty branch")
  2696. return true
  2697. }
  2698. Inspect(ssafn.Syntax(), fn)
  2699. }
  2700. return nil, nil
  2701. }
  2702. func CheckMapBytesKey(pass *analysis.Pass) (interface{}, error) {
  2703. for _, fn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs {
  2704. for _, b := range fn.Blocks {
  2705. insLoop:
  2706. for _, ins := range b.Instrs {
  2707. // find []byte -> string conversions
  2708. conv, ok := ins.(*ssa.Convert)
  2709. if !ok || conv.Type() != types.Universe.Lookup("string").Type() {
  2710. continue
  2711. }
  2712. if s, ok := conv.X.Type().(*types.Slice); !ok || s.Elem() != types.Universe.Lookup("byte").Type() {
  2713. continue
  2714. }
  2715. refs := conv.Referrers()
  2716. // need at least two (DebugRef) references: the
  2717. // conversion and the *ast.Ident
  2718. if refs == nil || len(*refs) < 2 {
  2719. continue
  2720. }
  2721. ident := false
  2722. // skip first reference, that's the conversion itself
  2723. for _, ref := range (*refs)[1:] {
  2724. switch ref := ref.(type) {
  2725. case *ssa.DebugRef:
  2726. if _, ok := ref.Expr.(*ast.Ident); !ok {
  2727. // the string seems to be used somewhere
  2728. // unexpected; the default branch should
  2729. // catch this already, but be safe
  2730. continue insLoop
  2731. } else {
  2732. ident = true
  2733. }
  2734. case *ssa.Lookup:
  2735. default:
  2736. // the string is used somewhere else than a
  2737. // map lookup
  2738. continue insLoop
  2739. }
  2740. }
  2741. // the result of the conversion wasn't assigned to an
  2742. // identifier
  2743. if !ident {
  2744. continue
  2745. }
  2746. pass.Reportf(conv.Pos(), "m[string(key)] would be more efficient than k := string(key); m[k]")
  2747. }
  2748. }
  2749. }
  2750. return nil, nil
  2751. }
  2752. func CheckRangeStringRunes(pass *analysis.Pass) (interface{}, error) {
  2753. return sharedcheck.CheckRangeStringRunes(pass)
  2754. }
  2755. func CheckSelfAssignment(pass *analysis.Pass) (interface{}, error) {
  2756. fn := func(node ast.Node) {
  2757. assign := node.(*ast.AssignStmt)
  2758. if assign.Tok != token.ASSIGN || len(assign.Lhs) != len(assign.Rhs) {
  2759. return
  2760. }
  2761. for i, stmt := range assign.Lhs {
  2762. rlh := Render(pass, stmt)
  2763. rrh := Render(pass, assign.Rhs[i])
  2764. if rlh == rrh {
  2765. ReportfFG(pass, assign.Pos(), "self-assignment of %s to %s", rrh, rlh)
  2766. }
  2767. }
  2768. }
  2769. pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.AssignStmt)(nil)}, fn)
  2770. return nil, nil
  2771. }
  2772. func buildTagsIdentical(s1, s2 []string) bool {
  2773. if len(s1) != len(s2) {
  2774. return false
  2775. }
  2776. s1s := make([]string, len(s1))
  2777. copy(s1s, s1)
  2778. sort.Strings(s1s)
  2779. s2s := make([]string, len(s2))
  2780. copy(s2s, s2)
  2781. sort.Strings(s2s)
  2782. for i, s := range s1s {
  2783. if s != s2s[i] {
  2784. return false
  2785. }
  2786. }
  2787. return true
  2788. }
  2789. func CheckDuplicateBuildConstraints(pass *analysis.Pass) (interface{}, error) {
  2790. for _, f := range pass.Files {
  2791. constraints := buildTags(f)
  2792. for i, constraint1 := range constraints {
  2793. for j, constraint2 := range constraints {
  2794. if i >= j {
  2795. continue
  2796. }
  2797. if buildTagsIdentical(constraint1, constraint2) {
  2798. ReportfFG(pass, f.Pos(), "identical build constraints %q and %q",
  2799. strings.Join(constraint1, " "),
  2800. strings.Join(constraint2, " "))
  2801. }
  2802. }
  2803. }
  2804. }
  2805. return nil, nil
  2806. }
  2807. func CheckSillyRegexp(pass *analysis.Pass) (interface{}, error) {
  2808. // We could use the rule checking engine for this, but the
  2809. // arguments aren't really invalid.
  2810. for _, fn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs {
  2811. for _, b := range fn.Blocks {
  2812. for _, ins := range b.Instrs {
  2813. call, ok := ins.(*ssa.Call)
  2814. if !ok {
  2815. continue
  2816. }
  2817. switch CallName(call.Common()) {
  2818. case "regexp.MustCompile", "regexp.Compile", "regexp.Match", "regexp.MatchReader", "regexp.MatchString":
  2819. default:
  2820. continue
  2821. }
  2822. c, ok := call.Common().Args[0].(*ssa.Const)
  2823. if !ok {
  2824. continue
  2825. }
  2826. s := constant.StringVal(c.Value)
  2827. re, err := syntax.Parse(s, 0)
  2828. if err != nil {
  2829. continue
  2830. }
  2831. if re.Op != syntax.OpLiteral && re.Op != syntax.OpEmptyMatch {
  2832. continue
  2833. }
  2834. pass.Reportf(call.Pos(), "regular expression does not contain any meta characters")
  2835. }
  2836. }
  2837. }
  2838. return nil, nil
  2839. }
  2840. func CheckMissingEnumTypesInDeclaration(pass *analysis.Pass) (interface{}, error) {
  2841. fn := func(node ast.Node) {
  2842. decl := node.(*ast.GenDecl)
  2843. if !decl.Lparen.IsValid() {
  2844. return
  2845. }
  2846. if decl.Tok != token.CONST {
  2847. return
  2848. }
  2849. groups := GroupSpecs(pass.Fset, decl.Specs)
  2850. groupLoop:
  2851. for _, group := range groups {
  2852. if len(group) < 2 {
  2853. continue
  2854. }
  2855. if group[0].(*ast.ValueSpec).Type == nil {
  2856. // first constant doesn't have a type
  2857. continue groupLoop
  2858. }
  2859. for i, spec := range group {
  2860. spec := spec.(*ast.ValueSpec)
  2861. if len(spec.Names) != 1 || len(spec.Values) != 1 {
  2862. continue groupLoop
  2863. }
  2864. switch v := spec.Values[0].(type) {
  2865. case *ast.BasicLit:
  2866. case *ast.UnaryExpr:
  2867. if _, ok := v.X.(*ast.BasicLit); !ok {
  2868. continue groupLoop
  2869. }
  2870. default:
  2871. // if it's not a literal it might be typed, such as
  2872. // time.Microsecond = 1000 * Nanosecond
  2873. continue groupLoop
  2874. }
  2875. if i == 0 {
  2876. continue
  2877. }
  2878. if spec.Type != nil {
  2879. continue groupLoop
  2880. }
  2881. }
  2882. ReportNodef(pass, group[0], "only the first constant in this group has an explicit type")
  2883. }
  2884. }
  2885. pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.GenDecl)(nil)}, fn)
  2886. return nil, nil
  2887. }
  2888. func CheckTimerResetReturnValue(pass *analysis.Pass) (interface{}, error) {
  2889. for _, fn := range pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA).SrcFuncs {
  2890. for _, block := range fn.Blocks {
  2891. for _, ins := range block.Instrs {
  2892. call, ok := ins.(*ssa.Call)
  2893. if !ok {
  2894. continue
  2895. }
  2896. if !IsCallTo(call.Common(), "(*time.Timer).Reset") {
  2897. continue
  2898. }
  2899. refs := call.Referrers()
  2900. if refs == nil {
  2901. continue
  2902. }
  2903. for _, ref := range FilterDebug(*refs) {
  2904. ifstmt, ok := ref.(*ssa.If)
  2905. if !ok {
  2906. continue
  2907. }
  2908. found := false
  2909. for _, succ := range ifstmt.Block().Succs {
  2910. if len(succ.Preds) != 1 {
  2911. // Merge point, not a branch in the
  2912. // syntactical sense.
  2913. // FIXME(dh): this is broken for if
  2914. // statements a la "if x || y"
  2915. continue
  2916. }
  2917. ssautil.Walk(succ, func(b *ssa.BasicBlock) bool {
  2918. if !succ.Dominates(b) {
  2919. // We've reached the end of the branch
  2920. return false
  2921. }
  2922. for _, ins := range b.Instrs {
  2923. // TODO(dh): we should check that
  2924. // we're receiving from the channel of
  2925. // a time.Timer to further reduce
  2926. // false positives. Not a key
  2927. // priority, considering the rarity of
  2928. // Reset and the tiny likeliness of a
  2929. // false positive
  2930. if ins, ok := ins.(*ssa.UnOp); ok && ins.Op == token.ARROW && IsType(ins.X.Type(), "<-chan time.Time") {
  2931. found = true
  2932. return false
  2933. }
  2934. }
  2935. return true
  2936. })
  2937. }
  2938. if found {
  2939. pass.Reportf(call.Pos(), "it is not possible to use Reset's return value correctly, as there is a race condition between draining the channel and the new timer expiring")
  2940. }
  2941. }
  2942. }
  2943. }
  2944. }
  2945. return nil, nil
  2946. }
  2947. func CheckToLowerToUpperComparison(pass *analysis.Pass) (interface{}, error) {
  2948. fn := func(node ast.Node) {
  2949. binExpr := node.(*ast.BinaryExpr)
  2950. var negative bool
  2951. switch binExpr.Op {
  2952. case token.EQL:
  2953. negative = false
  2954. case token.NEQ:
  2955. negative = true
  2956. default:
  2957. return
  2958. }
  2959. const (
  2960. lo = "strings.ToLower"
  2961. up = "strings.ToUpper"
  2962. )
  2963. var call string
  2964. if IsCallToAST(pass, binExpr.X, lo) && IsCallToAST(pass, binExpr.Y, lo) {
  2965. call = lo
  2966. } else if IsCallToAST(pass, binExpr.X, up) && IsCallToAST(pass, binExpr.Y, up) {
  2967. call = up
  2968. } else {
  2969. return
  2970. }
  2971. bang := ""
  2972. if negative {
  2973. bang = "!"
  2974. }
  2975. ReportNodef(pass, binExpr, "should use %sstrings.EqualFold(a, b) instead of %s(a) %s %s(b)", bang, call, binExpr.Op, call)
  2976. }
  2977. pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.BinaryExpr)(nil)}, fn)
  2978. return nil, nil
  2979. }
  2980. func CheckUnreachableTypeCases(pass *analysis.Pass) (interface{}, error) {
  2981. // Check if T subsumes V in a type switch. T subsumes V if T is an interface and T's method set is a subset of V's method set.
  2982. subsumes := func(T, V types.Type) bool {
  2983. tIface, ok := T.Underlying().(*types.Interface)
  2984. if !ok {
  2985. return false
  2986. }
  2987. return types.Implements(V, tIface)
  2988. }
  2989. subsumesAny := func(Ts, Vs []types.Type) (types.Type, types.Type, bool) {
  2990. for _, T := range Ts {
  2991. for _, V := range Vs {
  2992. if subsumes(T, V) {
  2993. return T, V, true
  2994. }
  2995. }
  2996. }
  2997. return nil, nil, false
  2998. }
  2999. fn := func(node ast.Node) {
  3000. tsStmt := node.(*ast.TypeSwitchStmt)
  3001. type ccAndTypes struct {
  3002. cc *ast.CaseClause
  3003. types []types.Type
  3004. }
  3005. // All asserted types in the order of case clauses.
  3006. ccs := make([]ccAndTypes, 0, len(tsStmt.Body.List))
  3007. for _, stmt := range tsStmt.Body.List {
  3008. cc, _ := stmt.(*ast.CaseClause)
  3009. // Exclude the 'default' case.
  3010. if len(cc.List) == 0 {
  3011. continue
  3012. }
  3013. Ts := make([]types.Type, len(cc.List))
  3014. for i, expr := range cc.List {
  3015. Ts[i] = pass.TypesInfo.TypeOf(expr)
  3016. }
  3017. ccs = append(ccs, ccAndTypes{cc: cc, types: Ts})
  3018. }
  3019. if len(ccs) <= 1 {
  3020. // Zero or one case clauses, nothing to check.
  3021. return
  3022. }
  3023. // Check if case clauses following cc have types that are subsumed by cc.
  3024. for i, cc := range ccs[:len(ccs)-1] {
  3025. for _, next := range ccs[i+1:] {
  3026. if T, V, yes := subsumesAny(cc.types, next.types); yes {
  3027. ReportNodef(pass, next.cc, "unreachable case clause: %s will always match before %s", T.String(), V.String())
  3028. }
  3029. }
  3030. }
  3031. }
  3032. pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.TypeSwitchStmt)(nil)}, fn)
  3033. return nil, nil
  3034. }
  3035. func CheckSingleArgAppend(pass *analysis.Pass) (interface{}, error) {
  3036. fn := func(node ast.Node) {
  3037. if !IsCallToAST(pass, node, "append") {
  3038. return
  3039. }
  3040. call := node.(*ast.CallExpr)
  3041. if len(call.Args) != 1 {
  3042. return
  3043. }
  3044. ReportfFG(pass, call.Pos(), "x = append(y) is equivalent to x = y")
  3045. }
  3046. pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.CallExpr)(nil)}, fn)
  3047. return nil, nil
  3048. }
  3049. func CheckStructTags(pass *analysis.Pass) (interface{}, error) {
  3050. fn := func(node ast.Node) {
  3051. for _, field := range node.(*ast.StructType).Fields.List {
  3052. if field.Tag == nil {
  3053. continue
  3054. }
  3055. tags, err := parseStructTag(field.Tag.Value[1 : len(field.Tag.Value)-1])
  3056. if err != nil {
  3057. ReportNodef(pass, field.Tag, "unparseable struct tag: %s", err)
  3058. continue
  3059. }
  3060. for k, v := range tags {
  3061. if len(v) > 1 {
  3062. ReportNodef(pass, field.Tag, "duplicate struct tag %q", k)
  3063. continue
  3064. }
  3065. switch k {
  3066. case "json":
  3067. checkJSONTag(pass, field, v[0])
  3068. case "xml":
  3069. checkXMLTag(pass, field, v[0])
  3070. }
  3071. }
  3072. }
  3073. }
  3074. pass.ResultOf[inspect.Analyzer].(*inspector.Inspector).Preorder([]ast.Node{(*ast.StructType)(nil)}, fn)
  3075. return nil, nil
  3076. }
  3077. func checkJSONTag(pass *analysis.Pass, field *ast.Field, tag string) {
  3078. //lint:ignore SA9003 TODO(dh): should we flag empty tags?
  3079. if len(tag) == 0 {
  3080. }
  3081. fields := strings.Split(tag, ",")
  3082. for _, r := range fields[0] {
  3083. if !unicode.IsLetter(r) && !unicode.IsDigit(r) && !strings.ContainsRune("!#$%&()*+-./:<=>?@[]^_{|}~ ", r) {
  3084. ReportNodef(pass, field.Tag, "invalid JSON field name %q", fields[0])
  3085. }
  3086. }
  3087. var co, cs, ci int
  3088. for _, s := range fields[1:] {
  3089. switch s {
  3090. case "omitempty":
  3091. co++
  3092. case "":
  3093. // allow stuff like "-,"
  3094. case "string":
  3095. cs++
  3096. // only for string, floating point, integer and bool
  3097. T := Dereference(pass.TypesInfo.TypeOf(field.Type).Underlying()).Underlying()
  3098. basic, ok := T.(*types.Basic)
  3099. if !ok || (basic.Info()&(types.IsBoolean|types.IsInteger|types.IsFloat|types.IsString)) == 0 {
  3100. ReportNodef(pass, field.Tag, "the JSON string option only applies to fields of type string, floating point, integer or bool, or pointers to those")
  3101. }
  3102. case "inline":
  3103. ci++
  3104. default:
  3105. ReportNodef(pass, field.Tag, "unknown JSON option %q", s)
  3106. }
  3107. }
  3108. if co > 1 {
  3109. ReportNodef(pass, field.Tag, `duplicate JSON option "omitempty"`)
  3110. }
  3111. if cs > 1 {
  3112. ReportNodef(pass, field.Tag, `duplicate JSON option "string"`)
  3113. }
  3114. if ci > 1 {
  3115. ReportNodef(pass, field.Tag, `duplicate JSON option "inline"`)
  3116. }
  3117. }
  3118. func checkXMLTag(pass *analysis.Pass, field *ast.Field, tag string) {
  3119. //lint:ignore SA9003 TODO(dh): should we flag empty tags?
  3120. if len(tag) == 0 {
  3121. }
  3122. fields := strings.Split(tag, ",")
  3123. counts := map[string]int{}
  3124. var exclusives []string
  3125. for _, s := range fields[1:] {
  3126. switch s {
  3127. case "attr", "chardata", "cdata", "innerxml", "comment":
  3128. counts[s]++
  3129. if counts[s] == 1 {
  3130. exclusives = append(exclusives, s)
  3131. }
  3132. case "omitempty", "any":
  3133. counts[s]++
  3134. case "":
  3135. default:
  3136. ReportNodef(pass, field.Tag, "unknown XML option %q", s)
  3137. }
  3138. }
  3139. for k, v := range counts {
  3140. if v > 1 {
  3141. ReportNodef(pass, field.Tag, "duplicate XML option %q", k)
  3142. }
  3143. }
  3144. if len(exclusives) > 1 {
  3145. ReportNodef(pass, field.Tag, "XML options %s are mutually exclusive", strings.Join(exclusives, " and "))
  3146. }
  3147. }