Go-tools: Incorrect return simplification

Created on 25 Feb 2018  路  6Comments  路  Source: dominikh/go-tools

func (t *trainer) Train(labels []string, description string) error {
...
    if _, err := fmt.Fprintln(t.bufTrainingFile); err != nil {
        return err
    }
    return nil
}

yields

$ gosimple *.go
trainer.go:89:2:warning: 'if err != nil { return err }; return nil' can be simplified to 'return err' (S1013) (megacheck)

but

func (t *trainer) Train(labels []string, description string) error {
...
    return fmt.Fprintln(t.bufTrainingFile)
}

would never work since fmt.Fprintln returns multiple values.

Additional info:

$ gosimple --version
gosimple 2017.2
false-positive

Most helpful comment

That鈥檚 unfortunate. :( I like this check and want it to trigger in all applicable cases. But I understand.

All 6 comments

Can confirm, will fix.

Is this really a false-positive? I think it should be simplified to this:

_, err := fmt.Fprintln(t.bufTrainingFile)
return err

Yes, but in that case I think we need a different error message.

@dominikh How are you planning on fixing this? I agree with above, this doesn鈥檛 seem like a false positive, just the message wording might need adjusting to make it more applicable.

Also worth pointing out that the message said it can be simplified to return err, not to return fmt.Fprintln(...).

@shurcooL We'll see. That entire check has been a thorn in my side because I feel that it's too eager at times. It makes suggestions that only minimally improve code and that sometimes break consistency.

As a first step, I will likely restrict it to if conditions with no assignment, simply to reduce the number of times it makes the suggestion. In the future, I may further reduce the instances on which it makes the suggestion.

That鈥檚 unfortunate. :( I like this check and want it to trigger in all applicable cases. But I understand.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jtreleaven picture jtreleaven  路  3Comments

jadekler picture jadekler  路  3Comments

tonyghita picture tonyghita  路  4Comments

tallclair picture tallclair  路  3Comments

axcdnt picture axcdnt  路  3Comments