The point I was making is not simply to ignore a linter outcome, but add the reason behind it.
It might be a bad comment in your opinion, but that was not the point. Op di 7 mrt 2023 om 18:42 schreef Kurtis Rader <kra...@skepticism.us>: > On Mon, Mar 6, 2023 at 10:38 PM Marcello H <marcel...@gmail.com> wrote: > >> What I do (and is a good practice), is adding behind the comment more >> info. >> >> //nolint:errcheck // no need to test it, because it was tested before >> with stat > > > I appreciate that you're replying to what I hope is just a bad example but > please, never do that. It's a security bug. And even without a malicious > actor the path might change between the stat and the open. If you need to > check some attributes of the file always open it first (and check that the > open succeeded) then use the Stat method of the File object ( > https://pkg.go.dev/os#File.Stat). > > >> Op maandag 6 maart 2023 om 23:05:42 UTC+1 schreef Steven Hartland: >> >>> I would use the following if I truly wanted to ignore an error, but I >>> would question if ignoring the error from os.Open was ever a good idea. >>> If you want to handle a file not found error then do so, don't just ignore >>> the error. >>> file, _ := os.Open(name) //nolint: errcheck >>> >>> On Mon, 6 Mar 2023 at 20:14, Sergei Dyshel <qyron....@gmail.com> wrote: >>> >>>> Hello all, >>>> I'm incorporating golangci-lint >>>> <https://golangci-lint.run/usage/linters/> linter aggregator in my >>>> codebase and facing a dilemma on how to mark false positives for these 2 >>>> linters: >>>> >>>> - errcheck - reports unchecked errors. >>>> - unparam - reports unused function parameters. >>>> >>>> The "official" way is to use special comment directives that make >>>> linter ignore the line: >>>> >>>> func foo( >>>> name string, >>>> //nolint:unparam >>>> age int, >>>> ) { >>>> //nolint:errcheck >>>> file, _ := os.Open(name) >>>> .... >>>> >>>> Another, may be more portable way, would be using special functions >>>> which do nothing: >>>> >>>> // defined in some library utilities package >>>> func IgnoreErr(err error) {} >>>> func Unused(a any) {} >>>> >>>> func foo( >>>> name string, >>>> age int, >>>> ) { >>>> Unused(age) >>>> file, err := os.Open(name) >>>> IgnoreErr(err) >>>> ... >>>> >>>> What do you think the proper/idiomatic/better way among these two? >>>> >>>> -- >>>> You received this message because you are subscribed to the Google >>>> Groups "golang-nuts" group. >>>> To unsubscribe from this group and stop receiving emails from it, send >>>> an email to golang-nuts...@googlegroups.com. >>>> To view this discussion on the web visit >>>> https://groups.google.com/d/msgid/golang-nuts/6fc8a933-b68c-4e07-a4ee-98c4fe01ba8dn%40googlegroups.com >>>> <https://groups.google.com/d/msgid/golang-nuts/6fc8a933-b68c-4e07-a4ee-98c4fe01ba8dn%40googlegroups.com?utm_medium=email&utm_source=footer> >>>> . >>>> >>> -- >> You received this message because you are subscribed to the Google Groups >> "golang-nuts" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to golang-nuts+unsubscr...@googlegroups.com. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/golang-nuts/26a4f5d9-f590-4d40-a333-d0bfbe191ca3n%40googlegroups.com >> <https://groups.google.com/d/msgid/golang-nuts/26a4f5d9-f590-4d40-a333-d0bfbe191ca3n%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> > > > -- > Kurtis Rader > Caretaker of the exceptional canines Junior and Hank > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CADBSaR3gvCMdyKRSSfiPMO9S95cd9TphSkNKaAPSEskFsRLP0A%40mail.gmail.com.