Skip to content
This repository was archived by the owner on Nov 10, 2025. It is now read-only.

Some errors with --fix that can silently break code logicΒ #108

@ccoVeille

Description

@ccoVeille

Here is a piece of code

package foo

import (
	"encoding/xml"
	"strings"
)

func _(xmlInput string) {
	d := xml.NewDecoder(strings.NewReader(xmlInput))

	var tag struct {
		Whatever string `xml:"whatever"`
	}

	err := d.Decode(&tag)
	if err != nil {
		if syntaxErr, ok := err.(*xml.SyntaxError); ok && syntaxErr.Msg != "unexpected EOF" {
			panic("unexpected XML syntax error")
		}
	}
}

The logic of the code is crappy, that's not the point 😁

Here is what errorlint suggests:

type assertion on error will fail on wrapped errors. Use errors.As to check for specific errors (errorlint)
		if syntaxErr, ok := err.(*xml.SyntaxError); ok && syntaxErr.Msg != "unexpected EOF" {

And it's OK

What is not is what the code is replaced when using --fix

package foo

import (
	"encoding/xml"
	"errors"
	"strings"
)

func _(xmlInput string) {
	d := xml.NewDecoder(strings.NewReader(xmlInput))

	var tag struct {
		Whatever string `xml:"whatever"`
	}

	err := d.Decode(&tag)
	if err != nil {
		syntaxErr := &xml.SyntaxError{}
		if errors.As(err, &syntaxErr) {
			panic("unexpected XML syntax error")
		}
	}
}

The error.As is OK but a part of the test is nuked: && syntaxErr.Msg != "unexpected EOF" disappeared

I'm using buildID=0464206b09fb150f31313cdecad2010b30c95d892df7844ab7b297caea930e30

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions