| 1 | // Copyright 2024 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // Package yield defines an Analyzer that checks for mistakes related |
| 6 | // to the yield function used in iterators. |
| 7 | // |
| 8 | // # Analyzer yield |
| 9 | // |
| 10 | // yield: report calls to yield where the result is ignored |
| 11 | // |
| 12 | // After a yield function returns false, the caller should not call |
| 13 | // the yield function again; generally the iterator should return |
| 14 | // promptly. |
| 15 | // |
| 16 | // This example fails to check the result of the call to yield, |
| 17 | // causing this analyzer to report a diagnostic: |
| 18 | // |
| 19 | // yield(1) // yield may be called again (on L2) after returning false |
| 20 | // yield(2) |
| 21 | // |
| 22 | // The corrected code is either this: |
| 23 | // |
| 24 | // if yield(1) { yield(2) } |
| 25 | // |
| 26 | // or simply: |
| 27 | // |
| 28 | // _ = yield(1) && yield(2) |
| 29 | // |
| 30 | // It is not always a mistake to ignore the result of yield. |
| 31 | // For example, this is a valid single-element iterator: |
| 32 | // |
| 33 | // yield(1) // ok to ignore result |
| 34 | // return |
| 35 | // |
| 36 | // It is only a mistake when the yield call that returned false may be |
| 37 | // followed by another call. |
| 38 | package yield |