from a CommentGroup return a nilabilitySet of which identifiers are known annotated nilable
(group *ast.CommentGroup)
| 279 | |
| 280 | // from a CommentGroup return a nilabilitySet of which identifiers are known annotated nilable |
| 281 | func nilabilityFromCommentGroup(group *ast.CommentGroup) nilabilitySet { |
| 282 | set := make(nilabilitySet) |
| 283 | // in each of the following utility functions, isFinalVal=true because literally read annotations |
| 284 | // are considered final |
| 285 | markNilable := func(s string) { |
| 286 | if v, ok := set[s]; ok { |
| 287 | set[s] = v.makeNilable(true) |
| 288 | } else { |
| 289 | set[s] = EmptyVal.makeNilable(true) |
| 290 | } |
| 291 | } |
| 292 | markDeepNilable := func(s string) { |
| 293 | if v, ok := set[s]; ok { |
| 294 | set[s] = v.makeDeepNilable(true) |
| 295 | } else { |
| 296 | set[s] = EmptyVal.makeDeepNilable(true) |
| 297 | } |
| 298 | } |
| 299 | markNonNil := func(s string) { |
| 300 | if v, ok := set[s]; ok { |
| 301 | set[s] = v.makeNonNil(true) |
| 302 | } else { |
| 303 | set[s] = EmptyVal.makeNonNil(true) |
| 304 | } |
| 305 | } |
| 306 | markDeepNonNil := func(s string) { |
| 307 | if v, ok := set[s]; ok { |
| 308 | set[s] = v.makeDeepNonNil(true) |
| 309 | } else { |
| 310 | set[s] = EmptyVal.makeDeepNonNil(true) |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | if group != nil { |
| 315 | for _, comment := range group.List { |
| 316 | for _, seqMatch := range seqRegex.FindAllStringSubmatch(comment.Text, -1) { |
| 317 | |
| 318 | deepFunc, shallowFunc := markDeepNonNil, markNonNil |
| 319 | if seqMatch[1] == nilableKeyword { |
| 320 | deepFunc, shallowFunc = markDeepNilable, markNilable |
| 321 | } |
| 322 | |
| 323 | for match := range strings.SplitSeq(seqMatch[2], sep) { |
| 324 | match = strings.TrimSpace(match) |
| 325 | n := len(match) |
| 326 | |
| 327 | if n >= 2 && match[0] == '*' { |
| 328 | deepFunc(match[1:]) |
| 329 | continue |
| 330 | } |
| 331 | |
| 332 | if n >= 3 && match[n-2:] == "[]" { |
| 333 | deepFunc(match[:n-2]) |
| 334 | continue |
| 335 | } |
| 336 | |
| 337 | if n >= 3 && match[:2] == "<-" { |
| 338 | deepFunc(match[2:]) |
no test coverage detected