From 5275b914fbe26ac5dc514935d0ac611b3db22b6c Mon Sep 17 00:00:00 2001 From: Dominik Honnef Date: Tue, 21 May 2024 13:36:17 +0200 Subject: [PATCH] qf1001: handle expressions that have no type Closes: gh-1484 Closes: gh-1510 --- quickfix/qf1001/qf1001.go | 10 ++++++---- .../testdata/src/example.com/CheckDeMorgan/kvexpr.go | 11 +++++++++++ .../src/example.com/CheckDeMorgan/kvexpr.go.golden | 12 ++++++++++++ 3 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 quickfix/qf1001/testdata/src/example.com/CheckDeMorgan/kvexpr.go create mode 100644 quickfix/qf1001/testdata/src/example.com/CheckDeMorgan/kvexpr.go.golden diff --git a/quickfix/qf1001/qf1001.go b/quickfix/qf1001/qf1001.go index d6d3811b3..41625f921 100644 --- a/quickfix/qf1001/qf1001.go +++ b/quickfix/qf1001/qf1001.go @@ -40,10 +40,12 @@ func CheckDeMorgan(pass *analysis.Pass) (interface{}, error) { found := false ast.Inspect(expr, func(node ast.Node) bool { if expr, ok := node.(ast.Expr); ok { - if basic, ok := pass.TypesInfo.TypeOf(expr).Underlying().(*types.Basic); ok { - if (basic.Info() & types.IsFloat) != 0 { - found = true - return false + if typ := pass.TypesInfo.TypeOf(expr); typ != nil { + if basic, ok := typ.Underlying().(*types.Basic); ok { + if (basic.Info() & types.IsFloat) != 0 { + found = true + return false + } } } } diff --git a/quickfix/qf1001/testdata/src/example.com/CheckDeMorgan/kvexpr.go b/quickfix/qf1001/testdata/src/example.com/CheckDeMorgan/kvexpr.go new file mode 100644 index 000000000..8a6f82e1c --- /dev/null +++ b/quickfix/qf1001/testdata/src/example.com/CheckDeMorgan/kvexpr.go @@ -0,0 +1,11 @@ +package pkg + +func do() bool { + type Info struct { + idx int + } + + var state map[Info]int + // Don't crash on KeyValueExpr + return !(state[Info{idx: 6}] == 6 || false) //@ diag(`could apply De Morgan's law`) +} diff --git a/quickfix/qf1001/testdata/src/example.com/CheckDeMorgan/kvexpr.go.golden b/quickfix/qf1001/testdata/src/example.com/CheckDeMorgan/kvexpr.go.golden new file mode 100644 index 000000000..2ba7c5758 --- /dev/null +++ b/quickfix/qf1001/testdata/src/example.com/CheckDeMorgan/kvexpr.go.golden @@ -0,0 +1,12 @@ +-- Apply De Morgan's law -- +package pkg + +func do() bool { + type Info struct { + idx int + } + + var state map[Info]int + // Don't crash on KeyValueExpr + return state[Info{idx: 6}] != 6 && !false //@ diag(`could apply De Morgan's law`) +}