Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False Positive when using errGroup #296

Open
QianKuang8 opened this issue Dec 5, 2024 · 0 comments
Open

False Positive when using errGroup #296

QianKuang8 opened this issue Dec 5, 2024 · 0 comments

Comments

@QianKuang8
Copy link

Nilaway fail to get the correct answer when using errgroup(case1). But it works fine in normal case(case2).

package main

import (
	"context"
	"fmt"
	"golang.org/x/sync/errgroup"
)

func getData(s string) (*string, error) {
	if s == "" {
		return nil, fmt.Errorf("empty data")
	}
	return &s, nil
}

// Not OK
func case1() {
	g, _ := errgroup.WithContext(context.Background())
	var s *string
	g.Go(func() error {
		var err error
		s, err = getData("hello")
		if err != nil {
			return err
		}
		return nil
	})
	if err := g.Wait(); err != nil {
		return
	}
	fmt.Println(*s)
}

// OK
func case2() {
	var s *string
	var err error
	s, err = getData("hello")
	if err != nil {
		return
	}
	fmt.Println(*s)
}

func main() {
	case1()
	case2()
}

// error: Potential nil panic detected. Observed nil flow from source to dereference point:
//	- hello_world/main.go:30:15: unassigned variable `s` dereferenced
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant