-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial handling for iterators (#288)
NilAway is panicking on code that ranges over iterators. This PR adds initial handling for such that we assume the results from the iterators are always nonnil, without panicking. See #287
- Loading branch information
Showing
3 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright (c) 2023 Uber Technologies, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//go:build go1.23 | ||
|
||
// This file is meant for testing features in Go 1.23 and beyond. | ||
// TODO: Migrate these test cases in the mainstream test files once NilAway starts to support Go 1.23 is a base version. | ||
|
||
package nilaway | ||
|
||
import ( | ||
"testing" | ||
|
||
"golang.org/x/tools/go/analysis/analysistest" | ||
) | ||
|
||
func TestNilAway_Go123(t *testing.T) { | ||
t.Parallel() | ||
|
||
testdata := analysistest.TestData() | ||
|
||
// For descriptions of the purpose of each of the following tests, consult their source files | ||
// located in testdata/src/<package>. | ||
|
||
tests := []struct { | ||
name string | ||
patterns []string | ||
}{ | ||
{name: "LoopRangeGo123", patterns: []string{"go.uber.org/looprangego123"}}, | ||
} | ||
|
||
for _, tt := range tests { | ||
tt := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
t.Logf("Running test for packages %s", tt.patterns) | ||
|
||
analysistest.Run(t, testdata, Analyzer, tt.patterns...) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// This package aims to test nilability behavior for `range` in loops. | ||
|
||
// <nilaway no inference> | ||
package looprange | ||
|
||
import ( | ||
"maps" | ||
"slices" | ||
) | ||
|
||
func testIter() { | ||
i := 42 | ||
for element := range slices.Values([]*int{&i, &i, nil}) { | ||
print(*element) // FN: we do not really handle iterators for now, the elements from iterators are assumed to be nonnil. | ||
} | ||
for k, v := range maps.All(map[string]*int{"abc": &i, "def": nil}) { | ||
print(k) | ||
print(*v) // FN: we do not really handle iterators for now, the elements from iterators are assumed to be nonnil. | ||
} | ||
} |