Skip to content

Report slow string convertions using fmt #1036

@Jacalz

Description

@Jacalz

I think it would be very good if staticcheck could flag cases where people are using fmt for just simply converting to and from strings. It was found in my PR for fyne-io/fyne (fyne-io/fyne#2142 (comment)) that the strconv package generally is twice as fast as the fmt package for converting to and from strings. While it is very easy and simple to do so, I think it is a poor decision most of the time.

Below are a few examples of what I would suggest flagging:

// With a single format and equivalent format type to what is being passed.
fmt.Sprintf("%", val)

// Just passing one variable.
fmt.Sprint(val)
fmt.Sprintln(val)

// With a single format and equivalent format type to what is being passed.
fmt.Sscanf(str, "%", &val)

// Just passing one variable.
fmt.Scan(&val)
fmt.Scanln(&val)

All these cases can quite easily be changed to using strconv for pretty big performance improvements.

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-decisionWe have to decide if this check is feasible and desirablenew-check

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions